
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
NextMock is an enhanced mock for unittest.mock.Mock.
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
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
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
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)
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)
© Chun-Yan Ho (pilagod), 2020-NOW
Released under the MIT License
FAQs
NextMock is an enhanced mock for unittest.mock.Mock
We found that nextmock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.