[svn r63785] further renaming, streamlining the testing machinery and helpers.

--HG--
branch : trunk
This commit is contained in:
hpk 2009-04-07 12:48:57 +02:00
parent 5b31dfe357
commit a37c32783d
12 changed files with 86 additions and 99 deletions

View File

@ -181,7 +181,7 @@ class TestDSession:
session.loop_once(loopstate) session.loop_once(loopstate)
assert loopstate.colitems == [item2] # do not reschedule crash item assert loopstate.colitems == [item2] # do not reschedule crash item
testrep = evrec.getfirstnamed("itemtestreport") testrep = evrec.matchreport(names="itemtestreport")
assert testrep.failed assert testrep.failed
assert testrep.colitem == item1 assert testrep.colitem == item1
assert str(testrep.longrepr).find("crashed") != -1 assert str(testrep.longrepr).find("crashed") != -1
@ -204,9 +204,9 @@ class TestDSession:
session = DSession(item.config) session = DSession(item.config)
evrec = EventRecorder(session.bus) evrec = EventRecorder(session.bus)
session.queueevent("NOPevent", 42) session.queueevent("NOP", 42)
session.loop_once(session._initloopstate([])) session.loop_once(session._initloopstate([]))
assert evrec.getfirstnamed('NOPevent') assert evrec.getcall('NOP')
def runthrough(self, item): def runthrough(self, item):
session = DSession(item.config) session = DSession(item.config)
@ -274,10 +274,10 @@ class TestDSession:
evrec = EventRecorder(session.bus) evrec = EventRecorder(session.bus)
session.queueevent("itemtestreport", run(item, node)) session.queueevent("itemtestreport", run(item, node))
session.loop_once(loopstate) session.loop_once(loopstate)
assert not evrec.getfirstnamed("testnodedown") assert not evrec.getcalls("testnodedown")
session.queueevent("testnodedown", node, None) session.queueevent("testnodedown", node, None)
session.loop_once(loopstate) session.loop_once(loopstate)
assert evrec.getfirstnamed('testnodedown') == node assert evrec.getcall('testnodedown').node == node
def test_filteritems(self, testdir, EventRecorder): def test_filteritems(self, testdir, EventRecorder):
modcol = testdir.getmodulecol(""" modcol = testdir.getmodulecol("""

View File

@ -104,11 +104,11 @@ class TestNodeManager:
config = py.test.config._reparse([source, '--debug']) config = py.test.config._reparse([source, '--debug'])
assert config.option.debug assert config.option.debug
nodemanager = NodeManager(config, specs) nodemanager = NodeManager(config, specs)
evrec = EventRecorder(config.bus, debug=True) sorter = EventRecorder(config.bus, debug=True)
nodemanager.setup_nodes(putevent=[].append) nodemanager.setup_nodes(putevent=[].append)
for spec in nodemanager.gwmanager.specs: for spec in nodemanager.gwmanager.specs:
l = evrec.getnamed("trace") l = sorter.getcalls("trace")
print evrec.events print sorter.events
assert l assert l
nodemanager.teardown_nodes() nodemanager.teardown_nodes()

View File

@ -61,6 +61,9 @@ class Events:
def pyevent(self, eventname, args, kwargs): def pyevent(self, eventname, args, kwargs):
""" generically called for each notification event. """ """ generically called for each notification event. """
def pyevent__NOP(self, *args, **kwargs):
""" the no-operation call. """
def pyevent__gateway_init(self, gateway): def pyevent__gateway_init(self, gateway):
""" called after a gateway has been initialized. """ """ called after a gateway has been initialized. """

View File

@ -108,9 +108,8 @@ class TestDoctests:
>>> x == 1 >>> x == 1
False False
""") """)
events = testdir.inline_run(p) sorter = testdir.inline_run(p)
ev, = events.getnamed("itemtestreport") sorter.assertoutcome(failed=1)
assert ev.failed
def test_doctest_unexpected_exception(self, testdir): def test_doctest_unexpected_exception(self, testdir):
from py.__.test.outcome import Failed from py.__.test.outcome import Failed
@ -123,11 +122,9 @@ class TestDoctests:
2 2
""") """)
sorter = testdir.inline_run(p) sorter = testdir.inline_run(p)
events = sorter.getnamed("itemtestreport") call = sorter.getcall("itemtestreport")
assert len(events) == 1 assert call.rep.failed
ev, = events assert call.rep.longrepr
assert ev.failed
assert ev.longrepr
# XXX # XXX
#testitem, = items #testitem, = items
#excinfo = py.test.raises(Failed, "testitem.runtest()") #excinfo = py.test.raises(Failed, "testitem.runtest()")
@ -144,9 +141,8 @@ class TestDoctests:
''' '''
""") """)
events = testdir.inline_run(p, "--doctest-modules") sorter = testdir.inline_run(p, "--doctest-modules")
ev, = events.getnamed("itemtestreport") sorter.assertoutcome(failed=1)
assert ev.failed
def test_txtfile_failing(self, testdir): def test_txtfile_failing(self, testdir):
testdir.plugins.append('pytest_doctest') testdir.plugins.append('pytest_doctest')

