chainmock


Namechainmock JSON
Version 0.11.0 PyPI version JSON
download
home_pagehttps://github.com/ollipa/chainmock
SummaryMocking library for Python and Pytest
upload_time2024-03-23 13:57:21
maintainerNone
docs_urlNone
authorOlli Paakkunainen
requires_python<4.0,>=3.8
licenseMIT
keywords testing mock mocking pytest
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
  <img alt="chainmock" src="https://github.com/ollipa/chainmock/assets/25169984/ad243761-b2ed-4aff-89e7-699e2b4a7e6d">
</p>

<p align="center"><strong>Chainmock</strong> <em>- Mocking library for Python and Pytest.</em></p>

<p align="center">
<a href="https://pypi.org/project/chainmock/">
  <img src="https://img.shields.io/pypi/v/chainmock" alt="pypi">
</a>
<a href="https://github.com/ollipa/chainmock/actions/workflows/ci.yml">
  <img src="https://github.com/ollipa/chainmock/actions/workflows/ci.yml/badge.svg" alt="ci">
</a>
<a href="https://chainmock.readthedocs.io/">
  <img src="https://img.shields.io/readthedocs/chainmock" alt="documentation">
</a>
<a href="./LICENSE">
  <img src="https://img.shields.io/pypi/l/chainmock" alt="license">
</a>
</p>

<hr>

<p align="center">
<a href="https://chainmock.readthedocs.io/">
  <b>Documentation</b>
</a>
</p>

<hr>

Chainmock is a mocking Library for Python and pytest. Under the hood it uses Python standard library mocks providing an alternative syntax to create mocks and assertions. Chainmock also comes with some additional features to make testing faster and more straightforward. The syntax works especially well with pytest fixtures.

## Installation

Install with pip:

```
pip install chainmock
```

## Features

Chainmock supports all the same features that Python standard library unittest
mocks support and adds some convenient extra functionality.

- **Mocking**: Create _mocks_ and assert call counts and arguments or replace
  return values.
- **Spying**: _Spying_ proxies the calls to the original function or method.
  With spying you can assert call counts and arguments without mocking.
- **Stubs**: Easily create _stub_ objects that can be used in tests as fake data
  or to replace real objects.
- **Async support**: Chainmock supports mocking and spying _async_ functions and
  methods. Most of the time it also recognizes automatically when async mocking
  should be used so it is not any harder than mocking sync code.
- **Fully type annotated**: The whole codebase is fully type annotated so
  Chainmock works well with static analysis tools and editor autocomplete.
- Works with **Python 3.8+ and PyPy3**.
- Supports `pytest`, `unittest`, and `doctest` test runners.

## Examples

The entrypoint to Chainmock is the `mocker` function. Import the `mocker`
function as follows:

```python
from chainmock import mocker
```

### Mocking

To mock you just give the object that you want to mock to the `mocker` function.
After this you can start mocking individual attributes and methods as follows:

```python
# Assert that a certain method has been called exactly once
mocker(Teapot).mock("add_tea").called_once()

# Provide a return value and assert that method has been called twice
mocker(Teapot).mock("brew").return_value("mocked").called_twice()

# Assert that a method has been called with specific arguments at most twice
mocker(Teapot).mock("add_tea").all_calls_with("green").call_count_at_most(2)
```

### Spying

Spying is not any harder than mocking. You just need to call the `spy` method
instead of the `mock` method. After spying a callable, it works just like before
spying and you can start making assertions on it.

```python
# Assert that a certain method has been called at least once
mocker(Teapot).spy("add_tea").called()

# Check that a method has been called at most twice and has
# at least one call with the given argument
mocker(Teapot).spy("add_tea").any_call_with("green").call_count_at_most(2)
```

### Stubs

To create a stub object, just call `mocker` function without any arguments.

```python
# Create a stub with a method called "my_method"
stub = mocker().mock("my_method").return_value("it works!").self()
assert stub.my_method() == "it works!"

# You can give keyword arguments to the mocker function to quickly set properties
stub = mocker(my_property=10)
assert stub.my_property == 10
```

