Skip to content

Commit 64ae315

Browse files
test(approvaltests): add postfix parameter to PytestNamer
This enables the usage of the Namer multiple times in one test.
1 parent e38f4b5 commit 64ae315

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

approvaltests_config.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"directory": "tests/approvals"
3+
}

tests/approvaltests_namers.py

+27-19
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,47 @@
33

44
from approvaltests.namer.namer_base import NamerBase
55

6-
TEST_DIR = Path(__file__).parent
7-
APPROVED_DIR = TEST_DIR / "approvals"
8-
96

107
class PytestNamer(NamerBase):
11-
def __init__(self, extension=None):
8+
def __init__(self, extension: str = ".txt", postfix: str = ""):
129
"""An approval tests Namer for naming approved and received files.
1310
14-
These files will get stored under:
15-
`tests/approval/{module}/test_file.py--TestClass--test_func[a]`
16-
17-
This class uses the `PYTEST_CURRENT_TEST` environment variable, which
18-
consist of the node ID and the current stage:
11+
This Namer uses the `PYTEST_CURRENT_TEST` environment variable, which
12+
consist of the node ID and the current stage to derive names:
1913
`relative/path/to/test_file.py::TestClass::test_func[a] (call)`
2014
21-
During a pytest test session, stages can be setup, teardown or call.
22-
Approval tests should only be used during the call stage and therefore
23-
the ` (call)` postfix is removed.
15+
Above example will be translated to:
16+
`relative/path/to/test_file.py--TestClass--test_func[a]`
2417
25-
To avoid the forbidden character `:` in system paths, it is replaced by `-`.
18+
Following changes are applied to the `PYTEST_CURRENT_TEST` environmen
19+
variables:
20+
- To avoid the forbidden character `:` in system paths, it is
21+
replaced by `-`.
22+
- During a pytest test session, stages can be setup, teardown or
23+
call. Approval tests should only be used during the call stage
24+
and therefore the ` (call)` postfix is removed.
25+
26+
If verify is called multiple times use `postfix` parameter to
27+
differentiate names.
2628
"""
2729
self.nodeid: Path = Path(os.environ["PYTEST_CURRENT_TEST"])
30+
self.postfix = postfix
2831
NamerBase.__init__(self, extension)
2932

3033
def get_file_name(self) -> Path:
31-
"""File name is pytest nodeid w/out directory name and pytest stage."""
32-
return Path(str(self.nodeid.name).replace(" (call)", "").replace("::", "-"))
34+
"""File name is pytest nodeid w/out directory name and pytest."""
35+
file_name = str(self.nodeid.name).replace(" (call)", "").replace("::", "-")
36+
return Path(file_name) / self.postfix
3337

3438
def get_directory(self) -> Path:
3539
"""Directory is `tests/approval/{module}` derived from pytest nodeid."""
3640
parts = self.nodeid.parent.parts
3741
directory = Path(*[p for p in parts if p not in ["tests"]])
38-
return APPROVED_DIR / directory
39-
40-
def get_config(self) -> dict:
41-
return {}
42+
config = self.get_config()
43+
if "directory" in config.keys():
44+
return Path(config["directory"]) / directory
45+
else:
46+
return directory
47+
48+
def config_directory(self) -> str:
49+
return Path(__file__).parent.parent

0 commit comments

Comments
 (0)