2

vcrpy is the python record/play package, below is the common way from the guideline

class TestCloudAPI(unittest.TestCase):
    def test_get_api_token(self):
        with vcr.use_cassette('fixtures/vcr_cassettes/test_get_api_token.yaml'): 
            # real request and testing

    def test_container_lifecycle(self):
        with vcr.use_cassette('fixtures/vcr_cassettes/test_container_lifecycle.yaml'):

I want to have different record files, so I have to repeat this in every method.

Is it possible to have one line somewhere to simplify this like:

TEST_CASE_VCR(USE_METHOD_AS_FILENAME)
class TestCloudAPI(unittest.TestCase):
    def test_get_api_token(self):
        # real request and testing
    def test_container_lifecycle(self):
Larry Cai
  • 55,923
  • 34
  • 110
  • 156

3 Answers3

2

This is now supported in newer versions of vcrpy by omitting the cassette name altogether. From the documentation:

VCR.py now allows the omission of the path argument to the use_cassette function. Both of the following are now legal/should work

@my_vcr.use_cassette
def my_test_function():
...

In both cases, VCR.py will use a path that is generated from the provided test function’s name. If no cassette_library_dir has been set, the cassette will be in a file with the name of the test function in directory of the file in which the test function is declared. If a cassette_library_dir has been set, the cassette will appear in that directory in a file with the name of the decorated function.

It is possible to control the path produced by the automatic naming machinery by customizing the path_transformer and func_path_generator vcr variables

Anuj Kumar
  • 130
  • 1
  • 9
0

There isn't a feature to do this currently built in to VCR, but you can make your own. Check out the decorator that Venmo created.

  • Could you add a few relevant details from the article to your answer? Link only answers are frowned upon since there is a risk the link will become unavailable, which would make the answer no longer valid. – jpmc26 May 12 '14 at 19:15
0

This gets a lot easier with vcrpy-unittest which is--as you might guess--integration between vcrpy and unittest.

Your example becomes this:

from vcr_unittest import VCRTestCase

class TestCloudAPI(VCRTestCase):
    def test_get_api_token(self):
        # real request and testing

    def test_container_lifecycle(self):
        # real request and testing

and the cassettes are automatically named according to the test and saved in a cassettes dir alongside the test file. For example, this would create two files: cassettes/TestCloudAPI.test_get_api_token.yaml and cassettes/TestCloudAPI.test_container_lifecycle.yaml.

The directory and naming can be customized by overriding a couple methods: _get_cassette_library_dir and _get_cassette_name but it's probably not necessary.

vcrpy-unittest is on github at https://github.com/agriffis/vcrpy-unittest and PyPI at https://pypi.python.org/pypi/vcrpy-unittest

Aron Griffis
  • 1,699
  • 12
  • 19