Skip to content

obspy/vcr

Repository files navigation

vcr - decorator for capturing and simulating network communication

TravisCI Status AppVeyor Status PyPI License Gitter

Any Python socket communication in unittests (decorated with the @vcr) and/or doctests (containing a +VCR) will be recorded on the first run and saved into a special 'vcrtapes' directory as single pickled file for each test case. Future test runs will reuse those recorded network session allowing for faster tests without any network connection. In order to create a new recording one just needs to remove/rename the pickled session file(s).

So pretty similar to VCR.py but on socket level instead of HTTP/HTTPS level only - so therefore it should be more powerful ...

Inspired by:

Why was it initiated?

Network tests tend to fail sporadically, need usually a very long time (compared to other tests) and (surprise!) require a network connection (if not mocked). This module tackles all three issues mentioned above.

Installation

Install from GitHub:

pip install -U https://github.com/obspy/vcr/archive/master.zip

Usage

Just decorate your unit tests with @vcr:

import unittest

import requests
from vcr import vcr


class MyTestCase(unittest.TestCase):

   @vcr
   def test_something(self):
       response = requests.get('http://example.com')
       self.assertEqual(response.status_code, 200)

   @vcr(debug=True, overwrite=True, tape_file='python.vcr')
   def test_something_else(self):
       response = requests.get('http://python.org')
       self.assertEqual(response.status_code, 200)

VCR functionality within doctests requires currently a monkey patch and the +VCR keyword somewhere within the doctest as shown in test_doctest.py.

License

This library uses the LGPLv3 license. See LICENSE.txt for more details.