Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# pytest-mock

This pytest plugin provides a `mocker` fixture which is a thin-wrapper around the patching API provided by the mock package:

    import os

    class UnixFS:

        @staticmethod
        def rm(filename):
            os.remove(filename)

    def test_unix_fs(mocker):
        mocker.patch('os.remove')
        UnixFS.rm('file')
        os.remove.assert_called_once_with('file')

Besides undoing the mocking automatically after the end of the test, it also provides other nice utilities such as `spy` and `stub`, and uses pytest introspection when comparing calls.

# Install

Install using pip:

    $ pip install pytest-mock

Contents:

- Usage
  - Spy
  - Stub
  - Usage as context manager
  - Where to patch
- Configuration
  - Use standalone “mock” package
  - Improved reporting of mock call assertion errors
- Remarks
  - Type annotations
- Contributing
- About
  - Tidelift
  - License
  - Security contact information
- Changelog
  - Releases