Skip to main content

StrictMock

Installing

pip install StrictMock

Supported Versions

Python 3.11 unittest Python 3.12 unittest Python 3.13 unittest Python 3.14 unittest

Pytest 8.4.2 or greater

Design Concepts and Goals

Mocking a useful and standard tool for building unit tests. However, I have found a few frustrations with using them. Most of these frustrations are around how developers use mocks more than how the mock actually function; but the underlying issue is that it is easy for developers to make several mistakes resulting in a passing tests, when the test should have failed.

  1. Each method must be tested separately.
  2. Negative tests must be explicitly added to verify that specific method was mot called.
  3. Quite often devs will verify that a method was called, but not what values are actually passed in.

What StrictMock Brings

  1. A single set of expected values that list every action the mock takes.
  2. All actions the mock take are explicitly ordered.
  3. Any discrepancy causes an error to be raised immediately.
  4. The error will tell what needs to be done to fix the issue.
  5. The errors raised will be a child of MockError to further indicate it is a StrictMock that has detected a discrepancy.
  6. Each StrictMock will identify itself when raising an error so that a developer will be able to easily identify it for updating.

A StrictMock takes a fail fast approach. As soon as it encounters any discrepancy between what is expected and what has actually happened, it will raise an error. This is based on the simple premise that if an error occurs early on, then you cannot trust any events afterwards.

When an error is raised, it will be a subclass of MockError. This tells you that the error is due to an input (or missing return value). Thus you know you need to fix some aspect of the mock.

Another advantage of this is that if code being tested is modified, then the StrictMock will immediately start raising errors.

Finally, you may set up the StrictMock, not set any Expected events, and run the test. The Strict Mock will start giving you suggestions on how to fix the missing Expected events.

Supported Actions

  1. Methods on a class
  2. Decorator Properties
  3. Class Properties
  4. Artificial Properties
  5. Functions and Functors (Callable)
  6. Iterators
  7. Context Managers
  8. Mixed Objects (e.g object that acts as both a class and a container)
  9. Modules (module.classes and module.functions)

Limitations

  1. Properties created with decorators don't have type info, thus strict_mock will default to a type of Any. This can be worked around by using Artificial Properties.
  2. Attributes that become assigned to an instance after it is initialized will not automatically be created. This is due to the fact that they do not exist as part of the class. Once again, Artificial Properties provide a solution.
  3. There is an expectation that eq is correctly implemented for all types being used for args in the tests. In the event a type does not implement eq correctly, then you may use IValueEqual as a work around.
  4. Modules global instances are not directly supported.
  5. Module Constants should not be mocked. They are constants after all.
  6. There is not a patch feature. (Note: I am strongly biased towards dependency injection anyway).
  7. Threading is not officially supported as of yet.
  8. Importing classes with the same name from different modules may cause problems.

Using the StrictMock

Concepts and usage are further explored in ReadMeUsage

Example files are also provided. These are all unit tests that also provide usage documentation. Each of these files has the prefix "test_example". Furthermore, some of these files have the suffix "_errors". These files show common errors that may occur while creating the StrictMock.

Set Up Local Development Environment

python3 -m venv venv

source venv/bin/activate

pip install --upgrade pip

pip install -r requirements.txt

python setup.py egg_info

Linting / Unit tests

sh check.sh

linting only

sh lint.sh

unit tests only

pytest --cov=strict_mock tests --cov-report term-missing

Stub Generation

stubgen -p strict_mock -o .

Versions

0.5.0

Initial Release

0.6.3

Removed requirement for pytest

0.6.5

Verified support for 3.11 - 3.14

Metadata

Release files for StrictMock 0.6.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for StrictMock 0.6.6
File Size Uploaded Provenance Actions
strictmock-0.6.6.tar.gz 37.2 kB Without provenance Details for strictmock-0.6.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for StrictMock 0.6.6
File Interpreter ABI Platform Actions
strictmock-0.6.6-py3-none-any.whl Python 3 none any Details for strictmock-0.6.6-py3-none-any.whl

Total release size: 91.7 kB

Release files / strictmock-0.6.6.tar.gz

Download URL strictmock-0.6.6.tar.gz
Size 37.2 kB
Tags Source
SHA-256 checksum
How to use checksums
16feaadcf25785f4f1d1419f1a23e856549998d92f0c4a9a2a26503509a37506
BLAKE2b-256 checksum
How to use checksums
de389a0c835e3088c6594ff7d3e822dfe3796e958ad7e8cb15ff10ba93f8153f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / strictmock-0.6.6-py3-none-any.whl

Download URL strictmock-0.6.6-py3-none-any.whl
Size 54.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34d22704c077a9df7db0703fcb4aef8cb3bc3df4f57a61b1dd7cf0188049a21f
BLAKE2b-256 checksum
How to use checksums
e084d7e45ac46c336190d133e1ae16e046b4cb857fd32a7565ff9452cbfdadea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for StrictMock 0.6.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page