1. Error for “fixture ‘mocker’ not found”
After running pytest, it reported:
E fixture 'mocker' not found > available fixtures: cache, capfd, capsys, doctest_namespace, mock, mocker, monkeypatch, pytestconfig, record_xml_property, recwarn, request, requests_get, tmpdir, tmpdir_factory > use 'pytest --fixtures [testpath]' for help on them.
The solution is just installing the missing pip package:
pip install pytest-mock
2. How to make sure a function has been called without caring about its arguments?
There are two methods. The first method is using “.called”
... sender.send_email() mocker.patch.object(EmailSender, "_email_spec") assert sender._email_spec.called
The second method is using “mocker.spy()”
... spy = mocker.spy(sender, "_email_spec") sender.send_email() spy.assert_called()