VCR
Provides Mixins and TestCases for snapshot testing outgoing HTTP requests with VCR.
Depends on vcrpy and requests, but is also usable with other HTTP libraries than requests.
Note
The sub-dependencies will come automatically with the vcr extras:
maykin-common[vcr]. Because this is a test tool, only include it in your CI
and dev dependencies. Don’t include the vcr extra in the base dependencies
of your project.
- class maykin_common.vcr.SimpleVCRTestCase(*args: Any, **kwargs: Any)
A Django
SimpleTestCasewith theVCRMixinand avcrtag.Use this for testing “client code”.
- class maykin_common.vcr.TransactionVCRTestCase(*args: Any, **kwargs: Any)
A Django
TestCasewith theVCRMixinand avcrtag.Use this only if you really need it. i.e when you’re actually testing the transactional behaviour of your app.
- class maykin_common.vcr.VCRMixin(*args: Any, **kwargs: Any)
Mixin to use VCR cassettes to record HTTP requests/responses.
- VCR_FILTER_HEADERS: ClassVar[Collection[str]] = frozenset({'Authorization', 'x-api-key'})
Default set of request headers to filter out.
Passed to the
filter_headerskwarg of VCR by default.
- VCR_RECORD_MODE: vcr.config.RecordMode
Defaults to VCR_RECORD_MODE env variable or RecordMode.NONE. To (re-)record throw away the cassettes and set to RecordMode.ONCE
- VCR_TEST_FILES: Path | None = None
Cassettes will be stored in
VCR_TEST_FILES/vcr_cassettes/{test class name}/{test method name}.yamlIf left None, a
filesdirectory at the same level as the test class file will be used.
- vcr_enabled: bool
Easy toggle to temporarily turn vcr off during development or debugging. So True does not enable recording “episodes” that would otherwise not be recorded.
- vcr_raises(exception: Callable[[], Exception]=<class 'requests.exceptions.RequestException'>) AbstractContextManager[vcr.cassette.Cassette]
Simulate occurrence of an error during HTTP request.
Example:
from requests.exceptions import SSLError, Timeout # sometimes people let certificates expire with self.vcr_raises(SSLError): response = function_under_test_that_uses_requests() # or services/connections are down with self.vcr_raises(Timeout): response = function_under_test_that_uses_requests()
Note
Instead of performing and recording a request, this raises an exception. So there will be no request nor cassette!
- class maykin_common.vcr.VCRTestCase(*args: Any, **kwargs: Any)
A Django
TestCasewith theVCRMixinand avcrtag.Use this if your Model may do HTTP requests.