2008-08-16 23:26:59 +08:00
|
|
|
"""
|
|
|
|
internal classes for
|
|
|
|
|
|
|
|
* executing test items
|
|
|
|
* running collectors
|
|
|
|
* and generating report events about it
|
|
|
|
"""
|
|
|
|
|
|
|
|
import py, os, sys
|
|
|
|
|
|
|
|
from py.__.test import event
|
2009-02-27 18:18:27 +08:00
|
|
|
from py.__.test.outcome import Exit
|
2009-03-22 03:31:09 +08:00
|
|
|
from py.__.test.dist.mypickle import ImmutablePickler
|
2008-08-16 23:26:59 +08:00
|
|
|
import py.__.test.custompdb
|
|
|
|
|
|
|
|
class RobustRun(object):
|
2009-02-27 18:18:27 +08:00
|
|
|
""" a robust setup/execute/teardown protocol used both for test collectors
|
|
|
|
and test items.
|
|
|
|
"""
|
2008-08-16 23:26:59 +08:00
|
|
|
def __init__(self, colitem, pdb=None):
|
|
|
|
self.colitem = colitem
|
2009-03-18 07:48:07 +08:00
|
|
|
self.getcapture = colitem.config._getcapture
|
2008-08-16 23:26:59 +08:00
|
|
|
self.pdb = pdb
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<%s colitem=%s>" %(self.__class__.__name__, self.colitem)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
""" return result of running setup, execution, teardown procedures. """
|
|
|
|
excinfo = None
|
|
|
|
res = NORESULT
|
|
|
|
capture = self.getcapture()
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
when = "setup"
|
|
|
|
self.setup()
|
|
|
|
try:
|
|
|
|
res = self.execute()
|
|
|
|
finally:
|
|
|
|
when = "teardown"
|
|
|
|
self.teardown()
|
|
|
|
when = "execute"
|
|
|
|
finally:
|
|
|
|
outerr = capture.reset()
|
|
|
|
except (Exit, KeyboardInterrupt):
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
return self.makereport(res, when, excinfo, outerr)
|
|
|
|
|
|
|
|
class ItemRunner(RobustRun):
|
|
|
|
def setup(self):
|
2009-03-18 07:48:07 +08:00
|
|
|
self.colitem.config._setupstate.prepare(self.colitem)
|
2008-08-16 23:26:59 +08:00
|
|
|
def teardown(self):
|
2009-03-18 07:48:07 +08:00
|
|
|
self.colitem.config._setupstate.teardown_exact(self.colitem)
|
2008-08-16 23:26:59 +08:00
|
|
|
def execute(self):
|
2009-02-27 18:18:27 +08:00
|
|
|
#self.colitem.config.pytestplugins.pre_execute(self.colitem)
|
2008-09-02 16:58:14 +08:00
|
|
|
self.colitem.runtest()
|
2009-02-27 18:18:27 +08:00
|
|
|
#self.colitem.config.pytestplugins.post_execute(self.colitem)
|
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
def makereport(self, res, when, excinfo, outerr):
|
2009-03-18 07:48:07 +08:00
|
|
|
testrep = self.colitem.config.pytestplugins.call_firstresult(
|
2009-03-07 02:07:44 +08:00
|
|
|
"pytest_item_makereport", item=self.colitem,
|
|
|
|
excinfo=excinfo, when=when, outerr=outerr)
|
2008-10-07 17:26:56 +08:00
|
|
|
if self.pdb and testrep.failed:
|
|
|
|
tw = py.io.TerminalWriter()
|
|
|
|
testrep.toterminal(tw)
|
|
|
|
self.pdb(excinfo)
|
|
|
|
return testrep
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
class CollectorRunner(RobustRun):
|
|
|
|
def setup(self):
|
|
|
|
pass
|
|
|
|
def teardown(self):
|
|
|
|
pass
|
|
|
|
def execute(self):
|
2008-09-02 16:58:14 +08:00
|
|
|
return self.colitem._memocollect()
|
2008-08-16 23:26:59 +08:00
|
|
|
def makereport(self, res, when, excinfo, outerr):
|
2009-02-27 18:18:27 +08:00
|
|
|
return event.CollectionReport(self.colitem, res, excinfo, when, outerr)
|
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
NORESULT = object()
|
|
|
|
#
|
|
|
|
# public entrypoints / objects
|
|
|
|
#
|
|
|
|
|
|
|
|
def basic_collect_report(item):
|
|
|
|
return CollectorRunner(item).run()
|
|
|
|
|
|
|
|
def basic_run_report(item, pdb=None):
|
|
|
|
return ItemRunner(item, pdb=pdb).run()
|
|
|
|
|
|
|
|
from cPickle import Pickler, Unpickler
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
|
|
|
def forked_run_report(item, pdb=None):
|
|
|
|
EXITSTATUS_TESTEXIT = 4
|
|
|
|
|
|
|
|
ipickle = ImmutablePickler(uneven=0)
|
2009-03-18 07:48:07 +08:00
|
|
|
ipickle.selfmemoize(item.config)
|
2008-08-16 23:26:59 +08:00
|
|
|
def runforked():
|
|
|
|
try:
|
|
|
|
testrep = basic_run_report(item)
|
|
|
|
except (KeyboardInterrupt, Exit):
|
|
|
|
os._exit(EXITSTATUS_TESTEXIT)
|
|
|
|
return ipickle.dumps(testrep)
|
|
|
|
|
2008-08-21 20:12:20 +08:00
|
|
|
ff = py.process.ForkedFunc(runforked)
|
2008-08-16 23:26:59 +08:00
|
|
|
result = ff.waitfinish()
|
|
|
|
if result.retval is not None:
|
|
|
|
return ipickle.loads(result.retval)
|
|
|
|
else:
|
|
|
|
if result.exitstatus == EXITSTATUS_TESTEXIT:
|
|
|
|
raise Exit("forked test item %s raised Exit" %(item,))
|
2009-02-27 18:18:27 +08:00
|
|
|
return report_process_crash(item, result)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def report_process_crash(item, result):
|
2008-08-16 23:26:59 +08:00
|
|
|
path, lineno = item.getfslineno()
|
|
|
|
longrepr = [
|
|
|
|
("X", "CRASHED"),
|
|
|
|
("%s:%s: CRASHED with signal %d" %(path, lineno, result.signal)),
|
|
|
|
]
|
2009-02-27 18:18:27 +08:00
|
|
|
return event.ItemTestReport(item, excinfo=longrepr, when="???")
|