View File

@ -37,8 +37,7 @@ class TestCapture:
out, err = stdcapture.reset() out, err = stdcapture.reset()
assert out.startswith("42") assert out.startswith("42")
""") """)
ev, = evrec.getnamed("itemtestreport") evrec.assertoutcome(passed=1)
assert ev.passed
def test_stdfd_functional(self, testdir): def test_stdfd_functional(self, testdir):
testdir.plugins.append(IocapturePlugin()) testdir.plugins.append(IocapturePlugin())
@ -49,6 +48,4 @@ class TestCapture:
out, err = stdcapturefd.reset() out, err = stdcapturefd.reset()
assert out.startswith("42") assert out.startswith("42")
""") """)
ev, = evrec.getnamed("itemtestreport") evrec.assertoutcome(passed=1)
assert ev.passed

View File

@ -296,12 +296,19 @@ class EventRecorder(object):
return eventparser(*event.args, **event.kwargs) return eventparser(*event.args, **event.kwargs)
raise KeyError("popevent: %r not found in %r" %(name, self.events)) raise KeyError("popevent: %r not found in %r" %(name, self.events))
def getcalls(self, eventname): def getcall(self, name):
l = self.getcalls(name)
assert len(l) == 1, (name, l)
return l[0]
def getcalls(self, *names):
""" return list of ParsedCall instances matching the given eventname. """ """ return list of ParsedCall instances matching the given eventname. """
method = self._getcallparser(eventname) if len(names) == 1 and isinstance(names, str):
names = names.split()
l = [] l = []
for event in self.events: for event in self.events:
if event.name == eventname: if event.name in names:
method = self._getcallparser(event.name)
pevent = method(*event.args, **event.kwargs) pevent = method(*event.args, **event.kwargs)
l.append(pevent) l.append(pevent)
return l return l
@ -318,35 +325,34 @@ class EventRecorder(object):
exec code exec code
return locals()[mname] return locals()[mname]
def get(self, cls): # functionality for test reports
def getreports(self, names="itemtestreport collectionreport"):
names = names.split()
l = [] l = []
for event in self.events: for call in self.getcalls(*names):
try: l.append(call.rep)
value = event.args[0]
except IndexError:
continue
else:
if isinstance(value, cls):
l.append(value)
return l return l
def getnamed(self, *names): def matchreport(self, inamepart="", names="itemtestreport collectionreport"):
""" return a testreport whose dotted import path matches """
l = [] l = []
for event in self.events: for rep in self.getreports(names=names):
if event.name in names: if not inamepart or inamepart in rep.colitem.listnames():
l.append(event.args[0]) l.append(rep)
return l if not l:
raise ValueError("could not find test report matching %r: no test reports at all!" %
def getfirstnamed(self, name): (inamepart,))
for event in self.events: if len(l) > 1:
if event.name == name: raise ValueError("found more than one testreport matching %r: %s" %(
return event.args[0] inamepart, l))
return l[0]
def getfailures(self, names='itemtestreport collectionreport'): def getfailures(self, names='itemtestreport collectionreport'):
l = [] l = []
for ev in self.getnamed(*names.split()): for call in self.getcalls(*names.split()):
if ev.failed: if call.rep.failed:
l.append(ev) l.append(call.rep)
return l return l
def getfailedcollections(self): def getfailedcollections(self):
@ -356,13 +362,13 @@ class EventRecorder(object):
passed = [] passed = []
skipped = [] skipped = []
failed = [] failed = []
for ev in self.getnamed('itemtestreport'): # , 'collectionreport'): for rep in self.getreports("itemtestreport"):
if ev.passed: if rep.passed:
passed.append(ev) passed.append(rep)
elif ev.skipped: elif rep.skipped:
skipped.append(ev) skipped.append(rep)
elif ev.failed: elif rep.failed:
failed.append(ev) failed.append(rep)
return passed, skipped, failed return passed, skipped, failed
def countoutcomes(self): def countoutcomes(self):
@ -374,21 +380,6 @@ class EventRecorder(object):
assert skipped == len(realskipped) assert skipped == len(realskipped)
assert failed == len(realfailed) assert failed == len(realfailed)
def getreport(self, inamepart):
""" return a testreport whose dotted import path matches """
__tracebackhide__ = True
l = []
for rep in self.get(runner.ItemTestReport):
if inamepart in rep.colitem.listnames():
l.append(rep)
if not l:
raise ValueError("could not find test report matching %r: no test reports at all!" %
(inamepart,))
if len(l) > 1:
raise ValueError("found more than one testreport matching %r: %s" %(
inamepart, l))
return l[0]
def clear(self): def clear(self):
self.events[:] = [] self.events[:] = []
@ -407,9 +398,7 @@ def test_eventrecorder():
bus.notify("itemtestreport", rep) bus.notify("itemtestreport", rep)
failures = recorder.getfailures() failures = recorder.getfailures()
assert failures == [rep] assert failures == [rep]
failures = recorder.get(runner.ItemTestReport) failures = recorder.getfailures()
assert failures == [rep]
failures = recorder.getnamed("itemtestreport")
assert failures == [rep] assert failures == [rep]
rep = runner.ItemTestReport(None, None) rep = runner.ItemTestReport(None, None)

