如何在运行时访问pytest测试的总体测试结果?
根据pytest
测试运行的总体测试结果,我要执行条件拆卸。这意味着必须在执行完所有测试之后但离开测试运行程序之前,才能访问总体测试结果。我该如何实现?
回答:
我找不到合适的pytest挂钩来访问总体测试结果。
你不需要一个 自己收集测试结果。这是我需要批量访问测试结果时通常使用的蓝图:
# conftest.pyimport pytest
def pytest_sessionstart(session):
session.results = dict()
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
result = outcome.get_result()
if result.when == 'call':
item.session.results[item] = result
现在所有测试结果都存储在session.results
dict下;用法示例:
# conftest.py (continued)def pytest_sessionfinish(session, exitstatus):
print()
print('run status code:', exitstatus)
passed_amount = sum(1 for result in session.results.values() if result.passed)
failed_amount = sum(1 for result in session.results.values() if result.failed)
print(f'there are {passed_amount} passed and {failed_amount} failed tests')
运行测试将产生:
$ pytest -sv================================== test session starts ====================================
platform darwin -- Python 3.6.4, pytest-3.7.1, py-1.5.3, pluggy-0.7.1 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-51711988, inifile:
collected 3 items
test_spam.py::test_spam PASSED
test_spam.py::test_eggs PASSED
test_spam.py::test_fail FAILED
run status code: 1
there are 2 passed and 1 failed tests
======================================== FAILURES =========================================
_______________________________________ test_fail _________________________________________
def test_fail():
> assert False
E assert False
test_spam.py:10: AssertionError
=========================== 1 failed, 2 passed in 0.05 seconds ============================
如果总体pytest
退出代码(exitstatus
)是足够的信息(有关#通过,#失败等信息,则不是必需的),请使用以下代码:
# conftest.pydef pytest_sessionfinish(session, exitstatus):
print()
print('run status code:', exitstatus)
以上是 如何在运行时访问pytest测试的总体测试结果? 的全部内容, 来源链接: utcz.com/qa/405507.html