Implement progress percentage reporting while running tests

Fix #2657
This commit is contained in:
Bruno Oliveira 2017-09-27 14:42:55 -03:00
parent 7a7cb8c8c5
commit dab889304e
8 changed files with 81 additions and 27 deletions

View File

@ -51,6 +51,10 @@ def pytest_addoption(parser):
choices=['yes', 'no', 'auto'], choices=['yes', 'no', 'auto'],
help="color terminal output (yes/no/auto).") help="color terminal output (yes/no/auto).")
parser.addini("console_output_style",
help="console output: classic or with additional progress information.",
default='progress')
def pytest_configure(config): def pytest_configure(config):
config.option.verbose -= config.option.quiet config.option.verbose -= config.option.quiet
@ -135,16 +139,20 @@ class TerminalReporter:
self.showfspath = self.verbosity >= 0 self.showfspath = self.verbosity >= 0
self.showlongtestinfo = self.verbosity > 0 self.showlongtestinfo = self.verbosity > 0
self._numcollected = 0 self._numcollected = 0
self._session = None
self.stats = {} self.stats = {}
self.startdir = py.path.local() self.startdir = py.path.local()
if file is None: if file is None:
file = sys.stdout file = sys.stdout
self._writer = _pytest.config.create_terminal_writer(config, file) self._writer = _pytest.config.create_terminal_writer(config, file)
self._screen_width = self.writer.fullwidth
self.currentfspath = None self.currentfspath = None
self.reportchars = getreportopt(config) self.reportchars = getreportopt(config)
self.hasmarkup = self.writer.hasmarkup self.hasmarkup = self.writer.hasmarkup
self.isatty = file.isatty() self.isatty = file.isatty()
self._progress_items_reported = 0
self._show_progress_info = self.config.getini('console_output_style') == 'progress'
@property @property
def writer(self): def writer(self):
@ -163,6 +171,8 @@ class TerminalReporter:
def write_fspath_result(self, nodeid, res): def write_fspath_result(self, nodeid, res):
fspath = self.config.rootdir.join(nodeid.split("::")[0]) fspath = self.config.rootdir.join(nodeid.split("::")[0])
if fspath != self.currentfspath: if fspath != self.currentfspath:
if self.currentfspath is not None:
self._write_progress_information_filling_space()
self.currentfspath = fspath self.currentfspath = fspath
fspath = self.startdir.bestrelpath(fspath) fspath = self.startdir.bestrelpath(fspath)
self.writer.line() self.writer.line()
@ -177,6 +187,7 @@ class TerminalReporter:
if extra: if extra:
self.writer.write(extra, **kwargs) self.writer.write(extra, **kwargs)
self.currentfspath = -2 self.currentfspath = -2
self._write_progress_information_filling_space()
def ensure_newline(self): def ensure_newline(self):
if self.currentfspath: if self.currentfspath:
@ -256,20 +267,25 @@ class TerminalReporter:
rep = report rep = report
res = self.config.hook.pytest_report_teststatus(report=rep) res = self.config.hook.pytest_report_teststatus(report=rep)
cat, letter, word = res cat, letter, word = res
if isinstance(word, tuple):
word, markup = word
else:
markup = None
self.stats.setdefault(cat, []).append(rep) self.stats.setdefault(cat, []).append(rep)
self._tests_ran = True self._tests_ran = True
if not letter and not word: if not letter and not word:
# probably passed setup/teardown # probably passed setup/teardown
return return
running_xdist = hasattr(rep, 'node')
self._progress_items_reported += 1
if self.verbosity <= 0: if self.verbosity <= 0:
if not hasattr(rep, 'node') and self.showfspath: if not running_xdist and self.showfspath:
self.write_fspath_result(rep.nodeid, letter) self.write_fspath_result(rep.nodeid, letter)
else: else:
self.writer.write(letter) self.writer.write(letter)
self._write_progress_if_past_edge()
else: else:
if isinstance(word, tuple): if markup is None:
word, markup = word
else:
if rep.passed: if rep.passed:
markup = {'green': True} markup = {'green': True}
elif rep.failed: elif rep.failed:
@ -279,17 +295,42 @@ class TerminalReporter:
else: else:
markup = {} markup = {}
line = self._locationline(rep.nodeid, *rep.location) line = self._locationline(rep.nodeid, *rep.location)
if not hasattr(rep, 'node'): if not running_xdist:
self.write_ensure_prefix(line, word, **markup) self.write_ensure_prefix(line, word, **markup)
# self.writer.write(word, **markup)
else: else:
self.ensure_newline() self.ensure_newline()
if hasattr(rep, 'node'): if running_xdist:
self.writer.write("[%s] " % rep.node.gateway.id) self.writer.write("[%s] " % rep.node.gateway.id)
self.writer.write(word, **markup) self.writer.write(word, **markup)
self.writer.write(" " + line) self.writer.write(" " + line)
self.currentfspath = -2 self.currentfspath = -2
def _write_progress_if_past_edge(self):
if not self._show_progress_info:
return
last_item = self._progress_items_reported == self._session.testscollected
if last_item:
self._write_progress_information_filling_space()
return
past_edge = self.writer.chars_on_current_line + self._PROGRESS_LENGTH + 1 >= self._screen_width
if past_edge:
msg = self._get_progress_information_message()
self.writer.write(msg + '\n', cyan=True)
_PROGRESS_LENGTH = len(' [100%]')
def _get_progress_information_message(self):
progress = self._progress_items_reported * 100 // self._session.testscollected
return ' [{:3d}%]'.format(progress)
def _write_progress_information_filling_space(self):
if not self._show_progress_info:
return
msg = self._get_progress_information_message()
fill = ' ' * (self.writer.fullwidth - self.writer.chars_on_current_line - len(msg) - 1)
self.write(fill + msg, cyan=True)
def pytest_collection(self): def pytest_collection(self):
if not self.isatty and self.config.option.verbose >= 1: if not self.isatty and self.config.option.verbose >= 1:
self.write("collecting ... ", bold=True) self.write("collecting ... ", bold=True)
@ -332,6 +373,7 @@ class TerminalReporter:
@pytest.hookimpl(trylast=True) @pytest.hookimpl(trylast=True)
def pytest_sessionstart(self, session): def pytest_sessionstart(self, session):
self._session = session
self._sessionstarttime = time.time() self._sessionstarttime = time.time()
if not self.showheader: if not self.showheader:
return return

