fix issue12 - show plugin versions with "--version" and "--traceconfig" and also document how to add extra information to reporting test header

This commit is contained in:
holger krekel 2011-01-12 19:39:36 +01:00
parent 426e056d2b
commit 88cfaebbcb
9 changed files with 101 additions and 9 deletions

View File

@ -1,5 +1,8 @@
Changes between 2.0.0 and 2.0.1.devX 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 - fix issue17 (import-* reporting issue on python3) by
requiring py>1.4.0 (1.4.1 is going to include it) requiring py>1.4.0 (1.4.1 is going to include it)
- fix issue10 (numpy arrays truth checking) by refining - fix issue10 (numpy arrays truth checking) by refining

View File

@ -63,6 +63,7 @@ class PluginManager(object):
self._plugins = [] self._plugins = []
self._hints = [] self._hints = []
self.trace = TagTracer().get("pluginmanage") self.trace = TagTracer().get("pluginmanage")
self._plugin_distinfo = []
if os.environ.get('PYTEST_DEBUG'): if os.environ.get('PYTEST_DEBUG'):
err = sys.stderr err = sys.stderr
encoding = getattr(err, 'encoding', 'utf8') encoding = getattr(err, 'encoding', 'utf8')
@ -156,6 +157,7 @@ class PluginManager(object):
plugin = ep.load() plugin = ep.load()
except DistributionNotFound: except DistributionNotFound:
continue continue
self._plugin_distinfo.append((ep.dist, plugin))
self.register(plugin, name=name) self.register(plugin, name=name)
def consider_preparse(self, args): def consider_preparse(self, args):

View File

@ -28,6 +28,10 @@ def pytest_cmdline_main(config):
p = py.path.local(pytest.__file__) p = py.path.local(pytest.__file__)
sys.stderr.write("This is py.test version %s, imported from %s\n" % sys.stderr.write("This is py.test version %s, imported from %s\n" %
(pytest.__version__, p)) (pytest.__version__, p))
plugininfo = getpluginversioninfo(config)
if plugininfo:
for line in plugininfo:
sys.stderr.write(line + "\n")
return 0 return 0
elif config.option.help: elif config.option.help:
config.pluginmanager.do_configure(config) config.pluginmanager.do_configure(config)
@ -69,12 +73,27 @@ conftest_options = [
('pytest_plugins', 'list of plugin names to load'), ('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): def pytest_report_header(config):
lines = [] lines = []
if config.option.debug or config.option.traceconfig: if config.option.debug or config.option.traceconfig:
lines.append("using: pytest-%s pylib-%s" % lines.append("using: pytest-%s pylib-%s" %
(pytest.__version__,py.__version__)) (pytest.__version__,py.__version__))
verinfo = getpluginversioninfo(config)
if verinfo:
lines.extend(verinfo)
if config.option.traceconfig: if config.option.traceconfig:
lines.append("active plugins:") lines.append("active plugins:")
plugins = [] plugins = []

View File

@ -138,7 +138,7 @@ let's run the full monty::
E assert 4 < 4 E assert 4 < 4
test_compute.py:3: AssertionError 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 As expected when running the full range of ``param1`` values
we'll get an error on the last one. we'll get an error on the last one.
@ -167,13 +167,13 @@ directory with the above conftest.py::
$ py.test $ py.test
=========================== test session starts ============================ =========================== 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 I / gw1 I / gw2 I / gw3 I
gw0 [0] / gw1 [0] / gw2 [0] / gw3 [0] gw0 [0] / gw1 [0] / gw2 [0] / gw3 [0]
scheduling tests via LoadScheduling scheduling tests via LoadScheduling
============================= in 0.29 seconds ============================= ============================= in 0.43 seconds =============================
.. _`retrieved by hooks as item keywords`: .. _`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' $ py.test -rs # "-rs" means report details on the little 's'
=========================== test session starts ============================ =========================== 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 collecting ... collected 2 items
test_module.py .s test_module.py .s
========================= short test summary info ========================== ========================= 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 ==================== =================== 1 passed, 1 skipped in 0.02 seconds ====================
@ -227,7 +227,7 @@ Or run it including the ``slow`` marked test::
$ py.test --runslow $ py.test --runslow
=========================== test session starts ============================ =========================== 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 collecting ... collected 2 items
test_module.py .. 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`` to rather use your own application module rather than ``sys``
for handling flag. 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 =============================

View File

@ -1,7 +1,7 @@
""" """
unit and functional testing with Python. unit and functional testing with Python.
""" """
__version__ = '2.0.1.dev7' __version__ = '2.0.1.dev8'
__all__ = ['main'] __all__ = ['main']
from _pytest.core import main, UsageError, _preloadplugins from _pytest.core import main, UsageError, _preloadplugins

View File

@ -22,7 +22,7 @@ def main():
name='pytest', name='pytest',
description='py.test: simple powerful testing with Python', description='py.test: simple powerful testing with Python',
long_description = long_description, long_description = long_description,
version='2.0.1.dev7', version='2.0.1.dev8',
url='http://pytest.org', url='http://pytest.org',
license='MIT license', license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

View File

@ -231,6 +231,8 @@ def test_preparse_ordering_with_setuptools(testdir, monkeypatch):
assert name == "pytest11" assert name == "pytest11"
class EntryPoint: class EntryPoint:
name = "mytestplugin" name = "mytestplugin"
class dist:
pass
def load(self): def load(self):
class PseudoPlugin: class PseudoPlugin:
x = 42 x = 42

View File

@ -66,6 +66,7 @@ class TestBootstrapping:
assert name == "pytest11" assert name == "pytest11"
class EntryPoint: class EntryPoint:
name = "pytest_mytestplugin" name = "pytest_mytestplugin"
dist = None
def load(self): def load(self):
class PseudoPlugin: class PseudoPlugin:
x = 42 x = 42

View File

@ -1,13 +1,18 @@
import py, pytest,os import py, pytest,os
from _pytest.helpconfig import collectattr from _pytest.helpconfig import collectattr
def test_version(testdir): def test_version(testdir, pytestconfig):
result = testdir.runpytest("--version") result = testdir.runpytest("--version")
assert result.ret == 0 assert result.ret == 0
#p = py.path.local(py.__file__).dirpath() #p = py.path.local(py.__file__).dirpath()
result.stderr.fnmatch_lines([ result.stderr.fnmatch_lines([
'*py.test*%s*imported from*' % (pytest.__version__, ) '*py.test*%s*imported from*' % (pytest.__version__, )
]) ])
if pytestconfig.pluginmanager._plugin_distinfo:
result.stderr.fnmatch_lines([
"*setuptools registered plugins:",
"*at*",
])
def test_help(testdir): def test_help(testdir):
result = testdir.runpytest("--help") result = testdir.runpytest("--help")
@ -51,3 +56,9 @@ def test_hookvalidation_optional(testdir):
result = testdir.runpytest() result = testdir.runpytest()
assert result.ret == 0 assert result.ret == 0
def test_traceconfig(testdir):
result = testdir.runpytest("--traceconfig")
result.stdout.fnmatch_lines([
"*using*pytest*py*",
"*active plugins*",
])