Highly Deterministic Mocking
Project description
StrictMock
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.
- Each method must be tested separately.
- Negative tests must be explicitly added to verify that specific method was mot called.
- Quite often devs will verify that a method was called, but not what values are actually passed in.
What StrictMock Brings
- A single set of expected values that list every action the mock takes.
- All actions the mock take are explicitly ordered.
- Any discrepancy causes an error to be raised immediately.
- The error will tell what needs to be done to fix the issue.
- The errors raised will be a child of MockError to further indicate it is a StrictMock that has detected a discrepancy.
- 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
- Methods on a class
- Decorator Properties
- Class Properties
- Artificial Properties
- Functions and Functors (Callable)
- Iterators
- Context Managers
- Mixed Objects (e.g object that acts as both a class and a container)
- Modules (module.classes and module.functions)
Limitations
- Since the StrictMock addresses all events in a specific order, it may only be used for synchronous testing.
- 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.
- 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.
- 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.
- Modules global instances are not directly supported.
- Module Constants should not be mocked. They are constants after all.
- There is not a patch feature. (Note: I am strongly biased towards dependency injection anyway).
- Async is not officially supported as of yet.
- Threading is not officially supported as of yet.
- Importing classes with the same name from different modules may cause problems.
Installing
Once it is officially released.
Using the StrictMock
Concepts and usage are further explored in ReadMeUsage
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 .
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strictmock-0.5.0.tar.gz.
File metadata
- Download URL: strictmock-0.5.0.tar.gz
- Upload date:
- Size: 37.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71e80a20e3c43f621e8dfcfa34f615c319b7cb1350bc7af1ac1bbf579729cac0
|
|
| MD5 |
3a6f1852318d697d8f0670535d884eb8
|
|
| BLAKE2b-256 |
7d923f3a6e548acf64087575438972dd82e526cfdcd0a407c53bab71d83d8495
|
File details
Details for the file strictmock-0.5.0-py3-none-any.whl.
File metadata
- Download URL: strictmock-0.5.0-py3-none-any.whl
- Upload date:
- Size: 45.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b58c51d77468b3625094af182309361a3f070478b9bfd6083ba0a9460889b7bf
|
|
| MD5 |
5625bc40bc851cd112fc61b1e6733eb5
|
|
| BLAKE2b-256 |
7f6e03260af5095f692af4570e9826f510d7e4be65c7801a66c83b4b2e21ab21
|