diff --git a/py/_plugin/pytest_terminal.py b/py/_plugin/pytest_terminal.py index 1e1751b85..6143910d6 100644 --- a/py/_plugin/pytest_terminal.py +++ b/py/_plugin/pytest_terminal.py @@ -128,6 +128,20 @@ class TerminalReporter: else: return "???", dict(red=True) + def gettestid(self, item, relative=True): + fspath = item.fspath + chain = [x for x in item.listchain() if x.fspath == fspath] + chain = chain[1:] + names = [x.name for x in chain if x.name != "()"] + path = item.fspath + if relative: + relpath = path.relto(self.curdir) + if relpath: + path = relpath + names.insert(0, str(path)) + return "::".join(names) + + def pytest_internalerror(self, excrepr): for line in str(excrepr).split("\n"): self.write_line("INTERNALERROR> " + line) diff --git a/testing/plugin/test_pytest_terminal.py b/testing/plugin/test_pytest_terminal.py index 8aad9f7c8..5d152d12e 100644 --- a/testing/plugin/test_pytest_terminal.py +++ b/testing/plugin/test_pytest_terminal.py @@ -111,6 +111,22 @@ class TestTerminal: assert lines[1].endswith("xy.py .") assert lines[2] == "hello world" + def test_testid(self, testdir, linecomp): + func,method = testdir.getitems(""" + def test_func(): + pass + class TestClass: + def test_method(self): + pass + """) + tr = TerminalReporter(func.config, file=linecomp.stringio) + id = tr.gettestid(func) + assert id.endswith("test_testid.py::test_func") + fspath = py.path.local(id.split("::")[0]) + assert fspath.check() + id = tr.gettestid(method) + assert id.endswith("test_testid.py::TestClass::test_method") + def test_looponfailreport(self, testdir, linecomp): modcol = testdir.getmodulecol(""" def test_fail():