For more details and examples, see the [API reference](https://chainmock.readthedocs.io/en/latest/api_reference/).

## Similar projects

If chainmock is not what you need, check out also these cool projects:

- [flexmock](https://github.com/flexmock/flexmock): Chainmock's API is heavily
  inspired by flexmock. Flexmock doesn't use standard library unittest and it
  has fully custom mocking implementation. Compared to flexmock, chainmock has
  more familiar API if you have been using standard library unittest and
  Chainmock also supports async mocking and partial argument matching.
- [pytest-mock](https://github.com/pytest-dev/pytest-mock/): Similar to
  chainmock, pytest-mock is a wrapper for standard library unittest. However,
  pytest-mock doesn't provide any extra functionality as it just exposes
  unittest mocks directly to the user.

## Contributing

Do you like this project and want to help? If you need ideas, check out the open issues and feel free to open a new pull request. Bug reports and feature requests are also very welcome.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ollipa/chainmock",
    "name": "chainmock",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": "testing, mock, mocking, pytest",
    "author": "Olli Paakkunainen",
    "author_email": "olli@paakkunainen.fi",
    "download_url": "https://files.pythonhosted.org/packages/84/11/8befdcbdfdc5c67247e7d50d8371cdd9ac68c005d2b1fc3ade4e465892b1/chainmock-0.11.0.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\n  <img alt=\"chainmock\" src=\"https://github.com/ollipa/chainmock/assets/25169984/ad243761-b2ed-4aff-89e7-699e2b4a7e6d\">\n</p>\n\n<p align=\"center\"><strong>Chainmock</strong> <em>- Mocking library for Python and Pytest.</em></p>\n\n<p align=\"center\">\n<a href=\"https://pypi.org/project/chainmock/\">\n  <img src=\"https://img.shields.io/pypi/v/chainmock\" alt=\"pypi\">\n</a>\n<a href=\"https://github.com/ollipa/chainmock/actions/workflows/ci.yml\">\n  <img src=\"https://github.com/ollipa/chainmock/actions/workflows/ci.yml/badge.svg\" alt=\"ci\">\n</a>\n<a href=\"https://chainmock.readthedocs.io/\">\n  <img src=\"https://img.shields.io/readthedocs/chainmock\" alt=\"documentation\">\n</a>\n<a href=\"./LICENSE\">\n  <img src=\"https://img.shields.io/pypi/l/chainmock\" alt=\"license\">\n</a>\n</p>\n\n<hr>\n\n<p align=\"center\">\n<a href=\"https://chainmock.readthedocs.io/\">\n  <b>Documentation</b>\n</a>\n</p>\n\n<hr>\n\nChainmock is a mocking Library for Python and pytest. Under the hood it uses Python standard library mocks providing an alternative syntax to create mocks and assertions. Chainmock also comes with some additional features to make testing faster and more straightforward. The syntax works especially well with pytest fixtures.\n\n## Installation\n\nInstall with pip:\n\n```\npip install chainmock\n```\n\n## Features\n\nChainmock supports all the same features that Python standard library unittest\nmocks support and adds some convenient extra functionality.\n\n- **Mocking**: Create _mocks_ and assert call counts and arguments or replace\n  return values.\n- **Spying**: _Spying_ proxies the calls to the original function or method.\n  With spying you can assert call counts and arguments without mocking.\n- **Stubs**: Easily create _stub_ objects that can be used in tests as fake data\n  or to replace real objects.\n- **Async support**: Chainmock supports mocking and spying _async_ functions and\n  methods. Most of the time it also recognizes automatically when async mocking\n  should be used so it is not any harder than mocking sync code.\n- **Fully type annotated**: The whole codebase is fully type annotated so\n  Chainmock works well with static analysis tools and editor autocomplete.\n- Works with **Python 3.8+ and PyPy3**.\n- Supports `pytest`, `unittest`, and `doctest` test runners.\n\n## Examples\n\nThe entrypoint to Chainmock is the `mocker` function. Import the `mocker`\nfunction as follows:\n\n```python\nfrom chainmock import mocker\n```\n\n### Mocking\n\nTo mock you just give the object that you want to mock to the `mocker` function.\nAfter this you can start mocking individual attributes and methods as follows:\n\n```python\n# Assert that a certain method has been called exactly once\nmocker(Teapot).mock(\"add_tea\").called_once()\n\n# Provide a return value and assert that method has been called twice\nmocker(Teapot).mock(\"brew\").return_value(\"mocked\").called_twice()\n\n# Assert that a method has been called with specific arguments at most twice\nmocker(Teapot).mock(\"add_tea\").all_calls_with(\"green\").call_count_at_most(2)\n```\n\n### Spying\n\nSpying is not any harder than mocking. You just need to call the `spy` method\ninstead of the `mock` method. After spying a callable, it works just like before\nspying and you can start making assertions on it.\n\n```python\n# Assert that a certain method has been called at least once\nmocker(Teapot).spy(\"add_tea\").called()\n\n# Check that a method has been called at most twice and has\n# at least one call with the given argument\nmocker(Teapot).spy(\"add_tea\").any_call_with(\"green\").call_count_at_most(2)\n```\n\n### Stubs\n\nTo create a stub object, just call `mocker` function without any arguments.\n\n```python\n# Create a stub with a method called \"my_method\"\nstub = mocker().mock(\"my_method\").return_value(\"it works!\").self()\nassert stub.my_method() == \"it works!\"\n\n# You can give keyword arguments to the mocker function to quickly set properties\nstub = mocker(my_property=10)\nassert stub.my_property == 10\n```\n\nFor more details and examples, see the [API reference](https://chainmock.readthedocs.io/en/latest/api_reference/).\n\n## Similar projects\n\nIf chainmock is not what you need, check out also these cool projects:\n\n- [flexmock](https://github.com/flexmock/flexmock): Chainmock's API is heavily\n  inspired by flexmock. Flexmock doesn't use standard library unittest and it\n  has fully custom mocking implementation. Compared to flexmock, chainmock has\n  more familiar API if you have been using standard library unittest and\n  Chainmock also supports async mocking and partial argument matching.\n- [pytest-mock](https://github.com/pytest-dev/pytest-mock/): Similar to\n  chainmock, pytest-mock is a wrapper for standard library unittest. However,\n  pytest-mock doesn't provide any extra functionality as it just exposes\n  unittest mocks directly to the user.\n\n## Contributing\n\nDo you like this project and want to help? If you need ideas, check out the open issues and feel free to open a new pull request. Bug reports and feature requests are also very welcome.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Mocking library for Python and Pytest",
    "version": "0.11.0",
    "project_urls": {
        "Documentation": "https://chainmock.readthedocs.io",
        "Homepage": "https://github.com/ollipa/chainmock",
        "Issue Tracker": "https://github.com/ollipa/chainmock/issues",
        "Repository": "https://github.com/ollipa/chainmock"
    },
    "split_keywords": [
        "testing",
        " mock",
        " mocking",
        " pytest"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "24382e06b7b1ec9a4448e5a83f78b10653c7ab42cd98959ae300cfc36cea8769",
                "md5": "fdfea54a65d016ae68164e07a21c12a3",
                "sha256": "71bdfd8938159e2c504c2de78f9cc74aab0d39a1531c3f132b12548ef3e3e7ae"
            },
            "downloads": -1,
            "filename": "chainmock-0.11.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fdfea54a65d016ae68164e07a21c12a3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 18794,
            "upload_time": "2024-03-23T13:57:19",
            "upload_time_iso_8601": "2024-03-23T13:57:19.849165Z",
            "url": "https://files.pythonhosted.org/packages/24/38/2e06b7b1ec9a4448e5a83f78b10653c7ab42cd98959ae300cfc36cea8769/chainmock-0.11.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "84118befdcbdfdc5c67247e7d50d8371cdd9ac68c005d2b1fc3ade4e465892b1",
                "md5": "0a6587894a1819fe1788ad1f35d92965",
                "sha256": "601be8a7f33789f67a4e3ae1795bd59cca91b6a3bd00f7472619fa05a8ac4866"
            },
            "downloads": -1,
            "filename": "chainmock-0.11.0.tar.gz",
            "has_sig": false,
            "md5_digest": "0a6587894a1819fe1788ad1f35d92965",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 19239,
            "upload_time": "2024-03-23T13:57:21",
            "upload_time_iso_8601": "2024-03-23T13:57:21.348607Z",
            "url": "https://files.pythonhosted.org/packages/84/11/8befdcbdfdc5c67247e7d50d8371cdd9ac68c005d2b1fc3ade4e465892b1/chainmock-0.11.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-23 13:57:21",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ollipa",
    "github_project": "chainmock",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "chainmock"
}
        
Elapsed time: 0.22164s