{"id":13816696,"url":"https://github.com/pilagod/nextmock","last_synced_at":"2025-09-06T08:31:27.475Z","repository":{"id":52834555,"uuid":"325505328","full_name":"pilagod/nextmock","owner":"pilagod","description":"NextMock is an enhanced mock for unittest.mock.Mock","archived":false,"fork":false,"pushed_at":"2021-04-17T07:26:24.000Z","size":26,"stargazers_count":17,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-02T12:44:58.774Z","etag":null,"topics":["async","mock","python","stub","unit-testing"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/nextmock/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pilagod.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-30T09:11:03.000Z","updated_at":"2024-07-13T08:52:33.000Z","dependencies_parsed_at":"2022-09-26T17:30:30.205Z","dependency_job_id":null,"html_url":"https://github.com/pilagod/nextmock","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pilagod%2Fnextmock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pilagod%2Fnextmock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pilagod%2Fnextmock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pilagod%2Fnextmock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pilagod","download_url":"https://codeload.github.com/pilagod/nextmock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232105450,"owners_count":18473304,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","mock","python","stub","unit-testing"],"created_at":"2024-08-04T05:00:50.166Z","updated_at":"2025-01-01T17:20:13.419Z","avatar_url":"https://github.com/pilagod.png","language":"Python","readme":"# NextMock [![Build Status](https://travis-ci.com/pilagod/nextmock.svg?branch=master)](https://travis-ci.com/pilagod/nextmock) [![Coverage Status](https://coveralls.io/repos/github/pilagod/nextmock/badge.svg?branch=master)](https://coveralls.io/github/pilagod/nextmock?branch=master)\n\nNextMock is an enhanced mock for [unittest.mock.Mock](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock).\n\n## Features\n\n- Argument matching supported.\n- Async version (AsyncMock) provided.\n- Compatible with [unittest.mock.Mock](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock).\n\n## Usage\n\nFirst install `nextmock` from pip:\n\n```shell\n$ pip install nextmock\n```\n\nthen import Mock for common usage, AsyncMock for async usage:\n\n```python\nfrom nextmock import Mock\nfrom nextmock import AsyncMock\n```\n\n## API with Examples\n\n### with_args\n\nReturn/raise stub result/error only when given args are matched.\n\n\u003e Check out [`/nextmock/test/test_mock_with_args.py`](https://github.com/pilagod/nextmock/blob/master/nextmock/test/test_mock_with_args.py) for comprehensive exmaples.\n\n- args matching\n\n    ```python\n    m = Mock()\n\n    m.with_args(1, 2, 3).returns(123)\n\n    assert m(1, 2, 3) == 123\n    assert m(3, 2, 1) != 123\n    ```\n\n- kwargs matching\n\n    ```python\n    m = Mock()\n\n    m.with_args(a=1, b=2, c=3).returns(123)\n\n    assert m(a=1, b=2, c=3) == 123\n    assert m(a=3, b=2, c=1) != 123\n    ```\n\n- class matching\n\n    ```python\n    class Cmd:\n        def __init__(self, a: int, b: str):\n            self.a = a\n            self.b = b\n    \n    m = Mock()\n\n    m.with_args(Cmd(1, \"123\")).returns(123)\n\n    assert m(Cmd(1, \"123\")) == 123\n    assert m(Cmd(999, \"321\")) != 123\n    ```\n\n- args matcher\n\n    ```python\n    from nextmock import Arg\n\n    m = Mock()\n\n    m.with_args(1, 2, Arg.Any).returns(123)\n\n    assert m(1, 2, 1) == 123\n    assert m(1, 2, 9) == 123\n    assert m(1, 2, \"123\") == 123\n    ```\n\n- error raising\n\n    ```python\n    m = Mock()\n\n    m.with_args(1, 2, 3).raises(ValueError(\"value error\"))\n\n    with pytest.raises(ValueError) as e:\n        m(1, 2, 3)\n\n    assert str(e.value) == \"value error\"\n    ```\n\n- enum matching (0.0.1)\n\n    ```python\n    class Category(Enum):\n        A = \"a\"\n        B = \"b\"\n\n    m = Mock()\n\n    m.with_args(Category.A).returns(123)\n\n    assert m(Category.A) == 123\n    assert m(Category.B) != 123\n    ```\n\n### returns\n\nReturn stub result without matching args.\n\n```python\nm = Mock()\n\nm.returns(123)\n\nassert m(1, 2, 3) == 123\nassert m(a=1, b=2, c=3) == 123\n```\n\n### raises\n\nRaise stub error without matching args.\n\n```python\nm = Mock()\n\nm.raises(ValueError(\"value error\"))\n\nwith pytest.raises(ValueError) as e:\n    m(1, 2, 3)\n\nwith pytest.raises(ValueError) as e:\n    m(a=1, b=2, c=3)\n```\n\n## Compatibility\n\nInherit behavior from [unittest.mock.Mock](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock).\n\n\u003e Check out [`/nextmock/test/test_mock_compatibility.py`](https://github.com/pilagod/nextmock/blob/master/nextmock/test/test_mock_compatibility.py) for comprehensive examples.\n\n```python\nm = Mock()\n\nm.return_value = 123\n\nassert m(1, 2, 3) == 123\n\nm.assert_called_once()\nm.assert_called_with(1, 2, 3)\n```\n\n## License\n\n© Chun-Yan Ho (pilagod), 2020-NOW\n\nReleased under the [MIT License](https://github.com/pilagod/nextmock/blob/master/LICENSE)\n","funding_links":[],"categories":["Topics Index"],"sub_categories":["Unit Testing"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpilagod%2Fnextmock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpilagod%2Fnextmock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpilagod%2Fnextmock/lists"}