View File

@ -84,8 +84,8 @@ def test_simple_unittest(testdir):
self.assertEquals('foo', 'bar') self.assertEquals('foo', 'bar')
""") """)
sorter = testdir.inline_run(testpath) sorter = testdir.inline_run(testpath)
assert sorter.getreport("testpassing").passed assert sorter.matchreport("testpassing").passed
assert sorter.getreport("test_failing").failed assert sorter.matchreport("test_failing").failed
def test_setup(testdir): def test_setup(testdir):
testpath = testdir.makepyfile(test_two=""" testpath = testdir.makepyfile(test_two="""
@ -98,7 +98,7 @@ def test_setup(testdir):
self.assertEquals(1, self.foo) self.assertEquals(1, self.foo)
""") """)
sorter = testdir.inline_run(testpath) sorter = testdir.inline_run(testpath)
rep = sorter.getreport("test_setUp") rep = sorter.matchreport("test_setUp")
assert rep.passed assert rep.passed
def test_teardown(testdir): def test_teardown(testdir):

View File

@ -98,6 +98,7 @@ class BaseReport(object):
else: else:
out.line(str(longrepr)) out.line(str(longrepr))
# XXX rename to runtest() report ?
class ItemTestReport(BaseReport): class ItemTestReport(BaseReport):
""" Test Execution Report. """ """ Test Execution Report. """
failed = passed = skipped = False failed = passed = skipped = False
@ -138,11 +139,13 @@ class ItemTestReport(BaseReport):
self.longrepr = longrepr self.longrepr = longrepr
# XXX rename to collectreport
class CollectionReport(BaseReport): class CollectionReport(BaseReport):
""" Collection Report. """ """ Collection Report. """
skipped = failed = passed = False skipped = failed = passed = False
def __init__(self, colitem, result, excinfo=None, outerr=None): def __init__(self, colitem, result, excinfo=None, outerr=None):
# XXX rename to collector
self.colitem = colitem self.colitem = colitem
if not excinfo: if not excinfo:
self.passed = True self.passed = True

