terminalwriter: extract plugin printing logic and add positive unittests

This commit is contained in:
Ronny Pfannschmidt 2015-08-17 09:10:01 +02:00
parent 740a97a8cc
commit 7758bcd141
2 changed files with 38 additions and 14 deletions

View File

@ -298,18 +298,9 @@ class TerminalReporter:
plugininfo = config.pluginmanager.list_plugin_distinfo() plugininfo = config.pluginmanager.list_plugin_distinfo()
if plugininfo: if plugininfo:
l = []
for plugin, dist in plugininfo: lines.append(
# gets us name and version! "plugins: %s" % ", ".join(_plugin_nameversions(plugininfo)))
name = '{dist.project_name}-{dist.version}'.format(dist=dist)
# questionable convenience, but it keeps things short
if name.startswith("pytest-"):
name = name[7:]
# we decided to print python package names
# they can have more than one plugin
if name not in l:
l.append(name)
lines.append("plugins: %s" % ", ".join(l))
return lines return lines
def pytest_collection_finish(self, session): def pytest_collection_finish(self, session):
@ -554,3 +545,18 @@ def build_summary_stats_line(stats):
color = 'yellow' color = 'yellow'
return (line, color) return (line, color)
def _plugin_nameversions(plugininfo):
l = []
for plugin, dist in plugininfo:
# gets us name and version!
name = '{dist.project_name}-{dist.version}'.format(dist=dist)
# questionable convenience, but it keeps things short
if name.startswith("pytest-"):
name = name[7:]
# we decided to print python package names
# they can have more than one plugin
if name not in l:
l.append(name)
return l

View File

@ -1,19 +1,23 @@
""" """
terminal reporting of the full testing process. terminal reporting of the full testing process.
""" """
import collections
import pytest import pytest
import py import py
import pluggy import pluggy
import sys import sys
from _pytest.terminal import TerminalReporter, repr_pythonversion, getreportopt from _pytest.terminal import TerminalReporter, repr_pythonversion, getreportopt
from _pytest.terminal import build_summary_stats_line from _pytest.terminal import build_summary_stats_line, _plugin_nameversions
from _pytest import runner from _pytest import runner
def basic_run_report(item): def basic_run_report(item):
runner.call_and_report(item, "setup", log=False) runner.call_and_report(item, "setup", log=False)
return runner.call_and_report(item, "call", log=False) return runner.call_and_report(item, "call", log=False)
DistInfo = collections.namedtuple('DistInfo', ['project_name', 'version'])
class Option: class Option:
def __init__(self, verbose=False, fulltrace=False): def __init__(self, verbose=False, fulltrace=False):
self.verbose = verbose self.verbose = verbose
@ -40,6 +44,21 @@ def pytest_generate_tests(metafunc):
funcargs={'option': Option(fulltrace=True)}) funcargs={'option': Option(fulltrace=True)})
@pytest.mark.parametrize('input,expected', [
([DistInfo(project_name='test', version=1)], ['test-1']),
([DistInfo(project_name='pytest-test', version=1)], ['test-1']),
([
DistInfo(project_name='test', version=1),
DistInfo(project_name='test', version=1)
], ['test-1']),
], ids=['normal', 'prefix-strip', 'deduplicate'])
def test_plugin_nameversion(input, expected):
pluginlist = [(None, x) for x in input]
result = _plugin_nameversions(pluginlist)
assert result == expected
class TestTerminal: class TestTerminal:
def test_pass_skip_fail(self, testdir, option): def test_pass_skip_fail(self, testdir, option):
testdir.makepyfile(""" testdir.makepyfile("""
@ -783,4 +802,3 @@ def test_summary_stats(exp_line, exp_color, stats_arg):
print("Actually got: \"%s\"; with color \"%s\"" % (line, color)) print("Actually got: \"%s\"; with color \"%s\"" % (line, color))
assert line == exp_line assert line == exp_line
assert color == exp_color assert color == exp_color