[py-svn] r34859 - in py/dist/py/test/rsession: . testing
guido at codespeak.net
guido at codespeak.net
Wed Nov 22 14:30:54 CET 2006
Author: guido
Date: Wed Nov 22 14:30:53 2006
New Revision: 34859
Modified:
py/dist/py/test/rsession/reporter.py
py/dist/py/test/rsession/rest.py
py/dist/py/test/rsession/testing/test_rest.py
Log:
Cleanup and improvements of the ReST reporter. More will follow, however
currently executing 'py.test --rest > output.rst' should at least result in a
parsable document with test results and exceptions.
Modified: py/dist/py/test/rsession/reporter.py
==============================================================================
--- py/dist/py/test/rsession/reporter.py (original)
+++ py/dist/py/test/rsession/reporter.py Wed Nov 22 14:30:53 2006
@@ -34,6 +34,9 @@
# XXX: Testing purposes only
return 'localhost'
+ def get_item_name(self, event, colitem):
+ return "/".join(colitem.listnames())
+
def report(self, what):
repfun = getattr(self, "report_" + what.__class__.__name__,
self.report_unknown)
Modified: py/dist/py/test/rsession/rest.py
==============================================================================
--- py/dist/py/test/rsession/rest.py (original)
+++ py/dist/py/test/rsession/rest.py Wed Nov 22 14:30:53 2006
@@ -4,34 +4,54 @@
import py
import sys
+from StringIO import StringIO
from py.__.test.rsession.reporter import AbstractReporter
from py.__.rest.rst import *
class RestReporter(AbstractReporter):
def report_unknown(self, what):
- print "Unknown report: %s" % what
+ self.add_rest(Paragraph("Unknown report: %s" % what))
+
+ def report_SendItem(self, item):
+ address = self.get_host(item)
+ if self.config.option.verbose:
+ self.add_rest(Paragraph('sending item %s to %s' % (item.item,
+ address)))
+
+ def report_HostRSyncing(self, item):
+ self.add_rest(LiteralBlock('%10s: RSYNC ==> %s' % (item.hostname[:10],
+ item.remoterootpath)))
+
+ def report_HostReady(self, item):
+ self.add_rest(LiteralBlock('%10s: READY' % (item.hostname[:10],)))
def report_TestStarted(self, event):
- txt = "Test started, hosts: %s" % ", ".join(event.hosts)
- print Title(txt, abovechar='=', belowchar='=').text() + "\n"
+ txt = "Running tests on hosts: %s" % ", ".join(event.hosts)
+ self.add_rest(Title(txt, abovechar='=', belowchar='='))
self.timestart = event.timestart
+ def report_TestFinished(self, item):
+ self.timeend = item.timeend
+ self.summary()
+
+ def report_ImmediateFailure(self, item):
+ pass
+
def report_ItemStart(self, event):
item = event.item
- print
if isinstance(item, py.test.collect.Module):
lgt = len(list(item.tryiter()))
- name = "/".join(item.listnames())
+ lns = item.listnames()
+ name = "/".join(lns)
txt = 'Testing module %s (%d items)' % (name, lgt)
- print Title(txt, belowchar='-').text()
+ self.add_rest(Title(txt, belowchar='-'))
def print_summary(self, total, skipped_str, failed_str):
- print "\n"
- txt = "%d test run%s%s in %.2fs (rsync: %.2f)" % \
+ txt = "%d tests run%s%s in %.2fs (rsync: %.2f)" % \
(total, skipped_str, failed_str, self.timeend - self.timestart,
self.timersync - self.timestart)
- print Title(txt, belowchar="=").text()
-
+ self.add_rest(Title(txt, belowchar='-'))
+
def report_ReceivedItemOutcome(self, event):
host = self.get_host(event)
if event.outcome.passed:
@@ -46,7 +66,20 @@
self.failed[host] += 1
self.failed_tests_outcome.append(event)
# we'll take care of them later
- sshhost = self.get_host(event)
- itempath = " ".join(event.item.listnames()[1:])
- print ListItem(Text("%10s: %s %s" %(sshhost[:10], status, itempath))).text()
-
+ lns = event.item.listnames()[1:]
+ for i, ln in enumerate(lns):
+ if i > 0 and ln != '()':
+ lns[i] = '/%s' % (ln,)
+ itempath = ''.join(lns)
+ self.add_rest(ListItem(Text("%10s:" % (host[:10],)), Strong(status),
+ Text(itempath)))
+ if event.outcome.excinfo:
+ excinfo = event.outcome.excinfo
+ self.add_rest(Paragraph('exception:', Strong(excinfo.typename)))
+ self.add_rest(LiteralBlock(excinfo.value))
+ self.add_rest(LiteralBlock('\n'.join([str(x) for x in
+ excinfo.traceback])))
+
+ def add_rest(self, item):
+ self.out.write('%s\n\n' % (item.text(),))
+
Modified: py/dist/py/test/rsession/testing/test_rest.py
==============================================================================
--- py/dist/py/test/rsession/testing/test_rest.py (original)
+++ py/dist/py/test/rsession/testing/test_rest.py Wed Nov 22 14:30:53 2006
@@ -5,6 +5,133 @@
import py
from py.__.test.rsession.testing.test_reporter import AbstractTestReporter
from py.__.test.rsession.rest import RestReporter
+from py.__.rest.rst import *
+
+class RestTestReporter(RestReporter):
+ def __init__(self, *args, **kwargs):
+ if args:
+ super(RestReporter, self).__init__(*args, **kwargs)
+
+class Container(object):
+ def __init__(self, **kwargs):
+ self.__dict__.update(kwargs)
+
+class TestRestUnits(object):
+ def setup_method(self, method):
+ config, args = py.test.Config.parse(["some_sub"])
+ config.option.verbose = False
+ method.im_func.func_globals['reporter'] = r = RestReporter(config,
+ ['localhost'])
+ method.im_func.func_globals['stdout'] = s = py.std.StringIO.StringIO()
+ r.out = s # XXX will need to become a real reporter some time perhaps?
+
+ def test_report_unknown(self):
+ reporter.report_unknown('foo')
+ assert stdout.getvalue() == 'Unknown report\\: foo\n\n'
+
+ def test_report_SendItem(self):
+ item = Container(item='foo/bar.py', channel=False)
+ reporter.report_SendItem(item)
+ assert stdout.getvalue() == ''
+ stdout.seek(0)
+ stdout.truncate()
+ reporter.config.option.verbose = True
+ reporter.report_SendItem(item)
+ assert stdout.getvalue() == ('sending item foo/bar.py to '
+ 'localhost\n\n')
+
+ def test_report_HostRSyncing(self):
+ item = Container(hostname='localhost', remoterootpath='/foo/bar')
+ reporter.report_HostRSyncing(item)
+ assert stdout.getvalue() == ('::\n\n localhost: RSYNC ==> '
+ '/foo/bar\n\n')
+
+ def test_report_HostReady(self):
+ item = Container(hostname='localhost')
+ reporter.report_HostReady(item)
+ assert stdout.getvalue() == '::\n\n localhost: READY\n\n'
+
+ def test_report_TestStarted(self):
+ event = Container(hosts=['localhost', 'foo.com'], timestart=0)
+ reporter.report_TestStarted(event)
+ assert stdout.getvalue() == """\
+===========================================
+Running tests on hosts\: localhost, foo.com
+===========================================
+
+"""
+
+ def test_report_ItemStart(self):
+ class FakeModule(py.test.collect.Module):
+ def __init__(self):
+ pass
+ def tryiter(self):
+ return ['test_foo', 'test_bar']
+ def listnames(self):
+ return ['foo', 'bar.py']
+ event = Container(item=FakeModule())
+ reporter.report_ItemStart(event)
+ assert stdout.getvalue() == """\
+Testing module foo/bar.py (2 items)
+-----------------------------------
+
+"""
+
+ def test_print_summary(self):
+ reporter.timestart = 10
+ reporter.timeend = 20
+ reporter.timersync = 15
+ reporter.print_summary(10, '', '')
+ assert stdout.getvalue() == """\
+10 tests run in 10.00s (rsync\: 5.00)
+-------------------------------------
+
+"""
+
+ def test_ReceivedItemOutcome_PASSED(self):
+ outcome = Container(passed=True, excinfo=False)
+ item = Container(listnames=lambda: ['', 'foo.py', 'bar', '()', 'baz'])
+ event = Container(channel=False, outcome=outcome, item=item)
+ reporter.report_ReceivedItemOutcome(event)
+ assert stdout.getvalue() == ('* localhost\\: **PASSED** '
+ 'foo.py/bar()/baz\n\n')
+
+ def test_ReceivedItemOutcome_SKIPPED(self):
+ outcome = Container(passed=False, skipped=True, excinfo=False)
+ item = Container(listnames=lambda: ['', 'foo.py', 'bar', '()', 'baz'])
+ event = Container(channel=False, outcome=outcome, item=item)
+ reporter.report_ReceivedItemOutcome(event)
+ assert stdout.getvalue() == ('* localhost\\: **SKIPPED** '
+ 'foo.py/bar()/baz\n\n')
+
+ def test_ReceivedItemOutcome_FAILED(self):
+ excinfo = Container(typename='FooError', value='a foo has occurred',
+ traceback=[' in foo in line 1, in foo:',
+ ' bar()',
+ ' in bar in line 4, in bar:',
+ (' raise FooError("a foo has '
+ 'occurred")')])
+ outcome = Container(passed=False, skipped=False, excinfo=excinfo)
+ item = Container(listnames=lambda: ['', 'foo.py', 'bar', '()', 'baz'])
+ event = Container(channel=False, outcome=outcome, item=item)
+ reporter.report_ReceivedItemOutcome(event)
+ assert stdout.getvalue() == """\
+* localhost\: **FAILED** foo.py/bar()/baz
+
+exception\: **FooError**
+
+::
+
+ a foo has occurred
+
+::
+
+ in foo in line 1, in foo:
+ bar()
+ in bar in line 4, in bar:
+ raise FooError("a foo has occurred")
+
+"""
class TestRestReporter(AbstractTestReporter):
reporter = RestReporter
@@ -19,4 +146,29 @@
- localhost\\: FAILED py test rsession testing test\\_slave.py funcpass
- localhost\\: PASSED py test rsession testing test\\_slave.py funcpass
"""
- assert val == expected
+ print repr(val)
+ expected_start = """\
+* localhost\: **FAILED** py/test/rsession/testing/test\_slave.py/funcpass
+
+* localhost\: **SKIPPED** py/test/rsession/testing/test\_slave.py/funcpass
+
+* localhost\: **FAILED** py/test/rsession/testing/test\_slave.py/funcpass
+
+exception\\: **ZeroDivisionError**
+
+::
+
+ integer division or modulo by zero
+
+::
+
+"""
+
+ expected_end = """
+
+* localhost\: **PASSED** py/test/rsession/testing/test\_slave.py/funcpass
+
+"""
+ assert val.startswith(expected_start)
+ assert val.endswith(expected_end)
+
More information about the pytest-commit
mailing list