def assertOutput(self, template, kind, kwargs=None): """Assert the expected output from a run for a given test. :param template: A string where common strings have been replaced by a keyword so we don't run into pep8 warnings for long lines. :param kind: A string used to select the kind of test. :param kwargs: A dict with more keywords for the template. This allows some tests to add more keywords when they are test specific. """ test = tests.get_case(kind) # Get subunit output (what subprocess produce) stream = StringIO() res = subunit.TestProtocolClient(stream) test.run(res) # Inject it again (what controlling process consumes) receiver = subunit.ProtocolTestCase(StringIO(stream.getvalue())) out = StringIO() res = junitxml.JUnitXmlResult(out) # We don't care about timing here so we always return 0 which # simplifies matching the expected result res._now = lambda: 0.0 res._duration = lambda f: 0.0 expected = tests.expand_template_for_test(template, test, kwargs) res.startTestRun() receiver.run(res) # due to the nature of JUnit XML output, nothing will be written to # the stream until stopTestRun() is called. res.stopTestRun() self.assertEquals(expected, out.getvalue())
def setUp(self): self.io = StringIO() self.protocol = subunit.TestProtocolClient(self.io) self.test = TestTestProtocolClient("test_start_test") self.sample_details = {'something':Content( ContentType('text', 'plain'), lambda:['serialised\nform'])} self.sample_tb_details = dict(self.sample_details) self.sample_tb_details['traceback'] = TracebackContent( subunit.RemoteError(u"boo qux"), self.test)
def do_fork(suite): """Take suite and start up multiple runners by forking (Unix only). :param suite: TestSuite object. :return: An iterable of TestCase-like objects which can each have run(result) called on them to feed tests to result. """ tests = [] test_blocks = partition_tests(suite, concurrency_num) # Clear the tests from the original suite so it doesn't keep them alive suite._tests[:] = [] for process_tests in test_blocks: process_suite = unittest.TestSuite(process_tests) # Also clear each split list so new suite has only reference process_tests[:] = [] c2pread, c2pwrite = os.pipe() pid = os.fork() if pid == 0: try: stream = os.fdopen(c2pwrite, 'wb', 1) os.close(c2pread) # Leave stderr and stdout open so we can see test noise # Close stdin so that the child goes away if it decides to # read from stdin (otherwise its a roulette to see what # child actually gets keystrokes for pdb etc). sys.stdin.close() result = test_results.AutoTimingTestResultDecorator( subunit.TestProtocolClient(stream)) process_suite.run(result) except: # Try and report traceback on stream, but exit with error # even if stream couldn't be created or something else # goes wrong. The traceback is formatted to a string and # written in one go to avoid interleaving lines from # multiple failing children. try: stream.write(traceback.format_exc()) finally: os._exit(1) os._exit(0) else: os.close(c2pwrite) stream = os.fdopen(c2pread, 'rb', 1) test = TestInOtherProcess(stream, pid) tests.append(test) return tests
def assertOutput(self, expected, kind): test = tests.get_case(kind) # Get subunit output (what subprocess produce) stream = StringIO() res = subunit.TestProtocolClient(stream) test.run(res) # Inject it again (what controlling process consumes) receiver = subunit.ProtocolTestCase(StringIO(stream.getvalue())) out = StringIO() text_result = results.TextTestResult(out, verbosity=0) # We don't care about timing here so we always return 0 which # simplifies matching the expected result def zero(atime): return 0.0 text_result._delta_to_float = zero receiver.run(text_result) self.assertEquals(expected, out.getvalue())
def __init__(self, options): if subunit is None: raise Exception("Requires subunit 0.0.5 or better") if content is None: raise Exception("Requires testtools 0.9.2 or better") self.options = options self._stream = sys.stdout self._subunit = subunit.TestProtocolClient(self._stream) # Used to track the last layer that was set up or torn down. Either # None or (layer_name, last_touched_time). self._last_layer = None self.UTC = Utc() # Content types used in the output. self.TRACEBACK_CONTENT_TYPE = content.ContentType( 'text', 'x-traceback', dict(language='python', charset='utf8')) self.PROFILE_CONTENT_TYPE = content.ContentType( 'application', 'x-binary-profile') self.PLAIN_TEXT = content.ContentType('text', 'plain', {'charset': 'utf8'})
def make_result(self, get_id, test_command, previous_run=None): if getattr(self.options, 'subunit', False): if v2_avail: serializer = subunit.StreamResultToBytes(self._stdout) else: serializer = StreamToExtendedDecorator( subunit.TestProtocolClient(self._stdout)) # By pass user transforms - just forward it all, result = serializer # and interpret everything as success. summary = testtools.StreamSummary() summary.startTestRun() summary.stopTestRun() return result, summary else: # Apply user defined transforms. filter_tags = test_command.get_filter_tags() output = CLITestResult(self, get_id, self._stdout, previous_run, filter_tags=filter_tags) summary = output._summary return output, summary
def __init__(self, tree, name, command, fail_quickly=False): super(SubunitTreeStageBuilder, self).__init__(tree, name, command, fail_quickly) self.failed_test = None self.subunit_path = os.path.join( gitroot, "%s.%s.subunit" % (self.tree.tag, self.name)) self.tree.logfiles.append( (self.subunit_path, os.path.basename(self.subunit_path), "text/x-subunit")) self.subunit = open(self.subunit_path, 'w') formatter = subunithelper.PlainFormatter(False, True, {}) clients = [ formatter, subunit.TestProtocolClient(self.subunit), FailureTrackingTestResult(self) ] if fail_quickly: clients.append(AbortingTestResult(self)) self.subunit_server = subunit.TestProtocolServer( testtools.MultiTestResult(*clients), self.subunit) self.buffered = ""
def _subunit_factory(cls, stream): """Return a TestResult attached to the given stream.""" return _RunnableDecorator(subunit.TestProtocolClient(stream))
def printout(self): reporter = subunit.TestProtocolClient(sys.stdout) for binary in self.binaries: for tc in binary.testcases: test = GTestCase(tc, binary) test.run(reporter)
def make_subunit_result(stream): """Make a result that emits a subunit stream.""" return subunit.TestProtocolClient(stream)
def run_with_subunit(self, test): """Run a suite returning the subunit stream.""" stream = StringIO() res = subunit.TestProtocolClient(stream) test.run(res) return res, stream
def setUp(self): self.io = StringIO() self.protocol = subunit.TestProtocolClient(self.io) self.test = TestTestProtocolClient("test_start_test")