Skip to content

Commit

Permalink
Improve tests, clean up test code, and sort test functions/classes
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Dec 26, 2022
1 parent 94ed258 commit a0ed885
Show file tree
Hide file tree
Showing 7 changed files with 1,536 additions and 1,546 deletions.
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def _get_path(name):

def pytest_configure(config):
pytest.placeholders = Placeholders(placeholders)
config.addinivalue_line(
"markers", "add_placeholder: Define an additional placeholder for the cassette."
)
config.addinivalue_line(
"markers", "cassette_name: Name of cassette to use for test."
)
Expand Down
51 changes: 25 additions & 26 deletions tests/integration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,35 @@
class IntegrationTest:
"""Base class for PRAW integration tests."""

@pytest.fixture(autouse=True, scope="session")
def cassette_tracker(self):
"""Track cassettes to ensure unused cassettes are not uploaded."""
global existing_cassettes
for cassette in os.listdir(CASSETTES_PATH):
if cassette.endswith(".json"):
existing_cassettes.add(cassette[:-5])
yield
unused_cassettes = existing_cassettes - used_cassettes
if unused_cassettes and os.getenv("ENSURE_NO_UNUSED_CASSETTES", "0") == "1":
raise AssertionError(
f"The following cassettes are unused: {', '.join(unused_cassettes)}."
)

@pytest.fixture(autouse=True)
def cassette(self, request, recorder, cassette_name):
"""Wrap a test in a VCR.py cassette"""
"""Wrap a test in a Betamax cassette."""
global used_cassettes
kwargs = {}
for marker in request.node.iter_markers("add_placeholder"):
for key, value in marker.kwargs.items():
recorder.config.default_cassette_options["placeholders"].append(
{"placeholder": f"<{key.upper()}>", "replace": value}
)
for marker in request.node.iter_markers("recorder_kwargs"):
if marker is not None:
append_placeholders = marker.kwargs.pop("append_placeholders", None)
if append_placeholders is not None:
recorder.config.default_cassette_options["placeholders"].append(
append_placeholders
)
for key, value in marker.kwargs.items():
# Don't overwrite existing values since function markers are
# provided before class markers.
kwargs.setdefault(key, value)
for key, value in marker.kwargs.items():
# Don't overwrite existing values since function markers are provided
# before class markers.
kwargs.setdefault(key, value)
with recorder.use_cassette(cassette_name, **kwargs) as recorder:
cassette = recorder.current_cassette
if cassette.is_recording():
Expand All @@ -56,7 +69,7 @@ def read_only(self, reddit):

@pytest.fixture(autouse=True)
def recorder(self):
"""Configure VCR instance."""
"""Configure Betamax."""
session = requests.Session()
recorder = betamax.Betamax(session)
recorder.register_serializer(PrettyJSONSerializer)
Expand All @@ -72,20 +85,6 @@ def recorder(self):
# since placeholders persist between tests
Cassette.default_cassette_options["placeholders"] = []

@pytest.fixture(scope="session")
def cassette_tracker(self):
"""Return a dictionary to track cassettes."""
global existing_cassettes
for cassette in os.listdir(CASSETTES_PATH):
if cassette.endswith(".json"):
existing_cassettes.add(cassette.replace(".json", ""))
yield
unused_cassettes = existing_cassettes - used_cassettes
if unused_cassettes and os.getenv("ENSURE_NO_UNUSED_CASSETTES", "0") == "1":
raise AssertionError(
f"The following cassettes are unused: {', '.join(unused_cassettes)}."
)

@pytest.fixture
def cassette_name(self, request):
"""Return the name of the cassette to use."""
Expand Down

0 comments on commit a0ed885

Please sign in to comment.