def get_details_and_string(self): """Get a details dict and expected string.""" text1 = lambda: [_b("1\n2\n")] text2 = lambda: [_b("3\n4\n")] bin1 = lambda: [_b("5\n")] details = {'text 1': Content(ContentType('text', 'plain'), text1), 'text 2': Content(ContentType('text', 'strange'), text2), 'bin 1': Content(ContentType('application', 'binary'), bin1)} return (details, "Binary content: bin 1\n" "Text attachment: text 1\n------------\n1\n2\n" "------------\nText attachment: text 2\n------------\n" "3\n4\n------------\n")
def test___eq__(self): content_type = ContentType("foo", "bar") one_chunk = lambda: [_b("bytes")] two_chunk = lambda: [_b("by"), _b("tes")] content1 = Content(content_type, one_chunk) content2 = Content(content_type, one_chunk) content3 = Content(content_type, two_chunk) content4 = Content(content_type, lambda: [_b("by"), _b("te")]) content5 = Content(ContentType("f", "b"), two_chunk) self.assertEqual(content1, content2) self.assertEqual(content1, content3) self.assertNotEqual(content1, content4) self.assertNotEqual(content1, content5)
def test_extended_repr(self): content_type = ContentType('text', 'plain', { 'foo': 'bar', 'baz': 'qux' }) self.assertThat(repr(content_type), Equals('text/plain; baz="qux", foo="bar"'))
def __init__(self, err, test, capture_locals=False): """Create a TracebackContent for ``err``. :param err: An exc_info error tuple. :param test: A test object used to obtain failureException. :param capture_locals: If true, show locals in the traceback. """ if err is None: raise ValueError("err may not be None") exctype, value, tb = err # Skip test runner traceback levels if StackLinesContent.HIDE_INTERNAL_STACK: while tb and '__unittest' in tb.tb_frame.f_globals: tb = tb.tb_next limit = None # Disabled due to https://bugs.launchpad.net/testtools/+bug/1188420 if (False and StackLinesContent.HIDE_INTERNAL_STACK and test.failureException and isinstance(value, test.failureException)): # Skip assert*() traceback levels limit = 0 while tb and not self._is_relevant_tb_level(tb): limit += 1 tb = tb.tb_next stack_lines = list(traceback.TracebackException(exctype, value, tb, limit=limit, capture_locals=capture_locals).format()) content_type = ContentType('text', 'x-traceback', {"language": "python", "charset": "utf8"}) super(TracebackContent, self).__init__( content_type, lambda: [x.encode('utf8') for x in stack_lines])
def test_failure_empty_message(self): self.protocol.lineReceived("failure mcdonalds farm [\n") self.protocol.lineReceived("]\n") details = {} details['traceback'] = Content(ContentType("text", "x-traceback", {'charset': 'utf8'}), lambda:[""]) self.assertFailure(details)
def tearDown(self): """Runs after each test run""" super(TestFromSPEC, self).tearDown() ct = ContentType('application', 'json') # information on test dependencies mentioned in the SPEC self._get_dep_info() # configure default set of information to be reported for any test run # still can figure out why this can't be a loop self.addDetail('spec_info', Content(ct, lambda: [self._jds(self._details['spec_info'])])) self.addDetail('dep_info', Content(ct, lambda: [self._jds(self._details['dep_info'])])) self.addDetail('exec_info', Content(ct, lambda: [self._jds(self._details['exec_info'])])) self.addDetail('env_info', Content(ct, lambda: [self._jds(self._details['env_info'])])) self.addDetail('metric_info', Content(ct, lambda: [self._jds(self._details['metric_info'])])) self.addDetail('output_info', Content(ct, lambda: [self._jds(self._details['output_info'])])) self.addDetail('sys_info', Content(ct, lambda: [self._jds(self._get_system_info())])) # restore environment to its previous state self._restore_environment() # after EVERYTHING is done # remove status var again del os.environ['TESTKRAUT_TESTBED_PATH'] # wipe out testbed if not self._workdir is None: lgr.debug("remove work dir at '%s'" % self._workdir) import shutil shutil.rmtree(self._workdir) self._workdir = None
def check_success_or_xfail(self, as_success, error_message=None): if as_success: self.assertEqual([ ('startTest', self.test), ('addSuccess', self.test), ('stopTest', self.test), ], self.client._events) else: details = {} if error_message is not None: details['traceback'] = Content( ContentType("text", "x-traceback", {'charset': 'utf8'}), lambda: [_b(error_message)]) if isinstance(self.client, ExtendedTestResult): value = details else: if error_message is not None: value = subunit.RemoteError( _u("Text attachment: traceback\n" "------------\n") + _u(error_message) + _u("------------\n")) else: value = subunit.RemoteError() self.assertEqual([ ('startTest', self.test), ('addExpectedFailure', self.test, value), ('stopTest', self.test), ], self.client._events)
def test_success_empty_message(self): self.protocol.lineReceived(_b("success mcdonalds farm [\n")) self.protocol.lineReceived(_b("]\n")) details = {} details['message'] = Content(ContentType("text", "plain"), lambda: [_b("")]) self.assertSuccess(details)
def test___init___None_errors(self): self.assertThat(lambda: Content(None, None), raises_value_error) self.assertThat(lambda: Content(None, lambda: ["traceback"]), raises_value_error) self.assertThat( lambda: Content(ContentType("text", "traceback"), None), raises_value_error)
def check_fail_or_uxsuccess(self, as_fail, error_message=None): details = {} if error_message is not None: details['traceback'] = Content( ContentType("text", "x-traceback", {'charset': 'utf8'}), lambda: [_b(error_message)]) if isinstance(self.client, ExtendedTestResult): value = details else: value = None if as_fail: self.client._events[1] = self.client._events[1][:2] # The value is generated within the extended to original decorator: # todo use the testtools matcher to check on this. self.assertEqual([ ('startTest', self.test), ('addFailure', self.test), ('stopTest', self.test), ], self.client._events) elif value: self.assertEqual([ ('startTest', self.test), ('addUnexpectedSuccess', self.test, value), ('stopTest', self.test), ], self.client._events) else: self.assertEqual([ ('startTest', self.test), ('addUnexpectedSuccess', self.test), ('stopTest', self.test), ], self.client._events)
def test___init___sets_ivars(self): content = StacktraceContent() content_type = ContentType("text", "x-traceback", { "language": "python", "charset": "utf8" }) self.assertEqual(content_type, content.content_type)
def test___init___sets_ivars(self): content = TracebackContent(an_exc_info, self) content_type = ContentType("text", "x-traceback", {"language": "python", "charset": "utf8"}) self.assertEqual(content_type, content.content_type) result = unittest.TestResult() expected = result._exc_info_to_string(an_exc_info, self) self.assertEqual(expected, ''.join(list(content.iter_text())))
def failure_quoted_bracket(self, keyword): self.protocol.lineReceived("%s mcdonalds farm [\n" % keyword) self.protocol.lineReceived(" ]\n") self.protocol.lineReceived("]\n") details = {} details['traceback'] = Content(ContentType("text", "x-traceback", {'charset': 'utf8'}), lambda:["]\n"]) self.assertFailure(details)
def test___init___sets_content_type(self): stack_lines, expected = self._get_stack_line_and_expected_output() content = StackLinesContent(stack_lines) expected_content_type = ContentType("text", "x-traceback", { "language": "python", "charset": "utf8" }) self.assertEqual(expected_content_type, content.content_type)
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 _addResult(self, method, test, *args, **kwargs): if hasattr(test, "extraresults"): extras = lambda : [json.dumps(test.extraresults).encode()] kwargs = kwargs.copy() if "details" not in kwargs: kwargs["details"] = {} else: kwargs["details"] = kwargs["details"].copy() kwargs["details"]["extraresults"] = Content(ContentType("application", "json", {'charset': 'utf8'}), extras) return method(test, *args, **kwargs)
def success_quoted_bracket(self, keyword): # This tests it is accepted, but cannot test it is used today, because # of not having a way to expose it in Python so far. self.protocol.lineReceived(_b("%s mcdonalds farm [\n" % keyword)) self.protocol.lineReceived(_b(" ]\n")) self.protocol.lineReceived(_b("]\n")) details = {} details['message'] = Content(ContentType("text", "plain"), lambda: [_b("]\n")]) self.assertSuccess(details)
def assertSkip(self, reason): details = {} if reason is not None: details['reason'] = Content(ContentType("text", "plain"), lambda: [reason]) self.assertEqual([ ('startTest', self.test), ('addSkip', self.test, details), ('stopTest', self.test), ], self.client._events)
def __init__(self, err, test): """Create a TracebackContent for err.""" if err is None: raise ValueError("err may not be None") content_type = ContentType('text', 'x-traceback', {"language": "python", "charset": "utf8"}) self._result = TestResult() value = self._result._exc_info_to_unicode(err, test) super(TracebackContent, self).__init__( content_type, lambda: [value.encode("utf8")])
def test_error_empty_message(self): self.protocol.lineReceived("error mcdonalds farm [\n") self.protocol.lineReceived("]\n") details = {} details['traceback'] = Content(ContentType("text", "x-traceback", {'charset': 'utf8'}), lambda:[""]) self.assertEqual([ ('startTest', self.test), ('addError', self.test, details), ('stopTest', self.test), ], self.client._events)
def error_quoted_bracket(self, keyword): self.protocol.lineReceived("%s mcdonalds farm [\n" % keyword) self.protocol.lineReceived(" ]\n") self.protocol.lineReceived("]\n") details = {} details['traceback'] = Content(ContentType("text", "x-traceback", {'charset': 'utf8'}), lambda:["]\n"]) self.assertEqual([ ('startTest', self.test), ('addError', self.test, details), ('stopTest', self.test), ], self.client._events)
def test_add_skip_details(self): """Test addSkip on a TestProtocolClient with details.""" details = {'reason':Content( ContentType('text', 'plain'), lambda:['Has it really?'])} self.protocol.addSkip( self.test, details=details) self.assertEqual( self.io.getvalue(), "skip: %s [ multipart\n" "Content-Type: text/plain\n" "reason\n" "E\r\nHas it really?0\r\n" "]\n" % self.test.id())
def test_screenshot_taken_when_test_fails(self): class InnerTest(testcase.AutopilotTestCase): def test_foo(self): self.fail() test = InnerTest('test_foo') test_run = test.run() self.assertFalse(test_run.wasSuccessful()) screenshot_content = test.getDetails()['FailedTestScreenshot'] self.assertEqual(screenshot_content.content_type, ContentType("image", "png"))
def _mime_type(self): """ The MIME type parameters contain details specific to Python's logging system. This allows consumers to possibly parse back the log message. """ parameters = { "charset": "utf8", "language": "python", "format": self._format or "default", } return repr(ContentType("text", "x-log", parameters))
def test_take_screenshot(self): screenshot_name = self.getUniqueString() class InnerTest(testcase.AutopilotTestCase): def test_foo(self): self.take_screenshot(screenshot_name) test = InnerTest('test_foo') test_run = test.run() self.assertTrue(test_run.wasSuccessful()) screenshot_content = test.getDetails()[screenshot_name] self.assertEqual(screenshot_content.content_type, ContentType("image", "png"))
def test_render_traceback(self): """ If the test has a traceback detail attached, it gets flushed to the log widget. """ content_type = ContentType("text", "x-traceback") record = TestRecord.create("foo", status=EXISTS) self.repository.add_record(record) self.repository.update_record(record.set(status=INPROGRESS)) record.status = None record.timestamps = (record.timestamps[0], datetime.now(utc)) record.details["traceback"] = Content(content_type, lambda: [b("ugh")]) self.repository.update_record(record) canvas = self.record.render((50, )) self.assertThat(canvas.text[1], Contains(b("ugh")))
def _addResult(self, method, test, *args, exception = False, **kwargs): if hasattr(test, "extraresults"): extras = lambda : [json.dumps(test.extraresults).encode()] kwargs = kwargs.copy() if "details" not in kwargs: kwargs["details"] = {} else: kwargs["details"] = kwargs["details"].copy() kwargs["details"]["extraresults"] = Content(ContentType("application", "json", {'charset': 'utf8'}), extras) # if using details, need to encode any exceptions into the details obj, # testtools does not handle "err" and "details" together. if "details" in kwargs and exception and (len(args) >= 1 and args[0] is not None): kwargs["details"]["traceback"] = testtools.content.TracebackContent(args[0], test) args = [] return method(test, *args, **kwargs)
def __init__(self, stack_lines, prefix_content="", postfix_content=""): """Create a StackLinesContent for ``stack_lines``. :param stack_lines: A list of preprocessed stack lines, probably obtained by calling ``traceback.extract_stack`` or ``traceback.extract_tb``. :param prefix_content: If specified, a unicode string to prepend to the text content. :param postfix_content: If specified, a unicode string to append to the text content. """ content_type = ContentType('text', 'x-traceback', {"language": "python", "charset": "utf8"}) value = prefix_content + \ self._stack_lines_to_unicode(stack_lines) + \ postfix_content super(StackLinesContent, self).__init__( content_type, lambda: [value.encode("utf8")])
def test_run(self): result = ExtendedTestResult() test = self.SampleExecTestCase("test_sample_method") test.run(result) mcdonald = subunit.RemotedTestCase("old mcdonald") bing = subunit.RemotedTestCase("bing crosby") bing_details = {} bing_details['traceback'] = Content(ContentType("text", "x-traceback", {'charset': 'utf8'}), lambda:["foo.c:53:ERROR invalid state\n"]) an_error = subunit.RemotedTestCase("an error") error_details = {} self.assertEqual([ ('startTest', mcdonald), ('addSuccess', mcdonald), ('stopTest', mcdonald), ('startTest', bing), ('addFailure', bing, bing_details), ('stopTest', bing), ('startTest', an_error), ('addError', an_error, error_details), ('stopTest', an_error), ], result._events)
def test_keywords_during_failure(self): # A smoke test to make sure that the details parsers have control # appropriately. self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("failure: old mcdonald [\n")) self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("failure a\n")) self.protocol.lineReceived(_b("failure: a\n")) self.protocol.lineReceived(_b("error a\n")) self.protocol.lineReceived(_b("error: a\n")) self.protocol.lineReceived(_b("success a\n")) self.protocol.lineReceived(_b("success: a\n")) self.protocol.lineReceived(_b("successful a\n")) self.protocol.lineReceived(_b("successful: a\n")) self.protocol.lineReceived(_b(" ]\n")) self.protocol.lineReceived(_b("]\n")) self.assertEqual(self.stdout.getvalue(), _b("")) details = {} details['traceback'] = Content( ContentType("text", "x-traceback", {'charset': 'utf8'}), lambda: [ _b("test old mcdonald\n" "failure a\n" "failure: a\n" "error a\n" "error: a\n" "success a\n" "success: a\n" "successful a\n" "successful: a\n" "]\n") ]) self.assertEqual(self.client._events, [ ('startTest', self.test), ('addFailure', self.test, details), ('stopTest', self.test), ])
def test___eq__(self): content_type1 = ContentType("foo", "bar", {"quux": "thing"}) content_type2 = ContentType("foo", "bar", {"quux": "thing"}) content_type3 = ContentType("foo", "bar", {"quux": "thing2"}) self.assertTrue(content_type1.__eq__(content_type2)) self.assertFalse(content_type1.__eq__(content_type3))