Comment 4 for bug 1687039

Revision history for this message
Jamie Lennox (jamielennox) wrote :

Thanks, it's always nice to here that people are using your stuff (even the problems).

So my concern is primarily between the interaction of the two mockers. I can understand the desire to isolate certain tests/behaviours/components, i'm still a little unsure from an intuitive perspective what a test author would expect in the scenario when they have many mockers.

So two possible solutions, one existing one new:

Existing: The way I have (and therefore requests-mock has) enforced that a behaviour has actually occurred is by examining history when the operation is complete. What this means is that in a test setup you can create hundreds of mocks that may never get called, and then after the test you look at request_history or any of the helpers to make sure that things happened in the order you expect.

I've always preferred this way because you can have giant fixture style lists of responses that you can just register and never use rather than have to tweak per test. The mock style vs mox.

When you create a mock like:

m = self.requests_mock.get('https://test.com/path')

that m is a useable object and has options like:

m.request_history
m.last_request
m.call_count

and all the other things that self.requests_mock has. So typically to make sure that the specific thing i'm testing occurred and not all the setup is to do assertions like:

self.assertTrue(m.called_once)

rather that have to inspect the full:

self.requests_mock.request_history

It doesn't give you isolation obviously because that mock is still there for later - but IMO you have a reasonable certainty from that that your test did the right thing, because otherwise it would fail.

New one:

I'm going to need to think about this a fair bit more, but what about a context manager within the mocker, like:

with self.requests_mock.new() as mocker:
    mocker.get('http://test.com', json={})

(new() is a terrible name). This is mentally what i feel like you're asking for and you can interrogate mocker independently of self.requests_mock. But I'm torn on what it provides.

So that route would only be available for the lifetime of mocker, but I think that any requests should probably still get added to self.requests_mock.request_history. You ensure that nothing after has access to that route - but you're pretty sure that's not happening anyway, you'd see it in call_count or something. Also mocks get evaluated in a last declared first fashion so if you're trying to override an existing address you can already do that.

I haven't looked yet as to how hard that would be to implement, I suspect not as easy as you'd think. But am I on the right track with the functionality?