View File

@ -630,10 +630,10 @@ class TestInvocationVariants(object):
testdir.chdir() testdir.chdir()
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*test_hello.py::test_hello*PASSED", "*test_hello.py::test_hello*PASSED*",
"*test_hello.py::test_other*PASSED", "*test_hello.py::test_other*PASSED*",
"*test_world.py::test_world*PASSED", "*test_world.py::test_world*PASSED*",
"*test_world.py::test_other*PASSED", "*test_world.py::test_other*PASSED*",
"*4 passed*" "*4 passed*"
]) ])
@ -641,7 +641,7 @@ class TestInvocationVariants(object):
result = testdir.runpytest("--pyargs", "-v", "ns_pkg.world.test_world::test_other") result = testdir.runpytest("--pyargs", "-v", "ns_pkg.world.test_world::test_other")
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*test_world.py::test_other*PASSED", "*test_world.py::test_other*PASSED*",
"*1 passed*" "*1 passed*"
]) ])

View File

@ -2119,6 +2119,10 @@ class TestFixtureMarker(object):
assert values == [1, 1, 2, 2] assert values == [1, 1, 2, 2]
def test_module_parametrized_ordering(self, testdir): def test_module_parametrized_ordering(self, testdir):
testdir.makeini("""
[pytest]
console_output_style=classic
""")
testdir.makeconftest(""" testdir.makeconftest("""
import pytest import pytest
@ -2165,6 +2169,10 @@ class TestFixtureMarker(object):
""") """)
def test_class_ordering(self, testdir): def test_class_ordering(self, testdir):
testdir.makeini("""
[pytest]
console_output_style=classic
""")
testdir.makeconftest(""" testdir.makeconftest("""
import pytest import pytest

View File

@ -960,6 +960,10 @@ class TestMetafuncFunctional(object):
]) ])
def test_parametrize_with_ids(self, testdir): def test_parametrize_with_ids(self, testdir):
testdir.makeini("""
[pytest]
console_output_style=classic
""")
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
@ -1005,9 +1009,9 @@ class TestMetafuncFunctional(object):
result = testdir.runpytest("-v") result = testdir.runpytest("-v")
assert result.ret == 1 assert result.ret == 1
result.stdout.fnmatch_lines_random([ result.stdout.fnmatch_lines_random([
"*test_function*basic*PASSED", "*test_function*basic*PASSED*",
"*test_function*1-1*PASSED", "*test_function*1-1*PASSED*",
"*test_function*advanced*FAILED", "*test_function*advanced*FAILED*",
]) ])
def test_fixture_parametrized_empty_ids(self, testdir): def test_fixture_parametrized_empty_ids(self, testdir):
@ -1062,8 +1066,8 @@ class TestMetafuncFunctional(object):
result = testdir.runpytest("-v") result = testdir.runpytest("-v")
assert result.ret == 1 assert result.ret == 1
result.stdout.fnmatch_lines_random([ result.stdout.fnmatch_lines_random([
"*test_function*a0*PASSED", "*test_function*a0*PASSED*",
"*test_function*a1*FAILED" "*test_function*a1*FAILED*"
]) ])
@pytest.mark.parametrize(("scope", "length"), @pytest.mark.parametrize(("scope", "length"),

View File

@ -238,6 +238,6 @@ def test_show_fixtures_and_execute_test(testdir):
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'*SETUP F arg*', '*SETUP F arg*',
'*test_arg (fixtures used: arg)F', '*test_arg (fixtures used: arg)F*',
'*TEARDOWN F arg*', '*TEARDOWN F arg*',
]) ])