View File

@ -163,11 +163,11 @@ class TestCollectPluginHooks:
testdir.plugins.append(Plugin()) testdir.plugins.append(Plugin())
testdir.mkdir("hello") testdir.mkdir("hello")
testdir.mkdir("world") testdir.mkdir("world")
evrec = testdir.inline_run() sorter = testdir.inline_run()
assert "hello" in wascalled assert "hello" in wascalled
assert "world" in wascalled assert "world" in wascalled
# make sure the directories do not get double-appended # make sure the directories do not get double-appended
colreports = evrec.getnamed("collectionreport") colreports = sorter.getreports(names="collectionreport")
names = [rep.colitem.name for rep in colreports] names = [rep.colitem.name for rep in colreports]
assert names.count("hello") == 1 assert names.count("hello") == 1
@ -206,11 +206,11 @@ class TestCustomConftests:
return parent.config.getvalue("XX") return parent.config.getvalue("XX")
""") """)
testdir.mkdir("hello") testdir.mkdir("hello")
evrec = testdir.inline_run(testdir.tmpdir) sorter = testdir.inline_run(testdir.tmpdir)
names = [rep.colitem.name for rep in evrec.getnamed("collectionreport")] names = [rep.colitem.name for rep in sorter.getreports("collectionreport")]
assert 'hello' not in names assert 'hello' not in names
evrec = testdir.inline_run(testdir.tmpdir, "--XX") evrec = testdir.inline_run(testdir.tmpdir, "--XX")
names = [rep.colitem.name for rep in evrec.getnamed("collectionreport")] names = [rep.colitem.name for rep in evrec.getreports("collectionreport")]
assert 'hello' in names assert 'hello' in names
class TestCollectorReprs: class TestCollectorReprs:

View File

@ -74,18 +74,17 @@ class TestBootstrapping:
mod.pytest_plugins = "pytest_a" mod.pytest_plugins = "pytest_a"
aplugin = testdir.makepyfile(pytest_a="""class APlugin: pass""") aplugin = testdir.makepyfile(pytest_a="""class APlugin: pass""")
plugins = PytestPlugins() plugins = PytestPlugins()
evrec = EventRecorder(plugins) sorter = EventRecorder(plugins)
#syspath.prepend(aplugin.dirpath()) #syspath.prepend(aplugin.dirpath())
py.std.sys.path.insert(0, str(aplugin.dirpath())) py.std.sys.path.insert(0, str(aplugin.dirpath()))
plugins.consider_module(mod) plugins.consider_module(mod)
evlist = evrec.getnamed("plugin_registered") call = sorter.getcall("plugin_registered")
assert len(evlist) == 1 assert call.plugin.__class__.__name__ == "APlugin"
assert evlist[0].__class__.__name__ == "APlugin"
# check that it is not registered twice # check that it is not registered twice
plugins.consider_module(mod) plugins.consider_module(mod)
evlist = evrec.getnamed("plugin_registered") l = sorter.getcalls("plugin_registered")
assert len(evlist) == 1 assert len(l) == 1
def test_consider_conftest(self, testdir): def test_consider_conftest(self, testdir):
pp = PytestPlugins() pp = PytestPlugins()

