From 1fc466e8ac04c19fa6e9471b315fcfe20414c067 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Fri, 27 Sep 2013 15:48:03 +0200 Subject: [PATCH] add terminalreporter.section|line methods to print extra information. --- CHANGELOG | 4 +++- _pytest/hookspec.py | 2 +- _pytest/terminal.py | 9 ++++++++- testing/test_mark.py | 2 +- testing/test_terminal.py | 13 +++++++++++++ 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index a9892d128..65d561183 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -142,7 +142,9 @@ Bug fixes: - better parametrize error messages, thanks Brianna Laugher - +- pytest_terminal_summary(terminalreporter) hooks can now use + ".section(title)" and ".line(msg)" methods to print extra + information at the end of a test run. Changes between 2.3.4 and 2.3.5 ----------------------------------- diff --git a/_pytest/hookspec.py b/_pytest/hookspec.py index 64479f03f..e4098c060 100644 --- a/_pytest/hookspec.py +++ b/_pytest/hookspec.py @@ -221,7 +221,7 @@ def pytest_report_teststatus(report): pytest_report_teststatus.firstresult = True def pytest_terminal_summary(terminalreporter): - """ add additional section in terminal summary reporting. """ + """ add additional section in terminal summary reporting. """ # ------------------------------------------------------------------------- # doctest hooks diff --git a/_pytest/terminal.py b/_pytest/terminal.py index 92caee3f4..82ec17885 100644 --- a/_pytest/terminal.py +++ b/_pytest/terminal.py @@ -100,7 +100,7 @@ class TerminalReporter: self.startdir = self.curdir = py.path.local() if file is None: file = py.std.sys.stdout - self._tw = py.io.TerminalWriter(file) + self._tw = self.writer = py.io.TerminalWriter(file) self.currentfspath = None self.reportchars = getreportopt(config) self.hasmarkup = self._tw.hasmarkup @@ -148,6 +148,12 @@ class TerminalReporter: self.ensure_newline() self._tw.sep(sep, title, **markup) + def section(self, title, sep="=", **kw): + self._tw.sep(sep, title, **kw) + + def line(self, msg, **kw): + self._tw.line(msg, **kw) + def pytest_internalerror(self, excrepr): for line in str(excrepr).split("\n"): self.write_line("INTERNALERROR> " + line) @@ -179,6 +185,7 @@ class TerminalReporter: res = self.config.hook.pytest_report_teststatus(report=rep) cat, letter, word = res self.stats.setdefault(cat, []).append(rep) + self._tests_ran = True if not letter and not word: # probably passed setup/teardown return diff --git a/testing/test_mark.py b/testing/test_mark.py index eeea4f023..fae13323a 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -317,7 +317,7 @@ class TestFunctional: request.applymarker(pytest.mark.hello) def pytest_terminal_summary(terminalreporter): l = terminalreporter.stats['passed'] - terminalreporter._tw.line("keyword: %s" % l[0].keywords) + terminalreporter.writer.line("keyword: %s" % l[0].keywords) """) testdir.makepyfile(""" def test_func(arg): diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 0507530a4..3a0693d97 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -699,3 +699,16 @@ def test_tbstyle_native_setup_error(testdir): result.stdout.fnmatch_lines([ '*File *test_tbstyle_native_setup_error.py", line *, in setup_error_fixture*' ]) + +def test_terminal_summary(testdir): + testdir.makeconftest(""" + def pytest_terminal_summary(terminalreporter): + w = terminalreporter + w.section("hello") + w.line("world") + """) + result = testdir.runpytest() + result.stdout.fnmatch_lines(""" + *==== hello ====* + world + """)