From 460afdf4047f50bec8e3a5260a7aa4506f38b5b1 Mon Sep 17 00:00:00 2001 From: Thomas Vandal Date: Sun, 5 Apr 2026 04:39:14 -0400 Subject: [PATCH] fix(pytest): replace `rootpath` with `rootdir` (#106) * Replace rootdir with rootpath * Add try/except to handle older pytest versions as well * Extract try/except to a function and use in `pytest_runtest_makereport` --- neotest_python/pytest_.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/neotest_python/pytest_.py b/neotest_python/pytest_.py index a010b6e..53cc062 100644 --- a/neotest_python/pytest_.py +++ b/neotest_python/pytest_.py @@ -77,10 +77,19 @@ class NeotestResultCollector: def pytest_configure(self, config: "pytest.Config"): self.pytest_config = config + def _get_abs_path(self, file_path: Union[str, Path]): + try: + # rootpath is now the preferred way to access root + abs_path = str(self.pytest_config.rootpath / file_path) + except AttributeError: + # fallback to rootdir for older pytest versions + abs_path = str(Path(self.pytest_config.rootdir, file_path)) + return abs_path + def pytest_deselected(self, items: List["pytest.Item"]): for report in items: file_path, *name_path = report.nodeid.split("::") - abs_path = str(Path(self.pytest_config.rootdir, file_path)) + abs_path = self._get_abs_path(file_path) *namespaces, test_name = name_path valid_test_name, *params = test_name.split("[") # ] pos_id = "::".join([abs_path, *(namespaces), valid_test_name]) @@ -130,7 +139,7 @@ class NeotestResultCollector: return file_path, *name_path = report.nodeid.split("::") - abs_path = str(Path(self.pytest_config.rootdir, file_path)) + abs_path = self._get_abs_path(file_path) *namespaces, test_name = name_path valid_test_name, *params = test_name.split("[") # ]