NextMock is an enhanced mock for unittest.mock.Mock
Project description
NextMock
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"
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
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
File details
Details for the file nextmock-0.0.0.tar.gz
.
File metadata
- Download URL: nextmock-0.0.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56ea02d126d607472d5fc7c996748aa2493c8129aa05117ad0124409c62ba808 |
|
MD5 | c13d079b2ba5d435bbb114d4a61ea006 |
|
BLAKE2b-256 | 26976f92ae9b80af41f3f0960b17aa68baa3292ec1048d8c3018e55b38b93fa5 |
File details
Details for the file nextmock-0.0.0-py3-none-any.whl
.
File metadata
- Download URL: nextmock-0.0.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c1dd0fde6c5f75fff7b119800159cbad276d27c6dd0785f5793e9657180e4c3 |
|
MD5 | fa3438d2320a30b8cf0dad96168771dd |
|
BLAKE2b-256 | bd0afa5fd429ebcf892beaf6e21a9df47b2af5a96e34bc8bfe59da45cd8aca18 |