mock.Mock()
is great. It's the reason that I use Python for small side projects - it makes unit testing a breeze. But over-reliance on mock objects for testing can obscure the meaning of your test (at best), and (at worst) it can create bugs in your test code.
- Use
spec_set
when creating object fakes. - Use
autospec=True
when stubbing method functionality.
Similar to mock.Mock()
, Pytest fixtures (functions defined with the decorator @pytest.fixture()
) can be super handy for writing unit tests, but if they are overused, they can obscure the meaning of what you're testing.