refine printing of exceptions via the pluginmanager.

if there is no pytest_internalerror() hook acknowledging
receival we print the exception to sys.stderr.  This helps
to see issues when there are failures in TerminalReporter
initialization.

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-10-05 17:21:41 +02:00
parent a054b63bac
commit cebcdb83cf
4 changed files with 22 additions and 2 deletions

View File

@ -103,7 +103,6 @@ class TerminalWriter(object):
# XXX deprecate stringio argument
def __init__(self, file=None, stringio=False, encoding=None):
if file is None:
if stringio:
self.stringio = file = py.io.TextIO()

View File

@ -118,6 +118,7 @@ class TerminalReporter:
def pytest_internalerror(self, excrepr):
for line in str(excrepr).split("\n"):
self.write_line("INTERNALERROR> " + line)
return 1
def pytest_plugin_registered(self, plugin):
if self.config.option.traceconfig:

View File

@ -2,6 +2,7 @@
managing loading and interacting with pytest plugins.
"""
import py
import sys
import inspect
from py._plugin import hookspec
@ -163,7 +164,11 @@ class PluginManager(object):
if excinfo is None:
excinfo = py.code.ExceptionInfo()
excrepr = excinfo.getrepr(funcargs=True, showlocals=True)
return self.hook.pytest_internalerror(excrepr=excrepr)
res = self.hook.pytest_internalerror(excrepr=excrepr)
if not py.builtin.any(res):
for line in str(excrepr).split("\n"):
sys.stderr.write("INTERNALERROR> %s\n" %line)
sys.stderr.flush()
def do_addoption(self, parser):
mname = "pytest_addoption"

View File

@ -214,6 +214,21 @@ class TestBootstrapping:
for name in 'xyz', 'pytest_xyz', 'pytest_Xyz', 'Xyz':
impname = canonical_importname(name)
def test_notify_exception(self, capfd):
pp = PluginManager()
excinfo = py.test.raises(ValueError, "raise ValueError(1)")
pp.notify_exception(excinfo)
out, err = capfd.readouterr()
assert "ValueError" in err
class A:
def pytest_internalerror(self, excrepr):
return True
pp.register(A())
pp.notify_exception(excinfo)
out, err = capfd.readouterr()
assert not err
class TestPytestPluginInteractions:
def test_addhooks_conftestplugin(self, testdir):