说明
subTest 是 unittest 的一个用于实现具有细微参数变化用例的方法,它可以把类似的操作集中于一个用例,并记录该用例中失败时的子测试特征参数,个人认为应该可以用于验证输入数据的格式、组成等。通过 unittest 自带的 TextTestRunner 是可以保存 subTest 返回的上下文管理器的,但是 HTMLTestRunner 就直接不记录任何信息了,请问是不是我这边用法不对,或者有支持 subTest 的其他 runner 吗,感谢

1. 通过 TextTestRunner 来运行测试套件
suite=unittest.TestSuite()
suite.addTest(test('test1'))
suite.addTest(test('test2'))
file=open('./result.txt','w')
runner=unittest.TextTestRunner(file)
runner.run(suite)

输出的测试结果信息如下,test2 通过,test1 部分失败:
.

======================================================================
FAIL: test1 (case.test) (x=1)


Traceback (most recent call last):
File "D:\2Temporary\auto\auto\Python\case.py", line 7, in test1
self.assertIn(func.add(x),[1,2,4,8,7,5])
AssertionError: 3 not found in [1, 2, 4, 8, 7, 5]

======================================================================
FAIL: test1 (case.test) (x=4)


Traceback (most recent call last):
File "D:\2Temporary\auto\auto\Python\case.py", line 7, in test1
self.assertIn(func.add(x),[1,2,4,8,7,5])
AssertionError: 6 not found in [1, 2, 4, 8, 7, 5]

======================================================================
FAIL: test1 (case.test) (x=7)


Traceback (most recent call last):
File "D:\2Temporary\auto\auto\Python\case.py", line 7, in test1
self.assertIn(func.add(x),[1,2,4,8,7,5])
AssertionError: 9 not found in [1, 2, 4, 8, 7, 5]

======================================================================
FAIL: test1 (case.test) (x=8)


Traceback (most recent call last):
File "D:\2Temporary\auto\auto\Python\case.py", line 7, in test1
self.assertIn(func.add(x),[1,2,4,8,7,5])
AssertionError: 10 not found in [1, 2, 4, 8, 7, 5]

======================================================================
FAIL: test1 (case.test) (x=9)


Traceback (most recent call last):
File "D:\2Temporary\auto\auto\Python\case.py", line 7, in test1
self.assertIn(func.add(x),[1,2,4,8,7,5])
AssertionError: 11 not found in [1, 2, 4, 8, 7, 5]


Ran 2 tests in 0.000s

FAILED (failures=5)

2.通过 HTMLTestRunner 运行

file=open('./result.html','wb')
runner=HTMLTestRunner.HTMLTestRunner(file)
runner.run(suite)

结果如下:

Test Group /Test case Count Pass Fail Error View
case.test test2 1 1 0 0 Detail
Total 1 1 0 0

即,test1 的测试结果没有记录


↙↙↙阅读原文可查看相关链接,并与作者交流