Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Document how to test Requests without going over the wire #1880

Open
kevinburke opened this Issue Jan 25, 2014 · 4 comments

Comments

Projects
None yet
4 participants
Contributor

kevinburke commented Jan 25, 2014

If I am writing unit tests that exercise HTTP requests, it may be useful to mock or stub the external service call, so that the unit tests don't go over the network and introduce latency or fail on a train or a plane.

It would be useful if Requests provided best practice documentation for:

  • how to intercept the request before it goes over the wire, and return a custom response
  • how to determine what Requests was actually ready to send over the wire (potentially by mocking urllib3.connectionpool.HTTP(S)Connection.urlopen?)

Betamax may be appropriate here but it would also be good to explain how to do this without introducing dependencies.

Owner

sigmavirus24 commented Jan 25, 2014

Unless you're on 3.3+ mock is an external dependency so your last sentence is quite wrong. Frankly, if you're performing any parsing of the response and the service is subject to change, e.g. the GitHub API, then mocking is error prone and an awful idea. That aside I still use mocking but not to stub out responses in tests.

Owner

sigmavirus24 commented Jan 25, 2014

Also regarding:

it may be useful to mock or stub the external service call, so that the unit tests don't go over the network and introduce latency or fail on a train or a plane

You can run HTTPBin locally and tell the tests to use that with an environment variable.

I agree in principle though that by default it shouldn't be talking to the network though. I just thought I'd point out a separate alternative.

@sigmavirus24 sigmavirus24 self-assigned this Oct 10, 2014

lorien commented Mar 8, 2015

I've written this package to test my network libraries: https://github.com/lorien/test_server
It is simple HTTP server based on tornado. It is running in separate python thread. You can configure response (headers, cookies, response body) for any request that your HTTP client sends to the server. Also you have access to details of request that the server received from HTTP client.
You can check out examples of tests here:

It took me a bit to figure this out too by looking through a lot of different tools while developing my own HTTP mock service framework (https://github.com/BenjamenMeyer/stackInABox) for testing.

So I do agree it could be useful to document how to use the Adapters to intercept stuff this way for testing. They are documented (http://docs.python-requests.org/en/latest/api/#requests.adapters.HTTPAdapter) but having a couple complete examples specific to how tests function would probably be a good thing.

FWIW...💰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment