This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Python unittest integration for vcr.py

Project Description

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 History

Release History

This version
History Node

0.1.6

History Node

0.1.5

History Node

0.1.4

History Node

0.1.3

History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
vcrpy-unittest-0.1.6.tar.gz (3.9 kB) Copy SHA256 Checksum SHA256 Source Jul 7, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting