最棘手的部分是将测试结果传递到fixture中,其余的几乎是微不足道的。在pytest的示例Making test result information available in fixtures之后,在conftest.py中添加一个自定义钩子:import pytest
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
outcome = yield
rep = outcome.get_result()
setattr(item, "rep_" + rep.when, rep)
return rep
现在,您可以使用自定义拆卸逻辑增强driver装置,以防测试失败:
^{pr2}$
运行测试会产生:$ pytest -sv
=============================== test session starts ===============================
platform darwin Python 3.6.3, ...
cachedir: .cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-48521762, inifile:
plugins: ...
collecting ... collected 2 items
test_spam.py::test_fail FAILED [ 50%]
debug Server Not Found
test_spam.py::test_ok PASSED [100%]
==================================== FAILURES =====================================
____________________________________ test_fail ____________________________________
driver =
...
- Captured log setup
remote_connection.py 474 DEBUG POST http://127.0.0.1:50319/session { ...
...
remote_connection.py 561 DEBUG Finished Request
======================== 1 failed, 1 passed in 7.23 seconds =======================