[py-svn] r51679 - in py/branch/event/py/test2: . testing
hpk at codespeak.net
hpk at codespeak.net
Wed Feb 20 11:51:10 CET 2008
Author: hpk
Date: Wed Feb 20 11:51:09 2008
New Revision: 51679
Modified:
py/branch/event/py/test2/collect.py
py/branch/event/py/test2/config.py
py/branch/event/py/test2/executor.py
py/branch/event/py/test2/item.py
py/branch/event/py/test2/present.py
py/branch/event/py/test2/testing/test_config.py
py/branch/event/py/test2/testing/test_executor.py
py/branch/event/py/test2/testing/test_session.py
py/branch/event/py/test2/testing/test_setup_nested.py
Log:
refactoring: output capturing is now the responsibility of Executors.
removed the hooks from the collection tree.
output capturing modes can be set by conftests
(maybe that should even take precendence before command line options?)
Modified: py/branch/event/py/test2/collect.py
==============================================================================
--- py/branch/event/py/test2/collect.py (original)
+++ py/branch/event/py/test2/collect.py Wed Feb 20 11:51:09 2008
@@ -148,16 +148,6 @@
def _getsortvalue(self):
return self.name
- _captured_out = _captured_err = None
- def startcapture(self):
- return None # by default collectors don't capture output
-
- def finishcapture(self):
- return None # by default collectors don't capture output
-
- def _getouterr(self):
- return self._captured_out, self._captured_err
-
def _get_collector_trail(self):
""" Shortcut
"""
@@ -348,12 +338,6 @@
res = self.makeitem(name, attr, usefilters=False)
return res
- def startcapture(self):
- self._config._startcapture(self, path=self.fspath)
-
- def finishcapture(self):
- self._config._finishcapture(self)
-
def _getobj(self):
failure = self._stickyfailure
if failure is not None:
Modified: py/branch/event/py/test2/config.py
==============================================================================
--- py/branch/event/py/test2/config.py (original)
+++ py/branch/event/py/test2/config.py Wed Feb 20 11:51:09 2008
@@ -243,23 +243,19 @@
%(chain[0], self.topdir))
return relpath, tuple([x.name for x in chain[1:]])
- def _startcapture(self, colitem, path=None):
- if not self.option.nocapture:
- assert not hasattr(colitem, '_capture')
+ def _getcapture(self, path=None):
+ if self.option.nocapture:
+ iocapture = "no"
+ else:
iocapture = self.getvalue("conf_iocapture", path=path)
- if iocapture == "fd":
- capture = py.io.StdCaptureFD()
- elif iocapture == "sys":
- capture = py.io.StdCapture()
- else:
- raise ValueError("unknown io capturing: " + iocapture)
- colitem._capture = capture
-
- def _finishcapture(self, colitem):
- if hasattr(colitem, '_capture'):
- capture = colitem._capture
- del colitem._capture
- colitem._captured_out, colitem._captured_err = capture.reset()
+ if iocapture == "fd":
+ return py.io.StdCaptureFD()
+ elif iocapture == "sys":
+ return py.io.StdCapture()
+ elif iocapture == "no":
+ return py.io.StdCapture(out=False, err=False, in_=False)
+ else:
+ raise ValueError("unknown io capturing: " + iocapture)
# this is the one per-process instance of py.test2 configuration
config_per_process = Config()
Modified: py/branch/event/py/test2/executor.py
==============================================================================
--- py/branch/event/py/test2/executor.py (original)
+++ py/branch/event/py/test2/executor.py Wed Feb 20 11:51:09 2008
@@ -17,23 +17,25 @@
def __init__(self, item, config):
self.item = item
self.config = config
+ self.testrep = repevent.ItemTestReport(item._get_collector_trail())
def setup_and_run(self):
testrep = self.execute()
self.config.bus.notify(testrep)
return testrep
- def run(self, capture=True):
- if capture:
- self.item.startcapture()
- try:
- self.item.run()
- finally:
- self.item.finishcapture()
- else:
+ def run(self):
+ capture = self.config._getcapture(path=self.item.fspath)
+ try:
self.item.run()
+ finally:
+ outerr = capture.reset()
+ self.testrep.iocapture_run = outerr
+ # XXX
+ self.item._run_capture = outerr
- def _fillreport(self, testrep, excinfo):
+ def _fillreport(self, excinfo):
+ testrep = self.testrep
if excinfo.errisinstance(Skipped):
testrep.skipped = True
else:
@@ -41,13 +43,9 @@
testrep.exconly = excinfo.exconly()
testrep.repr_failure = self.item.repr_failure(excinfo)
- def execute(self, capture=True):
- testrep = repevent.ItemTestReport(self.item._get_collector_trail())
+ def execute(self):
try:
- try:
- self.run(capture)
- finally:
- testrep.stdout, testrep.stderr = self.item._getouterr()
+ self.run()
except sysex:
raise
except:
@@ -55,19 +53,19 @@
if excinfo.errisinstance(Failed) and excinfo.value.excinfo:
excinfo = e.excinfo
self.config.bus.notify(repevent.ItemFinish(self.item, excinfo))
- self._fillreport(testrep, excinfo)
+ self._fillreport(excinfo)
if self.config.option.usepdb and not excinfo.errisinstance(Skipped):
py.__.test2.custompdb.post_mortem(excinfo._excinfo[2])
else:
- testrep.passed = True
- return testrep
+ self.testrep.passed = True
+ return self.testrep
class BoxExecutor(RunExecutor):
""" Same as RunExecutor, but boxes test instead
"""
def fun(self):
- testrep = RunExecutor.execute(self, capture=False)
- return testrep.dumps() # XXX self.config.option.tbstyle
+ testrep = RunExecutor.execute(self)
+ return testrep.dumps()
def execute(self):
b = Box(self.fun, config=self.config)
@@ -108,7 +106,7 @@
super(ApigenExecutor, self).__init__(item, config)
self.tracer = tracer
- def run(self, capture):
+ def run(self):
# only trace Python Function items
if hasattr(self.item, 'obj') and isinstance(self.item, py.test2.collect.Function):
orig_exec = self.item.execute
@@ -120,7 +118,7 @@
self.tracer.end_tracing()
self.item.execute = traced_exec
try:
- super(ApigenExecutor, self).run(capture)
+ super(ApigenExecutor, self).run()
finally:
self.item.execute = orig_exec
Modified: py/branch/event/py/test2/item.py
==============================================================================
--- py/branch/event/py/test2/item.py (original)
+++ py/branch/event/py/test2/item.py Wed Feb 20 11:51:09 2008
@@ -35,12 +35,6 @@
executor = getexecutor(self, self._config)
return executor.setup_and_run()
- def startcapture(self):
- self._config._startcapture(self, path=self.fspath)
-
- def finishcapture(self):
- self._config._finishcapture(self)
-
class Function(FunctionMixin, Item):
""" a Function Item is responsible for setting up
and executing a Python callable test object.
@@ -73,5 +67,3 @@
p.out.line(line)
p.repr_tb(self, excinfo)
return p.stringio.getvalue()
-
-
Modified: py/branch/event/py/test2/present.py
==============================================================================
--- py/branch/event/py/test2/present.py (original)
+++ py/branch/event/py/test2/present.py Wed Feb 20 11:51:09 2008
@@ -130,7 +130,8 @@
def repr_out_err(self, colitem):
for parent in colitem.listchain():
- for name, obj in zip(['out', 'err'], parent._getouterr()):
+ if hasattr(parent, '_run_capture'):
+ name, obj = parent._run_capture
if obj:
self.out.sep("- ", "%s: recorded std%s" % (parent.name, name))
self.out.line(obj)
Modified: py/branch/event/py/test2/testing/test_config.py
==============================================================================
--- py/branch/event/py/test2/testing/test_config.py (original)
+++ py/branch/event/py/test2/testing/test_config.py Wed Feb 20 11:51:09 2008
@@ -294,28 +294,26 @@
assert pl == [py.path.local()]
def test_config_iocapturing(self):
- self.tmpdir
config = py.test2.config._reparse([self.tmpdir])
assert config.getvalue("conf_iocapture")
tmpdir = self.tmpdir.ensure("sub-with-conftest", dir=1)
tmpdir.join("conftest.py").write(py.code.Source("""
- conf_iocapture = "sys"
+ conf_iocapture = "no"
"""))
config = py.test2.config._reparse([tmpdir])
- assert config.getvalue("conf_iocapture") == "sys"
- class dummy: pass
- config._startcapture(dummy)
- print 42
- py.std.os.write(1, "23")
- config._finishcapture(dummy)
- assert dummy._captured_out.strip() == "42"
-
- config = py.test2.config._reparse([tmpdir.dirpath()])
- config._startcapture(dummy, path=tmpdir)
- print 42
- py.std.os.write(1, "23")
- config._finishcapture(dummy)
- assert dummy._captured_out.strip() == "42"
+ assert config.getvalue("conf_iocapture") == "no"
+ capture = config._getcapture()
+ assert isinstance(capture, py.io.StdCapture)
+ assert not capture._out
+ assert not capture._err
+ assert not capture._in
+ assert isinstance(capture, py.io.StdCapture)
+ for opt, cls in (("sys", py.io.StdCapture),
+ ("fd", py.io.StdCaptureFD),
+ ):
+ config.option.conf_iocapture = opt
+ capture = config._getcapture()
+ assert isinstance(capture, cls)
def test_conflict_options(self):
def check_conflict_option(opts):
Modified: py/branch/event/py/test2/testing/test_executor.py
==============================================================================
--- py/branch/event/py/test2/testing/test_executor.py (original)
+++ py/branch/event/py/test2/testing/test_executor.py Wed Feb 20 11:51:09 2008
@@ -32,20 +32,20 @@
def test_run_executor_capture_stdout(self):
testrep = self.exrun("print")
- assert testrep.stdout == "samfing\n"
- assert not testrep.stderr
+ assert testrep.iocapture_run[0] == "samfing\n"
+ assert not testrep.iocapture_run[1]
def test_run_executor_capture_stderr(self):
testrep = self.exrun("printerr")
- assert testrep.stderr == "samfing\n"
- assert not testrep.stdout
+ assert testrep.iocapture_run[1] == "samfing\n"
+ assert not testrep.iocapture_run[0]
def test_box_executor_printfailing(self):
testrep = self.exrun("printfail")
assert not testrep.passed
assert testrep.failed
- assert testrep.stdout.find("samfing elz") != -1
- assert not testrep.stderr
+ assert testrep.iocapture_run[0].find("samfing elz") != -1
+ assert not testrep.iocapture_run[1]
def test_executor_explicit_Failed(self):
testrep = self.exrun("explicitfail")
Modified: py/branch/event/py/test2/testing/test_session.py
==============================================================================
--- py/branch/event/py/test2/testing/test_session.py (original)
+++ py/branch/event/py/test2/testing/test_session.py Wed Feb 20 11:51:09 2008
@@ -88,7 +88,7 @@
i = out.find('TypeError')
assert i != -1
- def test_conftest_Function_capturing_hooks(self):
+ def test_Function_capturing(self):
tfile = suptest.makeuniquepyfile("""
import py
print "module level output"
@@ -100,23 +100,15 @@
print >>py.std.sys.stderr, 2
raise ValueError
""")
- conftest = tfile.dirpath('conftest.py').write(py.code.Source("""
- import py
- class Function(py.test2.collect.Function):
- def startcapture(self):
- self._mycapture = True
-
- def finishcapture(self):
- self._testmycapture = True
- """))
sorter = suptest.events_from_cmdline([tfile.dirpath()])
passed, skipped, failed = sorter.listoutcomes()
assert len(passed) == 1
assert len(failed) == 1
- for ev in sorter.get(repevent.ItemFinish):
- assert ev.item._mycapture
- assert ev.item._testmycapture
+ ev_list = sorter.get(repevent.ItemTestReport)
+ ev1, ev2 = ev_list
+ assert ev1.iocapture_run == ("42\n", "23\n")
+ assert ev2.iocapture_run == ("1\n", "2\n")
def test_raises_output(self):
sorter = suptest.events_from_runsource('''
@@ -260,12 +252,12 @@
assert 0
""")
testrep = sorter.getreport("test_one")
- assert testrep.stdout.startswith("passed")
- assert not testrep.stderr
+ assert testrep.iocapture_run[0].startswith("passed")
+ assert not testrep.iocapture_run[1]
testrep = sorter.getreport("test_two")
- assert testrep.stdout.startswith("failed")
- assert not testrep.stderr
+ assert testrep.iocapture_run[0].startswith("failed")
+ assert not testrep.iocapture_run[1]
def test_function_repr_failure(self):
o = setupdata.getexamplefile("filetest.py")
Modified: py/branch/event/py/test2/testing/test_setup_nested.py
==============================================================================
--- py/branch/event/py/test2/testing/test_setup_nested.py (original)
+++ py/branch/event/py/test2/testing/test_setup_nested.py Wed Feb 20 11:51:09 2008
@@ -123,6 +123,6 @@
""")
rep = sorter.getreport("test_one")
assert rep.passed
- assert rep.stdout == "check\n"
- assert rep.stderr == "e\n"
+ assert rep.iocapture_run[0] == "check\n"
+ assert rep.iocapture_run[1] == "e\n"
More information about the pytest-commit
mailing list