Skip to main content

Python unittest integration for

Project description

PyPI Build Status Coverage Report Python Versions Gitter

This package provides VCRTestCase for simple integration between and Python’s venerable unittest.


Install from PyPI:

pip install vcrpy-unittest


Inherit from VCRTestCase for automatic recording and playback of HTTP interactions.

from vcr_unittest import VCRTestCase
import requests

class MyTestCase(VCRTestCase):
   def test_something(self):
       response = requests.get('')

Similar to how returns the cassette from the context manager, VCRTestCase makes the cassette available as self.cassette:

self.assertEqual(len(self.cassette), 1)
self.assertEqual(self.cassette.requests[0].uri, '')

By default cassettes will be placed in the cassettes subdirectory next to the test, named according to the test class and method. For example, the above test would read from and write to cassettes/MyTestCase.test_something.yaml

The configuration can be modified by overriding methods on your subclass: _get_vcr_kwargs, _get_cassette_library_dir and _get_cassette_name. To modify the VCR object after instantiation, for example to add a matcher, you can hook on _get_vcr, for example:

class MyTestCase(VCRTestCase):
    def _get_vcr(self, **kwargs):
        myvcr = super(MyTestCase, self)._get_vcr(**kwargs)
        myvcr.register_matcher('mymatcher', mymatcher)
        myvcr.match_on = ['mymatcher']
        return myvcr

See the source for the default implementations of these methods, and for more information.


In case inheriting from VCRTestCase is difficult because of an existing class hierarchy containing tests in the base classes, inherit from VCRMixin instead.

from vcr_unittest import VCRMixin
import requests
import unittest

class MyTestMixin(VCRMixin):
   def test_something(self):
       response = requests.get(self.url)

class MyTestCase(MyTestMixin, unittest.TestCase):
    url = ''


VCRTestCase supports a subset of the Python versions supported by Specifically Python 2.6 is excluded, because it lacks TestCase.addCleanup. Adding support for Python 2.6 would be pretty easy with tearDown but that implementation is fragile because it depends on nothing else going wrong in the inheritance chain. Rather than take on this additional complexity, Python 2.6 is simply excluded for now.


This library uses the MIT license, which is the same as See LICENSE.txt for more details.


Thanks to @kevin1024 for, and to @IvanMalison for his constructive critique on this package. Also thanks to @nedbat for his post regarding unittest and context managers, and to @davepeck for httreplay which served me well for so long.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for vcrpy-unittest, version 0.1.7
Filename, size File type Python version Upload date Hashes
Filename, size vcrpy_unittest-0.1.7-py2.py3-none-any.whl (6.8 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size vcrpy-unittest-0.1.7.tar.gz (4.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page