diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3469ffb..3bc1356 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,11 @@ Changelog ========= +2.1.0 +----- + +- add generic VCR function to help with test implementation + 2.0.3 ----- diff --git a/cassettedeck/deck.py b/cassettedeck/deck.py index 4549e37..eada897 100644 --- a/cassettedeck/deck.py +++ b/cassettedeck/deck.py @@ -7,8 +7,6 @@ class CassetteDeck: - """This is the object to use as a fixture in tests. - """ def __init__(self, cassette_library_dir=None, ignore_localhost=False, ignore_hosts=(), mode='once', mocked_services=None): diff --git a/cassettedeck/tests/test_vcr.py b/cassettedeck/tests/test_vcr.py new file mode 100644 index 0000000..33a6422 --- /dev/null +++ b/cassettedeck/tests/test_vcr.py @@ -0,0 +1,20 @@ +import os + +import aiohttp +from aiounittest import AsyncTestCase + +from cassettedeck.vcr import vcr + + +class TestVCR(AsyncTestCase): + + async def test_should_store_cassette_at_expected_location(self): + expected_path = os.path.join(os.path.dirname(__file__), "cassettes", f"{self.id()}.yaml") + self.assertFalse(os.path.exists(expected_path)) + + with vcr(self): + async with aiohttp.request("GET", "https://example.com") as response: + response.raise_for_status() + + self.assertTrue(os.path.isfile(expected_path)) + os.remove(expected_path) diff --git a/cassettedeck/vcr.py b/cassettedeck/vcr.py new file mode 100644 index 0000000..447aa94 --- /dev/null +++ b/cassettedeck/vcr.py @@ -0,0 +1,18 @@ +import inspect +import os +from contextlib import contextmanager +from unittest import TestCase + +from cassettedeck import CassetteDeck + + +@contextmanager +def vcr(test_case: TestCase): + """ + Test utility which allows cassettes to be stored with a unique name near their point of use. + """ + test_file = inspect.getfile(type(test_case)) + library = os.path.join(os.path.dirname(test_file), "cassettes") + my_vcr = CassetteDeck(cassette_library_dir=library, ignore_localhost=True) + with my_vcr.use_cassette(f"{test_case.id()}.yaml", mode="once") as cassette: + yield cassette diff --git a/setup.py b/setup.py index 988b9ee..43c047f 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name='cassettedeck', - version='2.0.3', + version='2.1.0', description='A library store and replay aiohttp requests', long_description='To simplify and speed up tests that make HTTP requests', author='Developer team at Onna Technologies', @@ -25,6 +25,7 @@ packages=find_packages(), install_requires=[ 'aiohttp', + "aiounittest", 'vcrpy==1.12.0', 'pyyaml==4.2b4', ]