我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用unittest.TextTestResult()。
def testGetDescriptionWithMultiLineDocstring(self): """Tests getDescription() for a method with a longer docstring. The second line of the docstring. """ result = unittest.TextTestResult(None, True, 1) self.assertEqual( result.getDescription(self), ('testGetDescriptionWithMultiLineDocstring ' '(' + __name__ + '.Test_TestResult)\n' 'Tests getDescription() for a method with a longer ' 'docstring.'))
def testGetDescriptionWithoutDocstring(self): result = unittest.TextTestResult(None, True, 1) self.assertEqual( result.getDescription(self), 'testGetDescriptionWithoutDocstring (' + __name__ + '.Test_TestResult)')
def testGetDescriptionWithOneLineDocstring(self): """Tests getDescription() for a method with a docstring.""" result = unittest.TextTestResult(None, True, 1) self.assertEqual( result.getDescription(self), ('testGetDescriptionWithOneLineDocstring ' '(' + __name__ + '.Test_TestResult)\n' 'Tests getDescription() for a method with a docstring.'))
def test_init(self): runner = unittest.TextTestRunner() self.assertFalse(runner.failfast) self.assertFalse(runner.buffer) self.assertEqual(runner.verbosity, 1) self.assertTrue(runner.descriptions) self.assertEqual(runner.resultclass, unittest.TextTestResult)
def test_multiple_inheritance(self): class AResult(unittest.TestResult): def __init__(self, stream, descriptions, verbosity): super(AResult, self).__init__(stream, descriptions, verbosity) class ATextResult(unittest.TextTestResult, AResult): pass # This used to raise an exception due to TextTestResult not passing # on arguments in its __init__ super call ATextResult(None, None, 1)
def buildResultClass(args): """Build a Result Class to use in the testcase execution""" class StampedResult(unittest.TextTestResult): """ Custom TestResult that prints the time when a test starts. As toaster-auto can take a long time (ie a few hours) to run, timestamps help us understand what tests are taking a long time to execute. """ def startTest(self, test): import time self.stream.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " - ") super(StampedResult, self).startTest(test) return StampedResult
def __init__(self, stream=None, descriptions=None, verbosity=0, ignore=False): self.ignore = ignore TextTestResult.__init__(self, stream, descriptions, verbosity)
def addError(self, test, err): """Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info(). """ if self._is_mandatory(test): err = self._add_mandatory_message(err) self.stop() # <- sets "self.shouldStop = True TextTestResult.addError(self, test, err)
def addFailure(self, test, err): """Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info(). """ if err[0] == ValidationError: exctype, value, tb = err # Unpack tuple. tb = HideInternalStackFrames(tb) # Hide internal frames. value._verbose = self.showAll # Set verbose flag (True/False). err = (exctype, value, tb) # Repack tuple. if self._is_mandatory(test): err = self._add_mandatory_message(err) self.stop() # <- sets "self.shouldStop = True TextTestResult.addFailure(self, test, err)
def addUnexpectedSuccess(self, test): """Called when a test was expected to fail, but succeed.""" if hasattr(self, 'addUnexpectedSuccess'): TextTestResult.addUnexpectedSuccess(self, test) if self._is_mandatory(test): self.stop() # <- sets "self.shouldStop = True
def test_init(self): runner = unittest.TextTestRunner() self.assertFalse(runner.failfast) self.assertFalse(runner.buffer) self.assertEqual(runner.verbosity, 1) self.assertEqual(runner.warnings, None) self.assertTrue(runner.descriptions) self.assertEqual(runner.resultclass, unittest.TextTestResult)
def test_tracebacks(self): @patch.object(Foo, 'f', object()) def test(): raise AssertionError try: test() except: err = sys.exc_info() result = unittest.TextTestResult(None, None, 0) traceback = result._exc_info_to_string(err, self) self.assertIn('raise AssertionError', traceback)
def testGetSubTestDescriptionWithoutDocstring(self): with self.subTest(foo=1, bar=2): result = unittest.TextTestResult(None, True, 1) self.assertEqual( result.getDescription(self._subtest), 'testGetSubTestDescriptionWithoutDocstring (' + __name__ + '.Test_TestResult) (bar=2, foo=1)') with self.subTest('some message'): result = unittest.TextTestResult(None, True, 1) self.assertEqual( result.getDescription(self._subtest), 'testGetSubTestDescriptionWithoutDocstring (' + __name__ + '.Test_TestResult) [some message]')
def testGetSubTestDescriptionWithoutDocstringAndParams(self): with self.subTest(): result = unittest.TextTestResult(None, True, 1) self.assertEqual( result.getDescription(self._subtest), 'testGetSubTestDescriptionWithoutDocstringAndParams ' '(' + __name__ + '.Test_TestResult) (<subtest>)')
def testGetNestedSubTestDescriptionWithoutDocstring(self): with self.subTest(foo=1): with self.subTest(bar=2): result = unittest.TextTestResult(None, True, 1) self.assertEqual( result.getDescription(self._subtest), 'testGetNestedSubTestDescriptionWithoutDocstring ' '(' + __name__ + '.Test_TestResult) (bar=2, foo=1)')
def testGetSubTestDescriptionWithMultiLineDocstring(self): """Tests getDescription() for a method with a longer docstring. The second line of the docstring. """ result = unittest.TextTestResult(None, True, 1) with self.subTest(foo=1, bar=2): self.assertEqual( result.getDescription(self._subtest), ('testGetSubTestDescriptionWithMultiLineDocstring ' '(' + __name__ + '.Test_TestResult) (bar=2, foo=1)\n' 'Tests getDescription() for a method with a longer ' 'docstring.'))
def startTest(self, test): super(TextTestResult, self).startTest(test) if self.showAll: self.stream.write('... ') self.stream.flush()