diff --git a/tests/conftest.py b/tests/conftest.py index aec430c..ba03317 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,7 @@ from pathlib import Path import pytest +from _pytest.nodes import Node from loguru import logger from pyinstrument import Profiler @@ -12,9 +13,7 @@ def auto_profile(request): """ Generate a HTML file for each test node in your test suite inside the .profiles directory. - Links: - * https://pyinstrument.readthedocs.io/en/latest/guide.html#profile-pytest-tests - * https://docs.pytest.org/en/latest/how-to/fixtures.html?highlight=conftest#scope-sharing-fixtures-across-classes-modules-packages-or-session + https://pyinstrument.readthedocs.io/en/latest/guide.html#profile-pytest-tests """ # noinspection PyPep8Naming @@ -27,8 +26,13 @@ def auto_profile(request): yield # Run test profiler.stop() - PROFILE_ROOT.mkdir(exist_ok=True) - results_file = PROFILE_ROOT / f"{request.node.name}.html" + node: Node = request.node + profile_html_path = PROFILE_ROOT / f"{node.path.parent.relative_to(TESTS_ROOT)}" + if not profile_html_path.exists(): + # If parents is false (the default), a missing parent raises FileNotFoundError. + # If exist_ok is false (the default), FileExistsError is raised if the target directory already exists. + profile_html_path.mkdir(parents=True, exist_ok=True) + results_file = profile_html_path / f"{node.name}.html" with open(results_file, "w", encoding="utf-8") as f_html: f_html.write(profiler.output_html()) - logger.info(f"Generated result: [{results_file}]") + logger.info(f"Generated profile result: [{results_file}]")