View File

@ -130,9 +130,9 @@ class SessionTests:
def test_one(): pass def test_one(): pass
""") """)
sorter = testdir.inline_run(testdir.tmpdir) sorter = testdir.inline_run(testdir.tmpdir)
skips = sorter.getnamed("collectionreport") reports = sorter.getreports("collectionreport")
assert len(skips) == 1 assert len(reports) == 1
assert skips[0].skipped assert reports[0].skipped
class TestNewSession(SessionTests): class TestNewSession(SessionTests):
def test_pdb_run(self, testdir, monkeypatch): def test_pdb_run(self, testdir, monkeypatch):
@ -146,7 +146,7 @@ class TestNewSession(SessionTests):
l.append(args) l.append(args)
monkeypatch.setattr(py.__.test.custompdb, 'post_mortem', mypdb) monkeypatch.setattr(py.__.test.custompdb, 'post_mortem', mypdb)
sorter = testdir.inline_run('--pdb', tfile) sorter = testdir.inline_run('--pdb', tfile)
rep = sorter.getreport("test_usepdb") rep = sorter.matchreport("test_usepdb")
assert rep.failed assert rep.failed
assert len(l) == 1 assert len(l) == 1
tb = py.code.Traceback(l[0][0]) tb = py.code.Traceback(l[0][0])
@ -199,11 +199,11 @@ class TestNewSession(SessionTests):
) )
sorter = testdir.inline_run('--collectonly', p.dirpath()) sorter = testdir.inline_run('--collectonly', p.dirpath())
itemstarted = sorter.getnamed("itemstart") itemstarted = sorter.getcalls("itemstart")
assert len(itemstarted) == 3 assert len(itemstarted) == 3
assert not sorter.getnamed("itemtestreport") assert not sorter.getreports("itemtestreport")
started = sorter.getcalls("collectionstart") started = sorter.getcalls("collectionstart")
finished = sorter.getnamed("collectionreport") finished = sorter.getreports("collectionreport")
assert len(started) == len(finished) assert len(started) == len(finished)
assert len(started) == 8 assert len(started) == 8
colfail = [x for x in finished if x.failed] colfail = [x for x in finished if x.failed]
@ -215,7 +215,7 @@ class TestNewSession(SessionTests):
testdir.makepyfile(__init__="") testdir.makepyfile(__init__="")
testdir.makepyfile(test_one="xxxx", test_two="yyyy") testdir.makepyfile(test_one="xxxx", test_two="yyyy")
sorter = testdir.inline_run("-x", testdir.tmpdir) sorter = testdir.inline_run("-x", testdir.tmpdir)
finished = sorter.getnamed("collectionreport") finished = sorter.getreports("collectionreport")
colfail = [x for x in finished if x.failed] colfail = [x for x in finished if x.failed]
assert len(colfail) == 1 assert len(colfail) == 1

View File

@ -27,9 +27,9 @@ def test_module_and_function_setup(testdir):
assert modlevel[0] == 42 assert modlevel[0] == 42
assert not hasattr(test_modlevel, 'answer') assert not hasattr(test_modlevel, 'answer')
""") """)
rep = sorter.getreport("test_modlevel") rep = sorter.matchreport("test_modlevel")
assert rep.passed assert rep.passed
rep = sorter.getreport("test_module") rep = sorter.matchreport("test_module")
assert rep.passed assert rep.passed
def test_class_setup(testdir): def test_class_setup(testdir):
@ -118,7 +118,7 @@ def test_func_generator_setup(testdir):
yield check yield check
assert x == [1] assert x == [1]
""") """)
rep = sorter.getreport("test_one") rep = sorter.matchreport("test_one", names="itemtestreport")
assert rep.passed assert rep.passed
def test_method_setup_uses_fresh_instances(testdir): def test_method_setup_uses_fresh_instances(testdir):