diff --git a/AUTHORS b/AUTHORS index 153375dca..a467dd941 100644 --- a/AUTHORS +++ b/AUTHORS @@ -138,6 +138,7 @@ Grigorii Eremeev (budulianin) Guido Wesdorp Guoqiang Zhang Harald Armin Massa +Harshna Henk-Jaap Wagenaar Holger Kohr Hugo van Kemenade diff --git a/changelog/8990.bugfix.rst b/changelog/8990.bugfix.rst new file mode 100644 index 000000000..12c93f2c6 --- /dev/null +++ b/changelog/8990.bugfix.rst @@ -0,0 +1 @@ +TerminalReporter's ``_locationline()`` will now pass the correct type of input arg to ``_pytest.pathlib.bestrelpath``. Previously it was passing in ``str`` which raised an exception. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 8c5be3b78..3e9acb1b4 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -862,8 +862,10 @@ class TerminalReporter: yellow=True, ) - def _locationline(self, nodeid, fspath, lineno, domain): - def mkrel(nodeid): + def _locationline( + self, nodeid: str, fspath: str, lineno: Optional[int], domain: str + ) -> str: + def mkrel(nodeid) -> str: line = self.config.cwd_relative_nodeid(nodeid) if domain and line.endswith(domain): line = line[: -len(domain)] @@ -873,13 +875,13 @@ class TerminalReporter: return line # collect_fspath comes from testid which has a "/"-normalized path. - if fspath: res = mkrel(nodeid) if self.verbosity >= 2 and nodeid.split("::")[0] != fspath.replace( "\\", nodes.SEP ): - res += " <- " + bestrelpath(self.startpath, fspath) + fs_path = Path(fspath) + res += " <- " + bestrelpath(self.startpath, fs_path) else: res = "[location]" return res + " " diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index 323ff7b24..bea224510 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -1165,3 +1165,16 @@ def test_log_file_cli_subdirectories_are_successfully_created( result = pytester.runpytest("--log-file=foo/bar/logf.log") assert "logf.log" in os.listdir(expected) assert result.ret == ExitCode.OK + + +def test_locationline_returns_best_relative_location(pytester: Pytester) -> None: + item = pytester.getitem("def test_func(): pass") + item.config.option.verbose = 2 + + tr = TerminalReporter(item.config) + fspath = f"{pytester.path}/unique/path" + nodeid = "nodeid::test" + result = tr._locationline(nodeid, fspath, 1, "domain") + + expected_result = f"{nodeid} <- unique/path " + assert result == expected_result