6from vcr.cassette import Cassette
8from cassettes import get_cassette_name
11def test_creates_cassette(cassette_name: str) -> None:
13 The filename of a VCR cassette is the test name, plus a YAML extension.
15 assert cassette_name == "test_creates_cassette.yml"
18@pytest.mark.parametrize(
19 ["expected_cassette_name"],
21 pytest.param("test_creates_parametrized_cassette[test1].yml", id="test1"),
22 pytest.param("test_creates_parametrized_cassette[test2].yml", id="test2"),
23 pytest.param("test_creates_parametrized_cassette[test3].yml", id="test3"),
26def test_creates_parametrized_cassette(
27 cassette_name: str, expected_cassette_name: str
30 In a parametrized test, the filename of the VCR cassette includes
31 the test case name in square brackets.
33 assert cassette_name == expected_cassette_name
36class TestCassetteNameInClass:
38 TestCassetteNameInClass exists so the next test can run inside
39 a class, simulating how tests are often organised in "real" test suites.
42 def test_prefixes_class_name_to_cassette(self, cassette_name: str) -> None:
44 In a test in a class, the filename of the VCR cassette includes
45 the class name as a prefix.
49 == "TestCassetteNameInClass.test_prefixes_class_name_to_cassette.yml"
52 @pytest.mark.parametrize(
53 ["expected_cassette_name"],
56 "TestCassetteNameInClass.test_prefixes_name_with_parametrized_cassette[test1].yml",
60 "TestCassetteNameInClass.test_prefixes_name_with_parametrized_cassette[test2].yml",
64 "TestCassetteNameInClass.test_prefixes_name_with_parametrized_cassette[test3].yml",
68 "TestCassetteNameInClass.test_prefixes_name_with_parametrized_cassette[test.name.with.periods].yml",
69 id="test.name.with.periods",
73 def test_prefixes_name_with_parametrized_cassette(
74 self, cassette_name: str, expected_cassette_name: str
77 In a parametrized test in a class, the filename of the
78 VCR cassette includes the class name as a prefix and the
79 test case name as a suffix.
81 assert cassette_name == expected_cassette_name
84@pytest.mark.parametrize("url", ["https://example.com"])
85def test_throws_if_bad_cassette_name(url: str, request: pytest.FixtureRequest) -> None:
87 Trying to use a URL as a VCR cassette name throws a ``ValueError``.
89 with pytest.raises(ValueError, match="Illegal characters in VCR cassette name"):
90 get_cassette_name(request)
93def test_creates_cassette_in_fixture_dir(vcr_cassette: Cassette) -> None:
95 The VCR cassette is created in the ``tests/fixtures/cassettes`` directory.
99 == "tests/fixtures/cassettes/test_creates_cassette_in_fixture_dir.yml"