2009-04-08 03:27:00 +08:00
|
|
|
import py
|
2009-04-08 23:19:46 +08:00
|
|
|
from py.__.test.outcome import Skipped
|
2009-05-23 01:56:05 +08:00
|
|
|
from py.__.test.runner import SetupState
|
2009-04-08 03:27:00 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
py.test.skip("remove me")
|
2009-04-08 03:27:00 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
config._setupstate = SetupState()
|
2009-04-08 03:27:00 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def pytest_unconfigure(config):
|
|
|
|
config._setupstate.teardown_all()
|
|
|
|
|
|
|
|
def pytest_item_setup_and_runtest(item):
|
|
|
|
setupstate = item.config._setupstate
|
|
|
|
|
|
|
|
# setup (and implied teardown of previous items)
|
|
|
|
call = item.config.guardedcall(lambda: setupstate.prepare(item))
|
|
|
|
if call.excinfo:
|
|
|
|
rep = ItemTestReport(item, call.excinfo, call.outerr)
|
|
|
|
item.config.hook.pytest_itemfixturereport(rep=rep)
|
|
|
|
return
|
2009-04-08 03:27:00 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
# runtest
|
|
|
|
call = item.config.guardedcall(lambda: item.runtest())
|
|
|
|
item.config.hook.pytest_item_runtest_finished(
|
|
|
|
item=item, excinfo=call.excinfo, outerr=call.outerr)
|
|
|
|
|
|
|
|
# teardown
|
|
|
|
call = item.config.guardedcall(lambda: setupstate.teardown_exact(item))
|
|
|
|
if call.excinfo:
|
|
|
|
rep = ItemFixtureReport(item, call.excinfo, call.outerr)
|
|
|
|
item.config.hook.pytest_itemfixturereport(rep=rep)
|
|
|
|
|
|
|
|
def pytest_collector_collect(collector):
|
|
|
|
call = item.config.guardedcall(lambda x: collector._memocollect())
|
|
|
|
return CollectReport(collector, res, excinfo, outerr)
|
2009-04-08 03:27:00 +08:00
|
|
|
|
|
|
|
class BaseReport(object):
|
|
|
|
def __repr__(self):
|
|
|
|
l = ["%s=%s" %(key, value)
|
|
|
|
for key, value in self.__dict__.items()]
|
|
|
|
return "<%s %s>" %(self.__class__.__name__, " ".join(l),)
|
|
|
|
|
|
|
|
def toterminal(self, out):
|
|
|
|
longrepr = self.longrepr
|
|
|
|
if hasattr(longrepr, 'toterminal'):
|
|
|
|
longrepr.toterminal(out)
|
|
|
|
else:
|
|
|
|
out.line(str(longrepr))
|
|
|
|
|
|
|
|
class ItemTestReport(BaseReport):
|
|
|
|
failed = passed = skipped = False
|
|
|
|
|
|
|
|
def __init__(self, colitem, excinfo=None, when=None, outerr=None):
|
|
|
|
self.colitem = colitem
|
|
|
|
if colitem and when != "setup":
|
|
|
|
self.keywords = colitem.readkeywords()
|
|
|
|
else:
|
|
|
|
# if we fail during setup it might mean
|
|
|
|
# we are not able to access the underlying object
|
|
|
|
# this might e.g. happen if we are unpickled
|
|
|
|
# and our parent collector did not collect us
|
|
|
|
# (because it e.g. skipped for platform reasons)
|
|
|
|
self.keywords = {}
|
|
|
|
if not excinfo:
|
|
|
|
self.passed = True
|
|
|
|
self.shortrepr = "."
|
|
|
|
else:
|
|
|
|
self.when = when
|
|
|
|
if not isinstance(excinfo, py.code.ExceptionInfo):
|
|
|
|
self.failed = True
|
|
|
|
shortrepr = "?"
|
|
|
|
longrepr = excinfo
|
|
|
|
elif excinfo.errisinstance(Skipped):
|
|
|
|
self.skipped = True
|
|
|
|
shortrepr = "s"
|
|
|
|
longrepr = self.colitem._repr_failure_py(excinfo, outerr)
|
|
|
|
else:
|
|
|
|
self.failed = True
|
|
|
|
shortrepr = self.colitem.shortfailurerepr
|
|
|
|
if self.when == "execute":
|
|
|
|
longrepr = self.colitem.repr_failure(excinfo, outerr)
|
|
|
|
else: # exception in setup or teardown
|
|
|
|
longrepr = self.colitem._repr_failure_py(excinfo, outerr)
|
|
|
|
shortrepr = shortrepr.lower()
|
|
|
|
self.shortrepr = shortrepr
|
|
|
|
self.longrepr = longrepr
|
|
|
|
|
|
|
|
|
|
|
|
class CollectReport(BaseReport):
|
|
|
|
skipped = failed = passed = False
|
|
|
|
|
|
|
|
def __init__(self, colitem, result, excinfo=None, outerr=None):
|
|
|
|
# XXX rename to collector
|
|
|
|
self.colitem = colitem
|
|
|
|
if not excinfo:
|
|
|
|
self.passed = True
|
|
|
|
self.result = result
|
|
|
|
else:
|
|
|
|
self.outerr = outerr
|
|
|
|
self.longrepr = self.colitem._repr_failure_py(excinfo, outerr)
|
|
|
|
if excinfo.errisinstance(Skipped):
|
|
|
|
self.skipped = True
|
|
|
|
self.reason = str(excinfo.value)
|
|
|
|
else:
|
|
|
|
self.failed = True
|
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
class ItemFixtureReport(BaseReport):
|
2009-04-08 03:27:00 +08:00
|
|
|
failed = passed = skipped = False
|
|
|
|
def __init__(self, item, excinfo=None, outerr=None):
|
|
|
|
self.item = item
|
|
|
|
self.outerr = outerr
|
|
|
|
if not excinfo:
|
|
|
|
self.passed = True
|
|
|
|
else:
|
|
|
|
if excinfo.errisinstance(Skipped):
|
|
|
|
self.skipped = True
|
|
|
|
else:
|
|
|
|
self.failed = True
|
|
|
|
self.excrepr = item._repr_failure_py(excinfo, [])
|
|
|
|
|
|
|
|
|
|
|
|
# ===============================================================================
|
|
|
|
#
|
|
|
|
# plugin tests
|
|
|
|
#
|
|
|
|
# ===============================================================================
|
|
|
|
|
|
|
|
def test_generic(plugintester):
|
2009-05-19 05:26:16 +08:00
|
|
|
plugintester.hookcheck()
|
2009-04-08 03:27:00 +08:00
|
|
|
|
|
|
|
class TestSetupState:
|
|
|
|
def test_setup_prepare(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("""
|
|
|
|
def setup_function(function):
|
|
|
|
function.myfunc = function
|
|
|
|
def test_func1():
|
|
|
|
pass
|
|
|
|
def test_func2():
|
|
|
|
pass
|
|
|
|
def teardown_function(function):
|
|
|
|
del function.myfunc
|
|
|
|
""")
|
|
|
|
item1, item2 = modcol.collect()
|
|
|
|
setup = SetupState()
|
|
|
|
setup.prepare(item1)
|
|
|
|
assert item1.obj.myfunc == item1.obj
|
|
|
|
assert not hasattr(item2, 'myfunc')
|
|
|
|
|
|
|
|
setup.prepare(item2)
|
|
|
|
assert item2.obj.myfunc == item2.obj
|
|
|
|
assert not hasattr(item1, 'myfunc')
|
|
|
|
|
|
|
|
def test_setup_teardown_exact(self, testdir):
|
|
|
|
item = testdir.getitem("""
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
def teardown_function(function):
|
|
|
|
function.tear = "down"
|
|
|
|
""")
|
|
|
|
setup = SetupState()
|
|
|
|
setup.prepare(item)
|
|
|
|
setup.teardown_exact(item)
|
|
|
|
assert item.obj.tear == "down"
|
|
|
|
|
|
|
|
def test_setup_teardown_exact(self, testdir):
|
|
|
|
item = testdir.getitem("""
|
|
|
|
def setup_module(mod):
|
|
|
|
mod.x = 1
|
|
|
|
def setup_function(function):
|
|
|
|
function.y = 2
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
def teardown_function(function):
|
|
|
|
del function.y
|
|
|
|
def teardown_module(mod):
|
|
|
|
del mod.x
|
|
|
|
""")
|
|
|
|
setup = SetupState()
|
|
|
|
setup.prepare(item)
|
|
|
|
assert item.obj.y == 2
|
|
|
|
assert item.parent.obj.x == 1
|
|
|
|
setup.teardown_all()
|
|
|
|
assert not hasattr(item.obj, 'y')
|
|
|
|
assert not hasattr(item.parent.obj, 'x')
|
|
|
|
|
|
|
|
class TestRunnerPlugin:
|
|
|
|
def test_pytest_item_setup_and_runtest(self, testdir):
|
|
|
|
item = testdir.getitem("""def test_func(): pass""")
|
2009-05-23 01:56:05 +08:00
|
|
|
pytest_configure(item.config)
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.getreportrecorder(item)
|
2009-05-23 01:56:05 +08:00
|
|
|
pytest_item_setup_and_runtest(item)
|
2009-05-21 20:33:09 +08:00
|
|
|
rep = reprec.getcall("pytest_itemtestreport").rep
|
2009-04-08 03:27:00 +08:00
|
|
|
assert rep.passed
|
2009-05-23 01:56:05 +08:00
|
|
|
|
2009-04-08 03:27:00 +08:00
|
|
|
class TestSetupEvents:
|
2009-05-23 01:56:05 +08:00
|
|
|
def pytest_funcarg__runfunc(self, request):
|
|
|
|
testdir = request.getfuncargvalue("testdir")
|
|
|
|
def runfunc(source):
|
|
|
|
item = testdir.getitem(source)
|
|
|
|
reprec = testdir.getreportrecorder(item)
|
|
|
|
pytest_item_setup_and_runtest(item)
|
|
|
|
return reprec
|
|
|
|
return runfunc
|
|
|
|
|
|
|
|
def test_setup_runtest_ok(self, runfunc):
|
|
|
|
reprec = runfunc("""
|
2009-04-08 03:27:00 +08:00
|
|
|
def setup_module(mod):
|
|
|
|
pass
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-05-23 01:56:05 +08:00
|
|
|
assert not reprec.getcalls("pytest_itemfixturereport")
|
2009-04-08 03:27:00 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_setup_fails(self, runfunc):
|
|
|
|
reprec = runfunc("""
|
2009-04-08 03:27:00 +08:00
|
|
|
def setup_module(mod):
|
|
|
|
print "world"
|
|
|
|
raise ValueError(42)
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-05-23 01:56:05 +08:00
|
|
|
rep = reprec.popcall("pytest_itemfixturereport").rep
|
2009-04-08 03:27:00 +08:00
|
|
|
assert rep.failed
|
|
|
|
assert not rep.skipped
|
|
|
|
assert rep.excrepr
|
|
|
|
assert "42" in str(rep.excrepr)
|
|
|
|
assert rep.outerr[0].find("world") != -1
|
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_teardown_fails(self, runfunc):
|
|
|
|
reprec = runfunc("""
|
2009-04-08 03:27:00 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
def teardown_function(func):
|
|
|
|
print "13"
|
|
|
|
raise ValueError(25)
|
|
|
|
""")
|
2009-05-23 01:56:05 +08:00
|
|
|
rep = reprec.popcall("pytest_itemfixturereport").rep
|
2009-04-08 03:27:00 +08:00
|
|
|
assert rep.failed
|
2009-05-23 01:56:05 +08:00
|
|
|
assert rep.item.name == "test_func"
|
2009-04-08 03:27:00 +08:00
|
|
|
assert not rep.passed
|
|
|
|
assert "13" in rep.outerr[0]
|
|
|
|
assert "25" in str(rep.excrepr)
|
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_setup_skips(self, runfunc):
|
|
|
|
reprec = runfunc("""
|
2009-04-08 03:27:00 +08:00
|
|
|
import py
|
|
|
|
def setup_module(mod):
|
|
|
|
py.test.skip("17")
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-05-23 01:56:05 +08:00
|
|
|
rep = reprec.popcall("pytest_itemfixturereport").rep
|
2009-04-08 03:27:00 +08:00
|
|
|
assert not rep.failed
|
|
|
|
assert rep.skipped
|
|
|
|
assert rep.excrepr
|
|
|
|
assert "17" in str(rep.excrepr)
|