Skip to main content

unittest-mocker

build codecov PyPI version

Inspired by the pytest-mock, but written from scratch for using with unittest and convenient tool - Class mocker

Installation

pip install unittest-mocker

Usage

General mocker

@activate_mocker
def test_create_client(self, mocker: Mocker):
    post_mock = mocker.patch('requests.post')
    post_mock.return_value.status_code = 200
    post_mock.return_value.json.return_value = {'msg': done}
    
    send_clients_to_crm()
    
    post_mock.assert_called_once_with(...)

or you can put the mocking logic in a separate method:

def mock_post(mocker, status):
    response = requests.Response()
    response.status_code = status
    response.headers = {'Location': '...'}
    response._content = 'some content'
    
    return mocker.patch('requests.post', return_value=response)

@activate_mocker
def test_upload_post_success(mocker):
    mocked_post = mock_post(mocker, 201)
    ...
@activate_mocker
def test_upload_post_error(mocker):
    mocked_post = mock_post(mocker, 500)
    ...

Details

mocker.patch under the hood calls unittest.mock.patch So you can apply any arguments of the standard mock library

The same with mocker.patch.object. It has the same parameters asunittest.mock.patch.object

Class mocker

Basic usage:

from unittest_mocker import activate_mocker, Mocker
from api import ApiClient

@activate_mocker
def test_api_client(self, mocker: Mocker):
    client_mock = mocker.patch_class('api.ApiClient')

    ApiClient(url='example.com')(command='start')
    
    client_mock.test_initialized_with(url='example.com')
    client_mock.mocks['__call__'].assert_called_once_with(command='start')

Syntax

# Variant 1
mocker.patch_class(target: str, methods=Optional[list[str]])
# Example:
mock = mocker.patch_class('some_module.SomeClass', methods=['method_1', 'method_2'])

# Variant 2
mocker.patch_class.object(target: Union[module, class], attribute: str, methods=Optional[list[str]])
# Example:
import some_module
mock = mocker.patch_class(some_module, 'SomeClass', methods=['method_1', 'method_2'])

All mocked methods are stored in mock.mocks dictionary and can be accessed:

mock['__init__'].assert_called_once_with(...)

patch_class always patches class's __init__ method.

Also, methods parameter also defaults to [__call__]. So that's correct:

mock = mocker.patch_class('some_module.SomeClass')
...
mock.mocks['__call__'].assert_called_once_with(...)

Helper test_initialized_with

mock.test_initialized_with() is just shortcut for mock['__init__'].assert_called_once_with()

Metadata

Release files for unittest-mocker 0.9.0

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

Source distribution (sdist)

Source distribution for unittest-mocker 0.9.0
File Size Uploaded
unittest-mocker-0.9.0.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unittest-mocker 0.9.0
File Interpreter ABI Platform
unittest_mocker-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.6 kB

Release files / unittest-mocker-0.9.0.tar.gz

Download URL unittest-mocker-0.9.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6589aa6509aebe669986e5388fb77639ab10e8a439bf0499d8e9ad056a88fab3
BLAKE2b-256 checksum
How to use checksums
e1bb43216ae7a99215e16aa7a7f2fe505729658d8d33abfe00b2c0a1c07736a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

Release files / unittest_mocker-0.9.0-py3-none-any.whl

Download URL unittest_mocker-0.9.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
761e89fe36897fb6e5252776e4a8dfa853db15aec6ff684e8762a5ef8de4e501
BLAKE2b-256 checksum
How to use checksums
32757711411dd37e99ef59b36df78aecad3e4bccfc5b537036e7c32f965c5242
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files

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