mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 09:35:34 +00:00
115 lines
4.6 KiB
Markdown
115 lines
4.6 KiB
Markdown
#VCR.py
|
|
|
|
This is a Python version of [Ruby's VCR library](https://github.com/myronmarston/vcr).
|
|
|
|
[](http://travis-ci.org/kevin1024/vcrpy)
|
|
|
|
##What it does
|
|
Simplify testing by recording all HTTP interactions and saving them to
|
|
"cassette" files, which are yaml files containing the contents of your
|
|
requests and responses. Then when you run your tests again, they all
|
|
just hit the text files instead of the internet. This speeds up
|
|
your tests and lets you work offline.
|
|
|
|
If the server you are testing against ever changes its API, all you need
|
|
to do is delete your existing cassette files, and run your tests again.
|
|
All of the mocked responses will be updated with the new API.
|
|
|
|
##Compatibility Notes
|
|
This should work with Python 2.6 and 2.7, and [pypy](http://pypy.org).
|
|
|
|
Currently I've only tested this with urllib2, urllib3, and requests. It's known to *NOT WORK* with urllib.
|
|
|
|
##How to use it
|
|
```python
|
|
import vcr
|
|
import urllib2
|
|
|
|
with vcr.use_cassette('fixtures/vcr_cassettes/synopsis.yaml'):
|
|
response = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
|
|
assert 'Example domains' in response
|
|
```
|
|
|
|
Run this test once, and VCR.py will record the http request to
|
|
`fixtures/vcr_cassettes/synopsis.yml`. Run it again, and VCR.py will replay the
|
|
response from iana.org when the http request is made. This test is now fast (no
|
|
real HTTP requests are made anymore), deterministic (the test will continue to
|
|
pass, even if you are offline, or iana.org goes down for maintenance) and
|
|
accurate (the response will contain the same headers and body you get from a
|
|
real request).
|
|
|
|
## Advanced Features
|
|
|
|
If you want, VCR.py can return information about the cassette it is
|
|
using to record your requests and responses. This will let you record
|
|
your requests and responses and make assertions on them, to make sure
|
|
that your code under test is generating the expected requests and
|
|
responses. This feature is not present in Ruby's VCR, but I think it is
|
|
a nice addition. Here's an example:
|
|
|
|
```python
|
|
import vcr
|
|
import urllib2
|
|
|
|
with vcr.use_cassette('fixtures/vcr_cassettes/synopsis.yaml') as cass:
|
|
response = urllib2.urlopen('http://www.zombo.com/').read()
|
|
# cass should have 1 request inside it
|
|
assert len(cass) == 1
|
|
# the request host should be zombo.com
|
|
assert cass.requests.keys()[0].host == 'zombo.com'
|
|
```
|
|
|
|
The Cassette object exposes the following properties which I consider
|
|
part of the API. The fields are as follows:
|
|
|
|
* `requests`: A list of vcr.Request objects containing the requests made
|
|
while this cassette was being used, ordered by the order that the
|
|
request was made.
|
|
* `responses`: A list of the responses made.
|
|
* `play_count`: The number of times this cassette has had a response
|
|
played back
|
|
* `play_counts`: A collections.Counter showing the number of times each
|
|
response has been played back, indexed by the request
|
|
* `response_of(request)`: Access the response for a given request.
|
|
|
|
|
|
|
|
##Installation
|
|
|
|
I finally uploaded VCR.py to pypi! So you can just `pip install vcrpy` (first you may need to `brew install libyaml` [[Homebrew](http://mxcl.github.com/homebrew/)])
|
|
|
|
##Ruby VCR compatibility
|
|
I'm not trying to match the format of the YAML files. Cassettes generated by
|
|
Ruby's VCR are not compatible with VCR.py. The API is similar but VCR.py
|
|
doesn't have nearly as many features.
|
|
|
|
##Known Issues
|
|
This library is a work in progress, so the API might change on you.
|
|
There are probably some [bugs](https://github.com/kevin1024/vcrpy/issues?labels=bug&page=1&state=open) floating around too.
|
|
|
|
##Changelog
|
|
* 0.1.0: *backwards incompatible release - delete your old cassette files*:
|
|
This release adds the ability to access the cassette to make assertions
|
|
on it, as well as a major code refactor thanks to @dlecocq. It also
|
|
fixes a couple longstanding bugs with redirects and HTTPS. [#3 and #4]
|
|
* 0.0.4: If you have libyaml installed, vcrpy will use the c bindings
|
|
instead. Speed up your tests! Thanks @dlecocq
|
|
* 0.0.3: Add support for requests 1.2.3. Support for older versions of requests dropped (thanks @vitormazzi and @bryanhelmig)
|
|
* 0.0.2: Add support for requests / urllib3
|
|
* 0.0.1: Initial Release
|
|
|
|
##Similar libraries in Python
|
|
Neither of these really implement the API I want, but I have cribbed some code
|
|
from them.
|
|
* https://github.com/bbangert/Dalton
|
|
* https://github.com/storborg/replaylib
|
|
|
|
These were created after I created VCR.py but do something similar:
|
|
|
|
* https://github.com/gabrielfalcao/HTTPretty
|
|
* https://github.com/kanzure/python-requestions
|
|
* https://github.com/uber/cassette
|
|
|
|
#License
|
|
This library uses the MIT license
|