Debian Codegraph @

doc/en/example/customdirectory/conftest.py

raw
1 # content of conftest.py
2 from __future__ import annotations
3
4 import json
5
6 import pytest
7
8
9 class ManifestDirectory(pytest.Directory):
10 def collect(self):
11 # The standard pytest behavior is to loop over all `test_*.py` files and
12 # call `pytest_collect_file` on each file. This collector instead reads
13 # the `manifest.json` file and only calls `pytest_collect_file` for the
14 # files defined there.
15 manifest_path = self.path / "manifest.json"
16 manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
17 ihook = self.ihook
18 for file in manifest["files"]:
19 yield from ihook.pytest_collect_file(
20 file_path=self.path / file, parent=self
21 )
22
23
24 @pytest.hookimpl
25 def pytest_collect_directory(path, parent):
26 # Use our custom collector for directories containing a `manifest.json` file.
27 if path.joinpath("manifest.json").is_file():
28 return ManifestDirectory.from_parent(parent=parent, path=path)
29 # Otherwise fallback to the standard behavior.
30 return None