自动化工具 请问 HTMLTestRunner 是不是不支持 subTest,有支持的 runner 推荐吗

米酒 · August 16, 2019 · Last by 米酒 replied at August 16, 2019 · 277 hits

说明
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的测试结果没有记录

共收到 1 条回复 时间 点赞
米酒 #1 · August 16, 2019 作者

看了下,pytest有支持subTest,后面再看下支不支持保存HTML新式的结果,https://github.com/pytest-dev/pytest-subtests/issues

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up