question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Write multiple tests to the same cassette

See original GitHub issue

Is it possible to write multiple tests to the same cassette? If multiple tests are making the same API calls, it’s inefficient to record them again and again, better to use a shared cassette.

This is possible with vcr.use_cassette, but doesn’t seem to be possible with pytest.mark.vcr, since the default cassette name is always the test name.

For example:

@pytest.mark.vcr("tests/cassettes/shared_cassette.yaml")
def test_a(): ...

@pytest.mark.vcr("tests/cassettes/shared_cassette.yaml")
def test_b(): ...

In this example, you will still write new cassettes to tests/cassettes/test_a.yaml and tests/cassettes/test_b.yaml, rather than using the shared one.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
DevilXDcommented, May 6, 2020

I think it’d be just fine to make it always write to the first cassette specified, while reading from all specified. If someone would need more control than that instead, I suggest making it possible to specify cassette fixtures as test arguments, roughly like pytest currently does with parametrization.

Reference: https://docs.pytest.org/en/latest/example/parametrize.html#parametrizing-conditional-raising (follow the link there for argument inputs types too)

Example:

@pytest.mark.vcr(gitlab_cassette="gitlab.yaml", slack_cassette="slack.yaml")
def test_apis(gitlab_cassette, slack_cassette):  # specifying more than one cassette kwarg enforces having to use the context manager yourself
    with gitlab_cassette:  # specify exactly what cassette you want to use here
        requests.get(...)
    with slack_cassette:
        requests.post(...)

This would also allow on custom operations being done on a cassette, such as using .rewind() - I’m not aware of an easy possibility of being able to do that right now.

Other possible usages:

@pytest.mark.vcr("main.yaml")  # just the cassette name, no kwarg
def test_apis(cassette):  # default fixture name giving access to the cassette itself
    # not specifying the cassette name as a kwarg means the entire test is implicatively wrapped just like before
    requests.get(...)
    cassette.rewind()  # possible given the cassette itself now
    requests.get(...)
@pytest.mark.vcr(my_cassette="main.yaml")  # explicit kwarg
def test_apis(my_cassette):  # fixture name matches the kwarg
    with my_cassette:  # explicit kwarg enforces context manager usage (just like in the above case with two of those)
        requests.get(...)
    my_cassette.rewind()  # possible given the cassette itself now
    with my_cassette:
        requests.post(...)
0reactions
Diaoulcommented, Jul 11, 2020

So here is my use case as it is slightly different from what this ticket was originally about: I want to isolate some recordings done in a fixture in a specific cassette so it is not recorded for every test running the fixture.

Example: a fixture that provides a logged in client for a specific API to other tests

# this I want in a specific cassette
@pytest.fixture(scope="module")
async def client():
    async with SomeClient() as client:
        await client.login("username", "password")
        yield client


# this can be on it's own cassette, without the login part
@pytest.mark.vcr
@pytest.mark.asyncio
async def test_complex_operation(client):
    response = await client.complex_operation()

    assert response.status_code == 200
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using multiple cassettes with VCR in one test - Stack Overflow
Try using VCR.insert_cassette which does not require a block. You'll need to call eject_cassette in your tear downs. I tried it and it...
Read more >
Multiple requests in one cassette - Google Groups
Hello, I'm writing a test for my subscription UI. The process includes 2 requests, one for authorization and one for charging the card....
Read more >
Introduction to unit testing with tape, the basics
The purpose of a unit test is to validate that the code unit performs as expected by executing it with crafted inputs and...
Read more >
tape - Testling
This guide covers the ins-and-outs of tape, a simple TAP-producing test library for node and browsers. For an exaustive list of all the...
Read more >
Existing VCR cassette requests being re-recorded ... - GitHub
We can commit these appended cassettes all day; but the mere fact is, ... My guess is that there is something else that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found