From 8871ca5bfa0d48a38994903f6b35d480bcc649fd Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sun, 31 Oct 2010 19:51:16 +0100 Subject: [PATCH] introduce "-q" option which decreases verbosity and basically leads to a unittest/nosetest-style "." output add it in an ini file like this: [pytest] addargs=-q and you get that by default. --- bench/bench.py | 2 +- doc/example/nonpython.txt | 2 +- pytest/plugin/terminal.py | 30 +++++++++++++++++++++++------- testing/plugin/test_terminal.py | 10 ++++++++++ tox.ini | 1 + 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/bench/bench.py b/bench/bench.py index b38251a15..2baa92440 100644 --- a/bench/bench.py +++ b/bench/bench.py @@ -2,7 +2,7 @@ import cProfile import py import pstats -stats = cProfile.run('py.test.cmdline.main(["empty.py"])', 'prof') +stats = cProfile.run('py.test.cmdline.main(["empty.py", ])', 'prof') p = pstats.Stats("prof") p.strip_dirs() p.sort_stats('cumulative') diff --git a/doc/example/nonpython.txt b/doc/example/nonpython.txt index 25a40d960..2e99904ac 100644 --- a/doc/example/nonpython.txt +++ b/doc/example/nonpython.txt @@ -23,7 +23,7 @@ You can create a simple example file: and if you installed `PyYAML`_ or a compatible YAML-parser you can now execute the test specification:: - nonpython $ py.test + nonpython $ py.test test_simple.yml =========================== test session starts ============================ platform linux2 -- Python 2.6.5 -- pytest-2.0.0.dev10 test path 1: /home/hpk/p/pytest/doc/example/nonpython diff --git a/pytest/plugin/terminal.py b/pytest/plugin/terminal.py index 686ef96f2..42a07591f 100644 --- a/pytest/plugin/terminal.py +++ b/pytest/plugin/terminal.py @@ -11,6 +11,8 @@ def pytest_addoption(parser): group = parser.getgroup("terminal reporting", "reporting", after="general") group._addoption('-v', '--verbose', action="count", dest="verbose", default=0, help="increase verbosity."), + group._addoption('-q', '--quiet', action="count", + dest="quiet", default=0, help="decreate verbosity."), group._addoption('-r', action="store", dest="reportchars", default=None, metavar="chars", help="show extra test summary info as specified by chars (f)ailed, " @@ -30,6 +32,7 @@ def pytest_addoption(parser): help="don't cut any tracebacks (default is to cut).") def pytest_configure(config): + config.option.verbose -= config.option.quiet if config.option.collectonly: reporter = CollectonlyReporter(config) else: @@ -87,6 +90,11 @@ def pytest_report_teststatus(report): class TerminalReporter: def __init__(self, config, file=None): self.config = config + self.verbosity = self.config.option.verbose + self.showheader = self.verbosity >= 0 + self.showfspath = self.verbosity >= 0 + self.showlongtestinfo = self.verbosity > 0 + self.stats = {} self.curdir = py.path.local() if file is None: @@ -158,10 +166,10 @@ class TerminalReporter: def pytest_runtest_logstart(self, nodeid, location, fspath): # ensure that the path is printed before the # 1st test of a module starts running - if self.config.option.verbose: + if self.showlongtestinfo: line = self._locationline(fspath, *location) self.write_ensure_prefix(line, "") - else: + elif self.showfspath: self.write_fspath_result(py.path.local(fspath), "") def pytest_runtest_logreport(self, report): @@ -172,8 +180,8 @@ class TerminalReporter: if not letter and not word: # probably passed setup/teardown return - if not self.config.option.verbose: - if not hasattr(rep, 'node'): + if self.verbosity <= 0: + if not hasattr(rep, 'node') and self.showfspath: self.write_fspath_result(rep.fspath, letter) else: self._tw.write(letter) @@ -209,12 +217,14 @@ class TerminalReporter: self.write_fspath_result(report.fspath, "S") def pytest_sessionstart(self, session): - self.write_sep("=", "test session starts", bold=True) self._sessionstarttime = py.std.time.time() + if not self.showheader: + return + self.write_sep("=", "test session starts", bold=True) verinfo = ".".join(map(str, sys.version_info[:3])) msg = "platform %s -- Python %s" % (sys.platform, verinfo) msg += " -- pytest-%s" % (py.test.__version__) - if self.config.option.verbose or self.config.option.debug or \ + if self.verbosity > 0 or self.config.option.debug or \ getattr(self.config.option, 'pastebin', None): msg += " -- " + str(sys.executable) self.write_line(msg) @@ -224,6 +234,8 @@ class TerminalReporter: self.write_line(line) def pytest_log_finishcollection(self): + if not self.showheader: + return for i, testarg in enumerate(self.config.args): self.write_line("test path %d: %s" %(i+1, testarg)) @@ -333,7 +345,11 @@ class TerminalReporter: parts.append("%d %s" %(len(val), key)) line = ", ".join(parts) # XXX coloring - self.write_sep("=", "%s in %.2f seconds" %(line, session_duration), bold=True) + msg = "%s in %.2f seconds" %(line, session_duration) + if self.verbosity >= 0: + self.write_sep("=", msg, bold=True) + else: + self.write_line(msg, bold=True) def summary_deselected(self): if 'deselected' in self.stats: diff --git a/testing/plugin/test_terminal.py b/testing/plugin/test_terminal.py index 244cd0ece..5dc740f92 100644 --- a/testing/plugin/test_terminal.py +++ b/testing/plugin/test_terminal.py @@ -37,6 +37,8 @@ def pytest_generate_tests(metafunc): funcargs={'option': Option(verbose=False)}) metafunc.addcall(id="verbose", funcargs={'option': Option(verbose=True)}) + metafunc.addcall(id="quiet", + funcargs={'option': Option(verbose=-1)}) metafunc.addcall(id="fulltrace", funcargs={'option': Option(fulltrace=True)}) @@ -430,6 +432,14 @@ class TestTerminalFunctional: ]) assert result.ret == 1 + def test_quiet_reporting(self, testdir): + p1 = testdir.makepyfile("def test_pass(): pass") + result = testdir.runpytest(p1, '-q') + s = result.stdout.str() + assert 'test session starts' not in s + assert p1.basename not in s + assert "===" not in s + def test_fail_extra_reporting(testdir): p = testdir.makepyfile("def test_this(): assert 0") result = testdir.runpytest(p) diff --git a/tox.ini b/tox.ini index ca901ee50..b50e93735 100644 --- a/tox.ini +++ b/tox.ini @@ -52,3 +52,4 @@ commands= [pytest] minversion=2.0 plugins=pytester +#addargs=-q -x