如何从单元测试中获取失败、成功、错误测试用例名称。 runner.TextTestResult?
How to get failed, succes, error test cases names from the unittest. runner.TextTestResult?
从下面的代码中,如何从结果变量中获取失败、成功、错误的测试用例名称?许多失败的、总的测试运行和所有可能来自 unittest。 runner.TextTestResult。但我也想知道失败的测试用例名称,即 Test_Sample1.test_2
import unittest
class Test_Sample1(unittest.TestCase):
def test_1(self):
pass
def test_2(self):
self.assertEqual(1,2)
def test_3(self):
pass
class Test_Sample2(unittest.TestCase):
def test_1(self):
pass
def test_2(self):
self.assertEqual(1,2)
def test_3(self):
pass
def main():
unitt = unittest.main (verbosity = 2, exit=False)
result = unitt.result
print(f'wasSuccessful {result.wasSuccessful()}')
print(f'errors {len(result.errors)}')
print(f'failures {len(result.failures)}')
print(f'testsRun {result.testsRun}')
if __name__ == '__main__':
main()
您可以使用自定义 TestRunner
+ TestResult
并根据需要进行配置。举个例子:
import unittest
from unittest.runner import TextTestResult, TextTestRunner
class Test1(unittest.TestCase):
def test_1(self):
pass
def test_2(self):
self.assertEqual(1, 2)
class Test2(unittest.TestCase):
def test_1(self):
self.assertEqual(1, 2)
@unittest.skip('skip example')
def test_2(self):
pass
def main():
class CustomResult(TextTestResult):
def __init__(self, *args, **kwargs):
super(CustomResult, self).__init__(*args, **kwargs)
self.success = []
def addSuccess(self, test):
super(CustomResult, self).addSuccess(test)
self.success.append(test)
unit = unittest.main(verbosity=2, exit=False, testRunner=TextTestRunner(resultclass=CustomResult))
result = unit.result # type: CustomResult
# just do here all what you need...
print('success:')
for test in result.success:
print(test)
print('*' * 20 + '\n')
print('skipped:')
for test, reason in result.skipped:
print('{}. reason: {}'.format(test, reason))
print('*' * 20 + '\n')
print('failures:')
for test, trace in result.failures:
print(test)
# print(trace) ...if you need
print('*' * 20 + '\n')
for error in result.errors:
# blablabla
continue
结果:
success:
test_1 (__main__.Test1)
********************
skipped:
test_2 (__main__.Test2). reason: skip example
********************
failures:
test_2 (__main__.Test1)
test_1 (__main__.Test2)
********************
从下面的代码中,如何从结果变量中获取失败、成功、错误的测试用例名称?许多失败的、总的测试运行和所有可能来自 unittest。 runner.TextTestResult。但我也想知道失败的测试用例名称,即 Test_Sample1.test_2
import unittest
class Test_Sample1(unittest.TestCase):
def test_1(self):
pass
def test_2(self):
self.assertEqual(1,2)
def test_3(self):
pass
class Test_Sample2(unittest.TestCase):
def test_1(self):
pass
def test_2(self):
self.assertEqual(1,2)
def test_3(self):
pass
def main():
unitt = unittest.main (verbosity = 2, exit=False)
result = unitt.result
print(f'wasSuccessful {result.wasSuccessful()}')
print(f'errors {len(result.errors)}')
print(f'failures {len(result.failures)}')
print(f'testsRun {result.testsRun}')
if __name__ == '__main__':
main()
您可以使用自定义 TestRunner
+ TestResult
并根据需要进行配置。举个例子:
import unittest
from unittest.runner import TextTestResult, TextTestRunner
class Test1(unittest.TestCase):
def test_1(self):
pass
def test_2(self):
self.assertEqual(1, 2)
class Test2(unittest.TestCase):
def test_1(self):
self.assertEqual(1, 2)
@unittest.skip('skip example')
def test_2(self):
pass
def main():
class CustomResult(TextTestResult):
def __init__(self, *args, **kwargs):
super(CustomResult, self).__init__(*args, **kwargs)
self.success = []
def addSuccess(self, test):
super(CustomResult, self).addSuccess(test)
self.success.append(test)
unit = unittest.main(verbosity=2, exit=False, testRunner=TextTestRunner(resultclass=CustomResult))
result = unit.result # type: CustomResult
# just do here all what you need...
print('success:')
for test in result.success:
print(test)
print('*' * 20 + '\n')
print('skipped:')
for test, reason in result.skipped:
print('{}. reason: {}'.format(test, reason))
print('*' * 20 + '\n')
print('failures:')
for test, trace in result.failures:
print(test)
# print(trace) ...if you need
print('*' * 20 + '\n')
for error in result.errors:
# blablabla
continue
结果:
success:
test_1 (__main__.Test1)
********************
skipped:
test_2 (__main__.Test2). reason: skip example
********************
failures:
test_2 (__main__.Test1)
test_1 (__main__.Test2)
********************