Comment 2 for bug 1687039

Revision history for this message
David Chudzicki (dchudz) wrote :

I actually did something very similar to your version initially! In our case we're explicitly calling the __enter__ and __exit__ in the setUp and tearDown methods, not using a fixture, but the effect is similar. I had a pull request adding some tests which add mocks to the same mocker the way your example does, and a colleague objected to it in code review, b/c my equivalent of "self.requests_mock.get('https://test.com/path', json={})" isn't called inside a context manager.

Of course the mock will be cleaned up between tests. But my colleague wanted to see my test's mock in an explicit context manager. The explicit context manager inside the test is nice because:

(a) that's the syntax I'm used to (but it breaks things given that's we're already setting up mocks in the setup) and
(b) especially if the test needed to use a couple different mocks, using two different context manager calls to make sure they didn't interfere with each other would be good.