View File

@ -820,7 +820,7 @@ def test_traceback_failure(testdir):
""") """)
result = testdir.runpytest(p1, "--tb=long") result = testdir.runpytest(p1, "--tb=long")
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*test_traceback_failure.py F", "*test_traceback_failure.py F*",
"====* FAILURES *====", "====* FAILURES *====",
"____*____", "____*____",
"", "",
@ -840,7 +840,7 @@ def test_traceback_failure(testdir):
result = testdir.runpytest(p1) # "auto" result = testdir.runpytest(p1) # "auto"
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*test_traceback_failure.py F", "*test_traceback_failure.py F*",
"====* FAILURES *====", "====* FAILURES *====",
"____*____", "____*____",
"", "",

View File

@ -266,7 +266,7 @@ class TestPerTestCapturing(object):
""") """)
result = testdir.runpytest(p1) result = testdir.runpytest(p1)
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*test_capturing_outerr.py .F", "*test_capturing_outerr.py .F*",
"====* FAILURES *====", "====* FAILURES *====",
"____*____", "____*____",
"*test_capturing_outerr.py:8: ValueError", "*test_capturing_outerr.py:8: ValueError",

View File

@ -78,7 +78,7 @@ class TestTerminal(object):
]) ])
else: else:
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*test_pass_skip_fail.py .sF" "*test_pass_skip_fail.py .sF*"
]) ])
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
" def test_func():", " def test_func():",
@ -142,12 +142,12 @@ class TestTerminal(object):
""") """)
result = testdir.runpytest(p2) result = testdir.runpytest(p2)
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*test_p2.py .", "*test_p2.py .*",
"*1 passed*", "*1 passed*",
]) ])
result = testdir.runpytest("-v", p2) result = testdir.runpytest("-v", p2)
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*test_p2.py::TestMore::test_p1* <- *test_p1.py*PASSED", "*test_p2.py::TestMore::test_p1* <- *test_p1.py*PASSED*",
]) ])
def test_itemreport_directclasses_not_shown_as_subclasses(self, testdir): def test_itemreport_directclasses_not_shown_as_subclasses(self, testdir):
@ -431,7 +431,7 @@ class TestTerminalFunctional(object):
) )
result = testdir.runpytest("-k", "test_two:", testpath) result = testdir.runpytest("-k", "test_two:", testpath)
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*test_deselected.py ..", "*test_deselected.py ..*",
"=* 1 test*deselected *=", "=* 1 test*deselected *=",
]) ])
assert result.ret == 0 assert result.ret == 0
@ -464,7 +464,7 @@ class TestTerminalFunctional(object):
finally: finally:
old.chdir() old.chdir()
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"test_passes.py ..", "test_passes.py ..*",
"* 2 pass*", "* 2 pass*",
]) ])
assert result.ret == 0 assert result.ret == 0
@ -481,7 +481,7 @@ class TestTerminalFunctional(object):
"platform %s -- Python %s*pytest-%s*py-%s*pluggy-%s" % ( "platform %s -- Python %s*pytest-%s*py-%s*pluggy-%s" % (
py.std.sys.platform, verinfo, py.std.sys.platform, verinfo,
pytest.__version__, py.__version__, pluggy.__version__), pytest.__version__, py.__version__, pluggy.__version__),
"*test_header_trailer_info.py .", "*test_header_trailer_info.py .*",
"=* 1 passed*in *.[0-9][0-9] seconds *=", "=* 1 passed*in *.[0-9][0-9] seconds *=",
]) ])
if pytest.config.pluginmanager.list_plugin_distinfo(): if pytest.config.pluginmanager.list_plugin_distinfo():