Skip to main content

NextMock Build Status Coverage Status

NextMock is an enhanced mock for unittest.mock.Mock.

Features

  • Argument matching supported.
  • Async version (AsyncMock) provided.
  • Compatible with unittest.mock.Mock.

Usage

First install nextmock from pip:

$ pip install nextmock

then import Mock for common usage, AsyncMock for async usage:

from nextmock import Mock
from nextmock import AsyncMock

API with Examples

with_args

Return/raise stub result/error only when given args are matched.

Check out /nextmock/test/test_mock_with_args.py for comprehensive exmaples.

  • args matching

    m = Mock()
    
    m.with_args(1, 2, 3).returns(123)
    
    assert m(1, 2, 3) == 123
    assert m(3, 2, 1) != 123
    
  • kwargs matching

    m = Mock()
    
    m.with_args(a=1, b=2, c=3).returns(123)
    
    assert m(a=1, b=2, c=3) == 123
    assert m(a=3, b=2, c=1) != 123
    
  • class matching

    class Cmd:
        def __init__(self, a: int, b: str):
            self.a = a
            self.b = b
    
    m = Mock()
    
    m.with_args(Cmd(1, "123")).returns(123)
    
    assert m(Cmd(1, "123")) == 123
    assert m(Cmd(999, "321")) != 123
    
  • args matcher

    from nextmock import Arg
    
    m = Mock()
    
    m.with_args(1, 2, Arg.Any).returns(123)
    
    assert m(1, 2, 1) == 123
    assert m(1, 2, 9) == 123
    assert m(1, 2, "123") == 123
    
  • error raising

    m = Mock()
    
    m.with_args(1, 2, 3).raises(ValueError("value error"))
    
    with pytest.raises(ValueError) as e:
        m(1, 2, 3)
    
    assert str(e.value) == "value error"
    
  • enum matching (0.0.1)

    class Category(Enum):
        A = "a"
        B = "b"
    
    m = Mock()
    
    m.with_args(Category.A).returns(123)
    
    assert m(Category.A) == 123
    assert m(Category.B) != 123
    

returns

Return stub result without matching args.

m = Mock()

m.returns(123)

assert m(1, 2, 3) == 123
assert m(a=1, b=2, c=3) == 123

raises

Raise stub error without matching args.

m = Mock()

m.raises(ValueError("value error"))

with pytest.raises(ValueError) as e:
    m(1, 2, 3)

with pytest.raises(ValueError) as e:
    m(a=1, b=2, c=3)

Compatibility

Inherit behavior from unittest.mock.Mock.

Check out /nextmock/test/test_mock_compatibility.py for comprehensive examples.

m = Mock()

m.return_value = 123

assert m(1, 2, 3) == 123

m.assert_called_once()
m.assert_called_with(1, 2, 3)

License

© Chun-Yan Ho (pilagod), 2020-NOW

Released under the MIT License

Metadata

Release files for nextmock 0.0.1

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

Source distribution (sdist)

Source distribution for nextmock 0.0.1
File Size Uploaded
nextmock-0.0.1.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nextmock 0.0.1
File Interpreter ABI Platform
nextmock-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / nextmock-0.0.1.tar.gz

Download URL nextmock-0.0.1.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
596d5de3d9793393cd35754c615eedc1bd80edb55c090f1d53d9a58b6886adbc
BLAKE2b-256 checksum
How to use checksums
3bcf5717a67c6e3242696ed4af62de9108acc2088067a92c70c9bf5ca9932912
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.7

Release files / nextmock-0.0.1-py3-none-any.whl

Download URL nextmock-0.0.1-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa72a9351603da28c1c705645fd03031c2cbc7c77b7cdab2566f1a8e51209a7e
BLAKE2b-256 checksum
How to use checksums
08ac45db3c56105cec6eec947d0d11b95fcff5fd167cbe0262d34cdeae764c82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.7

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

0.0.0

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