Skip to content

Commit

Permalink
test(approval): add custom Namer class
Browse files Browse the repository at this point in the history
add custom Namer class to make paramatrized tests work
  • Loading branch information
matthiasschaub committed May 23, 2024
1 parent 1259ed3 commit 56723c2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions tests/integration/test_approval.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from pathlib import Path

import pytest
from approvaltests import Options, verify_binary

from tests.comparator import GeoJSONComparator
from tests.namer import PytestNamer
from tests.reporter import SketchMapToolReporter

fixtures = Path(__file__).parent / "fixtures"


@pytest.mark.use_fixtures("vector")
@pytest.fixture(scope="session")
Expand All @@ -26,6 +23,7 @@ def test_smt_approver(sketch_map_marked_path, vector_path):
Options()
.with_reporter(SketchMapToolReporter(sketch_map=sketch_map_marked_path))
.with_comparator(GeoJSONComparator())
.with_namer(PytestNamer())
)
with open(vector_path, "rb") as f:
verify_binary(f.read(), ".geojson", options=options)
40 changes: 40 additions & 0 deletions tests/namer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os

from approvaltests import Namer

from tests import FIXTURE_DIR

APPROVED_DIR = FIXTURE_DIR / "approved"


class PytestNamer(Namer):
def __init__(self):
"""An approval tests Namer for naming approved and received text files.
The Namer includes fixture dir, module, class and function in name.
This class utilizes the `PYTEST_CURRENT_TEST` environment variable, which
consist of the nodeid and the current stage:
`relative/path/to/test_file.py::TestClass::test_func[a] (call)`
For better readability this class formats the filename to something like:
`test_file-TestClass-test_func-a
"""
# TODO: name clashes are possible.
# Include dir names (except tests/integration/) to avoid name clashes
nodeid = os.environ["PYTEST_CURRENT_TEST"]
nodeid_without_dir = nodeid.split("/")[-1]
parts = nodeid_without_dir.split("::")
raw = "-".join(parts)
self.name = (
raw.replace(".py", "")
.replace("[", "-")
.replace("]", "")
.replace(" (call)", "")
)

def get_received_filename(self) -> str:
return str(APPROVED_DIR / str(self.name + ".received" + ".txt"))

def get_approved_filename(self) -> str:
return str(APPROVED_DIR / str(self.name + ".approved" + ".txt"))

0 comments on commit 56723c2

Please sign in to comment.