From 98608611af860046a07a5f64e1b8144fa0798f7a Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 27 Jan 2010 12:52:19 +0100 Subject: [PATCH] closes #67 new super-short traceback-printing option: "--tb=line" will print a single line for each failing (python) test indicating its filename, lineno and the failure value --HG-- branch : trunk --- CHANGELOG | 1 + py/_plugin/pytest_terminal.py | 30 ++++++++++++++++---------- testing/plugin/test_pytest_terminal.py | 20 +++++++++++++++++ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 1f22b3b89..8275daed3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ Changes between 1.2.1 and 1.2.0 ===================================== +- fix issue67: new super-short traceback-printing option: "--tb=line" will print a single line for each failing (python) test indicating its filename, lineno and the failure value - fix issue78: always call python-level teardown functions even if the according setup failed - but make sure that setup is called repeatedly and no teardown if the setup raises a Skipped (as sone by py.test.skip()). diff --git a/py/_plugin/pytest_terminal.py b/py/_plugin/pytest_terminal.py index 1ea6792ca..7aca5e31b 100644 --- a/py/_plugin/pytest_terminal.py +++ b/py/_plugin/pytest_terminal.py @@ -18,8 +18,8 @@ def pytest_addoption(parser): help="show more info, valid: skipped,xfailed") group._addoption('--tb', metavar="style", action="store", dest="tbstyle", default='long', - type="choice", choices=['long', 'short', 'no'], - help="traceback verboseness (long/short/no).") + type="choice", choices=['long', 'short', 'no', 'line'], + help="traceback print mode (long/short/line/no).") group._addoption('--fulltrace', action="store_true", dest="fulltrace", default=False, help="don't cut any tracebacks (default is to cut).") @@ -272,15 +272,18 @@ class TerminalReporter: if failreports: self.write_sep("#", "LOOPONFAILING", red=True) for report in failreports: - try: - loc = report.longrepr.reprcrash - except AttributeError: - loc = str(report.longrepr)[:50] + loc = self._getcrashline(report) self.write_line(loc, red=True) self.write_sep("#", "waiting for changes") for rootdir in rootdirs: self.write_line("### Watching: %s" %(rootdir,), bold=True) + def _getcrashline(self, report): + try: + return report.longrepr.reprcrash + except AttributeError: + return str(report.longrepr)[:50] + def _reportinfoline(self, item): collect_fspath = self._getfspath(item) fspath, lineno, msg = self._getreportinfo(item) @@ -333,13 +336,18 @@ class TerminalReporter: # def summary_failures(self): - if 'failed' in self.stats and self.config.option.tbstyle != "no": + tbstyle = self.config.getvalue("tbstyle") + if 'failed' in self.stats and tbstyle != "no": self.write_sep("=", "FAILURES") for rep in self.stats['failed']: - msg = self._getfailureheadline(rep) - self.write_sep("_", msg) - self.write_platinfo(rep) - rep.toterminal(self._tw) + if tbstyle == "line": + line = self._getcrashline(rep) + self.write_line(line) + else: + msg = self._getfailureheadline(rep) + self.write_sep("_", msg) + self.write_platinfo(rep) + rep.toterminal(self._tw) def summary_errors(self): if 'error' in self.stats and self.config.option.tbstyle != "no": diff --git a/testing/plugin/test_pytest_terminal.py b/testing/plugin/test_pytest_terminal.py index 2fbf520a2..91b206561 100644 --- a/testing/plugin/test_pytest_terminal.py +++ b/testing/plugin/test_pytest_terminal.py @@ -153,6 +153,26 @@ class TestTerminal: assert '--calling--' not in s assert 'IndexError' not in s + def test_tb_crashline(self, testdir, option): + p = testdir.makepyfile(""" + import py + def g(): + raise IndexError + def test_func1(): + print (6*7) + g() # --calling-- + def test_func2(): + assert 0, "hello" + """) + result = testdir.runpytest("--tb=line") + bn = p.basename + result.stdout.fnmatch_lines([ + "*%s:3: IndexError*" % bn, + "*%s:8: AssertionError: hello*" % bn, + ]) + s = result.stdout.str() + assert "def test_func2" not in s + def test_show_path_before_running_test(self, testdir, linecomp): item = testdir.getitem("def test_func(): pass") tr = TerminalReporter(item.config, file=linecomp.stringio)