From 88cfaebbcbdd80bc01ad44834a1bb37bfa2c38e6 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 12 Jan 2011 19:39:36 +0100 Subject: [PATCH] fix issue12 - show plugin versions with "--version" and "--traceconfig" and also document how to add extra information to reporting test header --- CHANGELOG | 3 ++ _pytest/core.py | 2 ++ _pytest/helpconfig.py | 19 +++++++++++ doc/example/simple.txt | 66 ++++++++++++++++++++++++++++++++++---- pytest.py | 2 +- setup.py | 2 +- testing/test_config.py | 2 ++ testing/test_core.py | 1 + testing/test_helpconfig.py | 13 +++++++- 9 files changed, 101 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 011b2eaa7..d34af05b0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,8 @@ Changes between 2.0.0 and 2.0.1.devX ---------------------------------------------- +- fix issue12 - show plugin versions with "--version" and + "--traceconfig" and also document how to add extra information + to reporting test header - fix issue17 (import-* reporting issue on python3) by requiring py>1.4.0 (1.4.1 is going to include it) - fix issue10 (numpy arrays truth checking) by refining diff --git a/_pytest/core.py b/_pytest/core.py index aae7c1c6e..7a67e7cbd 100644 --- a/_pytest/core.py +++ b/_pytest/core.py @@ -63,6 +63,7 @@ class PluginManager(object): self._plugins = [] self._hints = [] self.trace = TagTracer().get("pluginmanage") + self._plugin_distinfo = [] if os.environ.get('PYTEST_DEBUG'): err = sys.stderr encoding = getattr(err, 'encoding', 'utf8') @@ -156,6 +157,7 @@ class PluginManager(object): plugin = ep.load() except DistributionNotFound: continue + self._plugin_distinfo.append((ep.dist, plugin)) self.register(plugin, name=name) def consider_preparse(self, args): diff --git a/_pytest/helpconfig.py b/_pytest/helpconfig.py index 6d0958420..0e37affcf 100644 --- a/_pytest/helpconfig.py +++ b/_pytest/helpconfig.py @@ -28,6 +28,10 @@ def pytest_cmdline_main(config): p = py.path.local(pytest.__file__) sys.stderr.write("This is py.test version %s, imported from %s\n" % (pytest.__version__, p)) + plugininfo = getpluginversioninfo(config) + if plugininfo: + for line in plugininfo: + sys.stderr.write(line + "\n") return 0 elif config.option.help: config.pluginmanager.do_configure(config) @@ -69,11 +73,26 @@ conftest_options = [ ('pytest_plugins', 'list of plugin names to load'), ] +def getpluginversioninfo(config): + lines = [] + plugininfo = config.pluginmanager._plugin_distinfo + if plugininfo: + lines.append("setuptools registered plugins:") + for dist, plugin in plugininfo: + loc = getattr(plugin, '__file__', repr(plugin)) + content = "%s-%s at %s" % (dist.project_name, dist.version, loc) + lines.append(" " + content) + return lines + def pytest_report_header(config): lines = [] if config.option.debug or config.option.traceconfig: lines.append("using: pytest-%s pylib-%s" % (pytest.__version__,py.__version__)) + + verinfo = getpluginversioninfo(config) + if verinfo: + lines.extend(verinfo) if config.option.traceconfig: lines.append("active plugins:") diff --git a/doc/example/simple.txt b/doc/example/simple.txt index ffdf625af..95fb70155 100644 --- a/doc/example/simple.txt +++ b/doc/example/simple.txt @@ -138,7 +138,7 @@ let's run the full monty:: E assert 4 < 4 test_compute.py:3: AssertionError - 1 failed, 4 passed in 0.02 seconds + 1 failed, 4 passed in 0.03 seconds As expected when running the full range of ``param1`` values we'll get an error on the last one. @@ -167,13 +167,13 @@ directory with the above conftest.py:: $ py.test =========================== test session starts ============================ - platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev3 + platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev8 gw0 I / gw1 I / gw2 I / gw3 I gw0 [0] / gw1 [0] / gw2 [0] / gw3 [0] scheduling tests via LoadScheduling - ============================= in 0.29 seconds ============================= + ============================= in 0.43 seconds ============================= .. _`retrieved by hooks as item keywords`: @@ -214,12 +214,12 @@ and when running it will see a skipped "slow" test:: $ py.test -rs # "-rs" means report details on the little 's' =========================== test session starts ============================ - platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev3 + platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev8 collecting ... collected 2 items test_module.py .s ========================= short test summary info ========================== - SKIP [1] /tmp/doc-exec-25/conftest.py:9: need --runslow option to run + SKIP [1] /tmp/doc-exec-46/conftest.py:9: need --runslow option to run =================== 1 passed, 1 skipped in 0.02 seconds ==================== @@ -227,7 +227,7 @@ Or run it including the ``slow`` marked test:: $ py.test --runslow =========================== test session starts ============================ - platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev3 + platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev8 collecting ... collected 2 items test_module.py .. @@ -303,3 +303,57 @@ accordingly in your application. It's also a good idea to rather use your own application module rather than ``sys`` for handling flag. +Adding info to test report header +-------------------------------------------------------------- + +.. regendoc:wipe + +It's easy to present extra information in a py.test run:: + + # content of conftest.py + + def pytest_report_header(config): + return "project deps: mylib-1.1" + +which will add the string to the test header accordingly:: + + $ py.test + =========================== test session starts ============================ + platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev8 + project deps: mylib-1.1 + collecting ... collected 0 items + + ============================= in 0.00 seconds ============================= + +.. regendoc:wipe + +You can also return a list of strings which will be considered as several +lines of information. You can of course also make the amount of reporting +information on e.g. the value of ``config.option.verbose`` so that +you present more information appropriately:: + + # content of conftest.py + + def pytest_report_header(config): + if config.option.verbose > 0: + return ["info1: did you know that ...", "did you?"] + +which will add info only when run with "--v":: + + $ py.test -v + =========================== test session starts ============================ + platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev8 -- /home/hpk/venv/0/bin/python + info1: did you know that ... + did you? + collecting ... collected 0 items + + ============================= in 0.00 seconds ============================= + +and nothing when run plainly:: + + $ py.test + =========================== test session starts ============================ + platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev8 + collecting ... collected 0 items + + ============================= in 0.00 seconds ============================= diff --git a/pytest.py b/pytest.py index 377a512f3..2a905365e 100644 --- a/pytest.py +++ b/pytest.py @@ -1,7 +1,7 @@ """ unit and functional testing with Python. """ -__version__ = '2.0.1.dev7' +__version__ = '2.0.1.dev8' __all__ = ['main'] from _pytest.core import main, UsageError, _preloadplugins diff --git a/setup.py b/setup.py index 593a15c52..6cf82f6aa 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def main(): name='pytest', description='py.test: simple powerful testing with Python', long_description = long_description, - version='2.0.1.dev7', + version='2.0.1.dev8', url='http://pytest.org', license='MIT license', platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], diff --git a/testing/test_config.py b/testing/test_config.py index 130ff2aa6..0e7c60028 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -231,6 +231,8 @@ def test_preparse_ordering_with_setuptools(testdir, monkeypatch): assert name == "pytest11" class EntryPoint: name = "mytestplugin" + class dist: + pass def load(self): class PseudoPlugin: x = 42 diff --git a/testing/test_core.py b/testing/test_core.py index 76721974e..c389b7ddf 100644 --- a/testing/test_core.py +++ b/testing/test_core.py @@ -66,6 +66,7 @@ class TestBootstrapping: assert name == "pytest11" class EntryPoint: name = "pytest_mytestplugin" + dist = None def load(self): class PseudoPlugin: x = 42 diff --git a/testing/test_helpconfig.py b/testing/test_helpconfig.py index 443ae7a82..02d32e18f 100644 --- a/testing/test_helpconfig.py +++ b/testing/test_helpconfig.py @@ -1,13 +1,18 @@ import py, pytest,os from _pytest.helpconfig import collectattr -def test_version(testdir): +def test_version(testdir, pytestconfig): result = testdir.runpytest("--version") assert result.ret == 0 #p = py.path.local(py.__file__).dirpath() result.stderr.fnmatch_lines([ '*py.test*%s*imported from*' % (pytest.__version__, ) ]) + if pytestconfig.pluginmanager._plugin_distinfo: + result.stderr.fnmatch_lines([ + "*setuptools registered plugins:", + "*at*", + ]) def test_help(testdir): result = testdir.runpytest("--help") @@ -51,3 +56,9 @@ def test_hookvalidation_optional(testdir): result = testdir.runpytest() assert result.ret == 0 +def test_traceconfig(testdir): + result = testdir.runpytest("--traceconfig") + result.stdout.fnmatch_lines([ + "*using*pytest*py*", + "*active plugins*", + ])