可以通过pytest hook 来实现用例执行结果的统计。
初步实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18CASES = {'total': 0, 'passed': 0, 'failed': 0, 'error': 0}
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
result = yield
report = result.get_result()
print(report)
print(report.nodeid)
if report.when == 'setup' and report.outcome == 'failed':
CASES['total'] += 1
CASES['error'] += 1
elif report.when == 'call':
CASES['total'] += 1
if report.outcome == 'passed':
CASES['passed'] += 1
elif report.outcome == 'failed':
CASES['failed'] += 1
print(CASES)这样有一个问题,当有失败重跑情况时,会把每次重跑都统计一次运行结果,结果会有偏差,不应该包含重跑的结果,因为是同一个Case。
换个方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19CASES = {'total': 0, 'passed': 0, 'failed': 0}
RUN_RESULT = {}
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
result = yield
report = result.get_result()
print(report)
if (report.when == 'setup' or report.when == 'teardown') and report.outcome == 'failed':
RUN_RESULT[report.nodeid] = 'failed'
elif report.when == 'call':
RUN_RESULT[report.nodeid] = report.outcome
print(RUN_RESULT)
CASES['total'] = len(RUN_RESULT)
CASES['passed'] = len({k: v for k, v in RUN_RESULT.items() if v == 'passed'})
CASES['failed'] = len({k: v for k, v in RUN_RESULT.items() if v == 'failed'})
print(CASES)结果
1
2{'test_demo.py::test_demo01[zhangsan]': 'failed', 'test_demo.py::test_demo01[lisi]': 'failed', 'test_demo.py::test_demo01[wangwu]': 'failed', 'test_demo.py::test_demo02': 'passed'}
{'total': 4, 'passed': 1, 'failed': 3}