def test_default(self): byte_stream = BytesIO() stream = StreamResultToBytes(byte_stream) stream.status(test_id="foo", test_status="inprogress") stream.status(test_id="foo", test_status="skip") output = self.run_command([], byte_stream.getvalue()) events = StreamResult() ByteStreamToStreamResult(BytesIO(output)).run(events) ids = set(event[1] for event in events._events) self.assertEqual([ ('status', 'foo', 'inprogress'), ('status', 'foo', 'skip'), ], [event[:3] for event in events._events])
def _list(self, test): test_ids, errors = list_test(test) try: fileno = self.stream.fileno() except: fileno = None if fileno is not None: stream = os.fdopen(fileno, "wb", 0) else: stream = self.stream result = StreamResultToBytes(stream) for test_id in test_ids: result.status(test_id=test_id, test_status="exists") return result, errors
def _list(self, test): test_ids, errors = list_test(test) try: fileno = self.stream.fileno() except: fileno = None if fileno is not None: stream = os.fdopen(fileno, 'wb', 0) else: stream = self.stream result = StreamResultToBytes(stream) for test_id in test_ids: result.status(test_id=test_id, test_status='exists') return result, errors
def subunit2junitxml(ctr): from testtools import ExtendedToStreamDecorator from testtools import StreamToExtendedDecorator from subunit import StreamResultToBytes from subunit.filters import filter_by_result from subunit.filters import run_tests_from_stream from spyne.util.six import BytesIO from junitxml import JUnitXmlResult sys.argv = ['subunit-1to2'] subunit1_file_name = 'test_result.%d.subunit' % ctr subunit2 = BytesIO() run_tests_from_stream(open(subunit1_file_name, 'rb'), ExtendedToStreamDecorator( StreamResultToBytes(subunit2))) subunit2.seek(0) sys.argv = ['subunit2junitxml'] sys.stdin = subunit2 def f(output): return StreamToExtendedDecorator(JUnitXmlResult(output)) junit_file_name = 'test_result.%d.xml' % ctr filter_by_result(f, junit_file_name, True, False, protocol_version=2, passthrough_subunit=True, input_stream=subunit2)
def __init__(self, reporter): TerminalReporter.__init__(self, reporter.config) self.writer = self._tw self.tests_count = 0 self.reports = [] self.skipped = [] self.failed = [] self.result = StreamResultToBytes(self.writer._file)
def _construct_subunit(**kwargs): from subunit import StreamResultToBytes stream = kwargs.pop('stream') failfast = kwargs.pop('failfast') _raise_on_unknown_kwargs(kwargs) result_object = LoggedTestResultDecorator( ExtendedToStreamDecorator(StreamResultToBytes(stream))) result_object.failfast = failfast return result_object
def run_tests_from_stream(input_stream, result, passthrough_stream=None, forward_stream=None, protocol_version=1, passthrough_subunit=True): """Run tests from a subunit input stream through 'result'. Non-test events - top level file attachments - are expected to be dropped by v2 StreamResults at the present time (as all the analysis code is in ExtendedTestResult API's), so to implement passthrough_stream they are diverted and copied directly when that is set. :param input_stream: A stream containing subunit input. :param result: A TestResult that will receive the test events. NB: This should be an ExtendedTestResult for v1 and a StreamResult for v2. :param passthrough_stream: All non-subunit input received will be sent to this stream. If not provided, uses the ``TestProtocolServer`` default, which is ``sys.stdout``. :param forward_stream: All subunit input received will be forwarded to this stream. If not provided, uses the ``TestProtocolServer`` default, which is to not forward any input. Do not set this when transforming the stream - items would be double-reported. :param protocol_version: What version of the subunit protocol to expect. :param passthrough_subunit: If True, passthrough should be as subunit otherwise unwrap it. Only has effect when forward_stream is None. (when forwarding as subunit non-subunit input is always turned into subunit) """ if 1 == protocol_version: test = ProtocolTestCase(input_stream, passthrough=passthrough_stream, forward=forward_stream) elif 2 == protocol_version: # In all cases we encapsulate unknown inputs. if forward_stream is not None: # Send events to forward_stream as subunit. forward_result = StreamResultToBytes(forward_stream) # If we're passing non-subunit through, copy: if passthrough_stream is None: # Not passing non-test events - split them off to nothing. router = StreamResultRouter(forward_result) router.add_rule(StreamResult(), 'test_id', test_id=None) result = CopyStreamResult([router, result]) else: # otherwise, copy all events to forward_result result = CopyStreamResult([forward_result, result]) elif passthrough_stream is not None: if not passthrough_subunit: # Route non-test events to passthrough_stream, unwrapping them for # display. passthrough_result = CatFiles(passthrough_stream) else: passthrough_result = StreamResultToBytes(passthrough_stream) result = StreamResultRouter(result) result.add_rule(passthrough_result, 'test_id', test_id=None) test = ByteStreamToStreamResult(input_stream, non_subunit_name='stdout') else: raise Exception("Unknown protocol version.") result.startTestRun() test.run(result) result.stopTestRun()
class SubunitTerminalReporter(TerminalReporter): def __init__(self, reporter): TerminalReporter.__init__(self, reporter.config) self.writer = self._tw self.tests_count = 0 self.reports = [] self.skipped = [] self.failed = [] self.result = StreamResultToBytes(self.writer._file) def _status(self, report, status): # task id test_id = report.nodeid # get time now = datetime.datetime.now(utc) # capture output writer=py.io.TerminalWriter(stringio=True) try: report.toterminal(writer) except: pass writer.stringio.seek(0) out = writer.stringio.read() out = str(out) # send status self.result.status(test_id=test_id, test_status=status, timestamp=now, file_name=report.fspath, file_bytes=out, mime_type="text/plain; charset=utf8") def pytest_collectreport(self, report): pass def pytest_collection_finish(self, session): if self.config.option.collectonly: self._printcollecteditems(session.items) def pytest_collection(self): # Prevent shoving `collecting` message pass def report_collect(self, final=False): # Prevent shoving `collecting` message pass def pytest_sessionstart(self, session): pass def pytest_runtest_logstart(self, nodeid, location): pass def pytest_sessionfinish(self, session, exitstatus): # always exit with exitcode 0 session.exitstatus = 0 def pytest_runtest_logreport(self, report): self.reports.append(report) test_id = report.nodeid if report.when in ['setup', 'session']: self._status(report, 'exists') if report.outcome == 'passed': self._status(report, 'inprogress') if report.outcome == 'failed': self._status(report, 'fail') elif report.when in ['call']: if hasattr(report, "wasxfail"): if report.skipped: self._status(report, 'xfail') elif report.failed: self._status(report, 'uxsuccess') elif report.outcome == 'failed': self._status(report, 'fail') self.failed.append(test_id) elif report.outcome == 'skipped': self._status(report, 'skip') self.skipped.append(test_id) elif report.when in ['teardown']: if test_id not in self.skipped and test_id not in self.failed: if report.outcome == 'passed': self._status(report, 'success') elif report.outcome == 'failed': self._status(report, 'fail') else: raise Exception(str(report)) def _printcollecteditems(self, items): for item in items: test_id = item.nodeid self.result.status(test_id=test_id, test_status='exists') def summary_stats(self): pass def summary_failures(self): # Prevent failure summary from being shown since we already # show the failure instantly after failure has occured. pass def summary_errors(self): # Prevent error summary from being shown since we already # show the error instantly after error has occured. pass
def test_passthrough(self): output = self.run_command([], b'hi thar') byte_stream = BytesIO() stream = StreamResultToBytes(byte_stream) stream.status(file_name="stdout", file_bytes=b'hi thar') self.assertEqual(byte_stream.getvalue(), output)
def test_tags(self): byte_stream = BytesIO() stream = StreamResultToBytes(byte_stream) stream.status( test_id="foo", test_status="inprogress", test_tags=set(["a"])) stream.status( test_id="foo", test_status="success", test_tags=set(["a"])) stream.status(test_id="bar", test_status="inprogress") stream.status(test_id="bar", test_status="inprogress") stream.status( test_id="baz", test_status="inprogress", test_tags=set(["a"])) stream.status( test_id="baz", test_status="success", test_tags=set(["a"])) output = self.run_command( ['-s', '--with-tag', 'a'], byte_stream.getvalue()) events = StreamResult() ByteStreamToStreamResult(BytesIO(output)).run(events) ids = set(event[1] for event in events._events) self.assertEqual(set(['foo', 'baz']), ids)
def test_tags(self): byte_stream = BytesIO() stream = StreamResultToBytes(byte_stream) stream.status(test_id="foo", test_status="inprogress", test_tags=set(["a"])) stream.status(test_id="foo", test_status="success", test_tags=set(["a"])) stream.status(test_id="bar", test_status="inprogress") stream.status(test_id="bar", test_status="inprogress") stream.status(test_id="baz", test_status="inprogress", test_tags=set(["a"])) stream.status(test_id="baz", test_status="success", test_tags=set(["a"])) output = self.run_command(['-s', '--with-tag', 'a'], byte_stream.getvalue()) events = StreamResult() ByteStreamToStreamResult(BytesIO(output)).run(events) ids = set(event[1] for event in events._events) self.assertEqual(set(['foo', 'baz']), ids)