|
3 | 3 |
|
4 | 4 | from approvaltests.namer.namer_base import NamerBase
|
5 | 5 |
|
6 |
| -TEST_DIR = Path(__file__).parent |
7 |
| -APPROVED_DIR = TEST_DIR / "approvals" |
8 |
| - |
9 | 6 |
|
10 | 7 | class PytestNamer(NamerBase):
|
11 |
| - def __init__(self, extension=None): |
| 8 | + def __init__(self, extension: str = ".txt", postfix: str = ""): |
12 | 9 | """An approval tests Namer for naming approved and received files.
|
13 | 10 |
|
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: |
19 | 13 | `relative/path/to/test_file.py::TestClass::test_func[a] (call)`
|
20 | 14 |
|
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]` |
24 | 17 |
|
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. |
26 | 28 | """
|
27 | 29 | self.nodeid: Path = Path(os.environ["PYTEST_CURRENT_TEST"])
|
| 30 | + self.postfix = postfix |
28 | 31 | NamerBase.__init__(self, extension)
|
29 | 32 |
|
30 | 33 | 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 |
33 | 37 |
|
34 | 38 | def get_directory(self) -> Path:
|
35 | 39 | """Directory is `tests/approval/{module}` derived from pytest nodeid."""
|
36 | 40 | parts = self.nodeid.parent.parts
|
37 | 41 | 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