我想项目中定义两个conftest.py,testcases/conftest.py和testcases/performance/conftest.py。两个conftest.py都定义pytest_runtest_makereport。performance下的case执行performance的pytest_runtest_makereport,其他的case执行默认的pytest_runtest_makereport。
实际运行结果:
当执行performance下的case时,两个pytest_runtest_makereport都会执行,testcases/performance/conftest.py下的pytest_runtest_makereport会先执行,testcases/conftest.py的pytest_runtest_makereport后执行。跟我期望的不太一样。
workaround:
testcases/performance/conftest.py
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
out = yield
report = out.get_result()
setattr(report, 'ignorelongreprtext', True)
testcases/conftest.py
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
"""
:param item:
"""
out = yield
report = out.get_result()
if hasattr(report, 'ignorelongreprtext') and report.ignorelongreprtext:
pass
#### run non ignorelongreprtext code
else:
if report.longreprtext:
print(report.longreprtext)
### run ignorelongreprtext code
1. 给report设置一个标识,用来控制两个pytest_runtest_makereport的差异。2. 通过tryfirst控制执行顺序。