pytest-mockito

raw JSON →
0.0.6.post1 verified Fri May 01 auth: no python

Base fixtures for using Mockito mocks with pytest. Integrates the Mockito library (a spy/mock framework) into pytest by providing fixtures that handle setup and teardown of mocks. Current version is 0.0.6.post1, with a slow release cadence.

pip install pytest-mockito
error NameError: name 'mocker' is not defined
cause Attempting to import mocker directly from the package instead of using it as a fixture parameter.
fix
Change your test function to accept mocker as an argument: def test_foo(mocker):
error pytest.PytestUnknownMarkWarning: Unknown pytest.mark.mocker
cause Using @pytest.mark.mocker decorator which does not exist; the fixture is only available as a function parameter.
fix
Remove the decorator; just add mocker as a parameter to the test function.
gotcha The mocker fixture must be used in every test that uses Mockito mocks; otherwise, mock state leaks between tests.
fix Add mocker as a parameter to your test function: def test_foo(mocker):
gotcha Do not import mocker from pytest-mockito; it is a pytest fixture that must be injected by name.
fix Use def test(mocker): ... and let pytest inject the fixture.
deprecated The package has seen no recent updates (last release 0.0.6.post1). It may rely on older versions of mockito. Check compatibility if using mockito 1.4+.
fix Pin mockito to version compatible with this plugin, or consider pytest-mock or unittest.mock.

Basic test using the mocker fixture to reset mockito state automatically.

import pytest
from mockito import when, verify

def test_example(mocker):
    obj = {'foo': 'bar'}
    when(obj).__getitem__('foo').thenReturn('mocked')
    assert obj['foo'] == 'mocked'
    verify(obj).__getitem__('foo')