rmoq - a request mock cachePosted 30.12.2014 · 3 min read
While working on feedhuddler.com there are tests that test the feedparsing functionality. It uses the actual feeds from their origins, which I think is ok for this situation. If the feeds are not longer parseable by feedhuddler the continous integration will fail and I can look into it. It could be managed by some downloaded feeds and mocking of the requests. However, I wouldn't feel comfortable by putting other peoples content in the feedhuddler repo. Furthermore, it should be easy to add a feed that is reported to break the parser without much work. The tests can take a while on the continous integration server without it beeing a big deal, but locally the time consuming tests makes it cumbersome to work test driven or to work with the tests at all.
rmoq was created to work around that problem. It listen on all requests made by the requests framework and intercepts them. If a request is already been cached in the cache folder it creates a response based on a local file. On the other hand, if no file exists for that url it will perform the request and save the content into a file and then return the response. Thus, if the files are already there the tests will run faster and no manual work is needed to create the local files.
A nice side-effect of using files as storage is that they can be added to the version control repo. In the case of the feedhuddler tests it is not wanted behaviour, but in other cases it might be just what you want to add these files to code repository.
The code can be found on github.com/relekang/rmoq and it can be installed with pip:
pip install rmoq
The basic way to use it is just to add a decorator to your test:
@rmoq.activate('test_fixtures')def test_remote_call(): response = requests.get('http://example.com') assert response.body == 'Example'
The path parameter sent in above is optional, and if left out the path will then be set to
fixtures/. The test function is then wrapped in a with statement that intercepts all requests and either load them from file or fetches the content from the url and saves it to a file before returning the response.
The feedhuddler tests went from
Ran 21 tests in 41.394s to
Ran 21 tests in 1.942s which is quite a speedup and the tests suite is much more pleasant to work on.