Giter Site home page Giter Site logo

vcrpy-unittest's Introduction

vcrpy-unittest

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.

If you implement a setUp method on your test class then make sure to call the parent version super().setUp() in your own in order to continue getting the cassettes produced.

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'

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.

vcrpy-unittest's People

Contributors

agriffis avatar antonofthewoods avatar dan-passaro avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.