Skip to main content

PyPI Build Status Coverage Report Python Versions Gitter

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

Installation

Install from PyPI:

pip install vcrpy-unittest

Usage

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('http://example.com')

Similar to how VCR.py 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, 'http://example.com')

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 VCR.py for more information.

VCRMixin

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 = 'http://example.com'

Compatibility

VCRTestCase supports a subset of the Python versions supported by VCR.py. 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.

License

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

Acknowledgements

Thanks to @kevin1024 for VCR.py, 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.

Release files for vcrpy-unittest 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vcrpy-unittest 0.1.7
File Size Uploaded
vcrpy-unittest-0.1.7.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for vcrpy-unittest 0.1.7
File Interpreter ABI Platform
vcrpy_unittest-0.1.7-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 10.8 kB

Release files / vcrpy-unittest-0.1.7.tar.gz

Download URL vcrpy-unittest-0.1.7.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a508d8848e678243f1c0d4b152a645c18403a8da517b8a44dfcfc35a675ec981
BLAKE2b-256 checksum
How to use checksums
7ccb432020e833be2e9c21578a1b2c9e704662c191e02a8cafb7c0761207451d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.9.1 pkginfo/1.3.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.19.6 CPython/3.6.6

Release files / vcrpy_unittest-0.1.7-py2.py3-none-any.whl

Download URL vcrpy_unittest-0.1.7-py2.py3-none-any.whl
Size 6.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b5e2d78bf4c13bc04dca04061dda934abba5f2c9d5201f148747800ff80a762b
BLAKE2b-256 checksum
How to use checksums
261fda7aa0b470f83fd9c7aa1c47f5ebffc63b8ef4018e2da3c0b10a3bcf7521
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.9.1 pkginfo/1.3.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.19.6 CPython/3.6.6

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page