Create a mock object for a Decorator
Project description
Mock Decorator
Mock Decorator simplifes the process of creating a mock for a decorator.
Features
- Can Mock Function Decorators.
- Can Mock Class Decorators.
- Can Mock Nested Decorators.
- Can Mock Simple Decorators. - like
@my_decorator - Can Mock Decorators with arguments. - like
@my_decorator(1) - Allows you to verify which Class or Function is decorated.
- Allows you to verify arguments passed to the decorator.
Why Use Mock Decorator?
Mocking a decorator in python is more difficult than mocking other objects. The trick is that part of the decorator code is executed at load time, and part is executed when the target function is called. Primer on Python Decorators is the best article I've found explaining how decorators work.
When possible, it's best to validate the presence of a decorator by validating the effect(s) of the decorator. But sometimes a mock is required.
To mock a decorator, the decorator definition must be replaced with a mock before the decorated function is loaded. How to mock a decorator in Python explains a method for creating a mock for a decorator. There are also many examples in Stack Overflow. But I find it very tedious to create a decorator mock. And tricky to figure out which method was decorated, and what arguments were passed to the decorator.
Mock Decorator is an implementation of a general purpose decorator mock. It tracks which function(s) were decorated, and which arguments were passed to the decorator.
Installation
pip install mock-decorator --upgrade
Usage
Note: The decorator to be mocked, and the function or class being decorated must be located in different modules.
Example Usage:
my_functions.py
from my_decorators import decorator_with_args
@my_dec(1,2)
def my_func(arg1, arg2="default"):
return f"my_func: {arg1} {arg2} done"
test_my_functions.py
from mock_decorator import MockDecorator
import my_decorators, my_functions
from unittest import mock
def test_my_func():
with mock.patch.object(my_decorators, "my_dec", MockDecorator()) as mock_dec: # 1
importlib.reload(my_functions) # 2
mock_dec.my_func.assert_called_once_with(1,2) # 3
-
Replace the decorator in its module with an instance of MockDecorator.
Template using mock from unitest:
with mock.patch.object(<decorator module>, "<decorator name>", MockDecorator()) as <mock_name>:
-
Import or Reload the module that includes the decorated function.
During the loading of the module, the MockDecorator is called to create the decorator wrapper function. At this time, the MockDecorator will add a MagicMock attribute to itself with the name of the target function. It will then call the new attribute with the arguments passed to the decorator.
-
Validate the mocked decorator was called with expected arguments.
Template:
<mock_name>.<decorated function name>.assert_called_once_with(<decorator args>)
Contribute
- Issue Tracker: https://github.com/WiredNerd/mock-decorator/issues
- Source Code: https://github.com/WiredNerd/mock-decorator
Support
If you are having issues, please let us know.
I can be contacted at: pbuschmail-github@yahoo.com
Or by opening an issue: https://github.com/WiredNerd/mock-decorator/issues
License
The project is licensed under the MIT license.
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 mock_decorator-1.0.1.tar.gz.
File metadata
- Download URL: mock_decorator-1.0.1.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57e94315f1c683aac06a982114a1a48770943d8c0f83881d57826ac95326ddbc
|
|
| MD5 |
81de6e4f67c7d2cfe6e90544374548fd
|
|
| BLAKE2b-256 |
dc22aaff858c46e438263d8fffd237172776dd061ddd433147747696377bd150
|
Provenance
The following attestation bundles were made for mock_decorator-1.0.1.tar.gz:
Publisher:
python-publish.yml on WiredNerd/mock-decorator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mock_decorator-1.0.1.tar.gz -
Subject digest:
57e94315f1c683aac06a982114a1a48770943d8c0f83881d57826ac95326ddbc - Sigstore transparency entry: 1231193213
- Sigstore integration time:
-
Permalink:
WiredNerd/mock-decorator@9aa3efd10b53b85603e3294522d508cea6228af3 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/WiredNerd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9aa3efd10b53b85603e3294522d508cea6228af3 -
Trigger Event:
release
-
Statement type:
File details
Details for the file mock_decorator-1.0.1-py3-none-any.whl.
File metadata
- Download URL: mock_decorator-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
200ebf495757c3bea323e4c68b5562dcfad44bcf778842ec5a0be5a90fa4cde3
|
|
| MD5 |
edaa17a8ce1c88c0925a39eec18b0e1e
|
|
| BLAKE2b-256 |
621ddb363274abce7f283fa582ebac739ab07a853aadd96b47420c6cd9127dcc
|
Provenance
The following attestation bundles were made for mock_decorator-1.0.1-py3-none-any.whl:
Publisher:
python-publish.yml on WiredNerd/mock-decorator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mock_decorator-1.0.1-py3-none-any.whl -
Subject digest:
200ebf495757c3bea323e4c68b5562dcfad44bcf778842ec5a0be5a90fa4cde3 - Sigstore transparency entry: 1231193220
- Sigstore integration time:
-
Permalink:
WiredNerd/mock-decorator@9aa3efd10b53b85603e3294522d508cea6228af3 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/WiredNerd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9aa3efd10b53b85603e3294522d508cea6228af3 -
Trigger Event:
release
-
Statement type: