将unittest与子测试和verbose = 2一起使用时,顶部失败的测试摘要缺少“ FAIL \ n”。
例如,此测试(在Python 3.7中):
import unittest
class TestThing(unittest.TestCase):
def test_thing1(self):
for i in range(10):
with self.subTest(i=i):
self.assertLess(i, 10)
def test_thing2(self):
for i in range(10):
with self.subTest(i=i):
self.assertLess(i, 9)
def test_thing3(self):
for i in range(10):
with self.subTest(i=i):
self.assertLess(i, 10)
if __name__ == '__main__':
unittest.main(verbosity=2)
输出结果:
>python .\blah.py
test_thing1 (__main__.TestThing) ... ok
test_thing2 (__main__.TestThing) ... test_thing3 (__main__.TestThing) ... ok
======================================================================
FAIL: test_thing2 (__main__.TestThing) (i=9)
----------------------------------------------------------------------
Traceback (most recent call last):
File ".\blah.py", line 16, in test_thing2
self.assertLess(i, 9)
AssertionError: 9 not less than 9
----------------------------------------------------------------------
Ran 3 tests in 0.003s
FAILED (failures=1)
如果我删除了subtest
,那么输出将变为:
test_thing1 (__main__.TestThing) ... ok
test_thing2 (__main__.TestThing) ... FAIL
test_thing3 (__main__.TestThing) ... ok
======================================================================
FAIL: test_thing2 (__main__.TestThing)
----------------------------------------------------------------------
Traceback (most recent call last):
File ".\blah.py", line 16, in test_thing2
self.assertLess(i, 9)
AssertionError: 9 not less than 9
----------------------------------------------------------------------
Ran 3 tests in 0.002s
FAILED (failures=1)
我尝试使用自己的resultclass
,但似乎addFailure
并未被要求进行子测试(就像常规测试一样)。
class MyTextTestResult(unittest.TextTestResult):
def addFailure(self, test, err):
print('adding a failure')
super().addFailure(test, err)
if self.showAll:
self.stream.writeln("FAIL")
elif self.dots:
self.stream.write('F')
self.stream.flush()
def addSuccess(self, test):
print('adding a success')
super().addSuccess(test)
if self.showAll:
self.stream.writeln("ok")
elif self.dots:
self.stream.write('.')
self.stream.flush()
有人知道如何解决子测试结果的输出(使用verbose = 2)吗?
答案 0 :(得分:0)
好像我在addSubTest
中更改了TextTestResult
一样。
class MyTextTestResult(unittest.TextTestResult):
def addSubTest(self, test, subtest, err):
super().addSubTest(test, subtest, err)
if err is not None:
if not self.stream.getvalue().strip().endswith('FAIL'):
self.stream.writeln("FAIL")
if __name__ == '__main__':
# unittest.main(verbosity=2)
runner = unittest.TextTestRunner(None, resultclass=MyTextTestResult, verbosity=2)
suite = unittest.TestSuite()
suite.addTest(TestThing('test_thing1'))
suite.addTest(TestThing('test_thing2'))
suite.addTest(TestThing('test_thing3'))
runner.run(suite)
test_thing1 (__main__.TestThing) ... ok
test_thing2 (__main__.TestThing) ... FAIL
test_thing3 (__main__.TestThing) ... ok
======================================================================
FAIL: test_thing2 (__main__.TestThing) (i=9)
----------------------------------------------------------------------
Traceback (most recent call last):
File ".\blah.py", line 22, in test_thing2
self.assertLess(i, 9)
AssertionError: 9 not less than 9
----------------------------------------------------------------------
Ran 3 tests in 0.004s
FAILED (failures=2)