kiwicom / pytest-recording

A pytest plugin that allows recording network interactions via VCR.py
MIT License
441 stars 35 forks source link

feat: "Lazy load" VCR to reduce plugin overhead #146

Closed herbierand closed 3 months ago

herbierand commented 3 months ago

Description

This PR defers importing _vcr in plugin.py until the vcr fixture is invoked with a vcr-marked test. The merge_kwargs helper has been moved to utils.py to allow importing it independently of the VCR modules required by _vcr.py. VCR object imports are now guarded using if TYPE_CHECKING.

To verify the performance improvement, I compared the results of running the same dummy test case with and without the vcr mark. I averaged results from running pytest 20 times, removing __pycache__ and .pytest_cache between runs. Here is a summary of the results:

pytest_recording version Unmarked Test Runtime @pytest.mark.vcr Runtime
0.13.1 291.47 ms 287.70 ms
This change 140.39 ms 294.15 ms

The results show a significant improvement in the runtime of unmarked tests, while the runtime of tests marked with @pytest.mark.vcr remains comparable to the previous version.

Checklist