pytest之插件pytest.hookimpl(hookwrapper=True, tryfirst=True)
1、该插件作用于pytest的钩子函数上,可以获取到测试用例不同执行阶段的结果(setup,call,teardown)
2、可以获取钩子方法的调用结果(返回一个result对象)和调用结果的测试报告(返回一个report对象)
@pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): """
每个测试用例执行后,制作测试报告
:param item:测试用例对象
:param call:测试用例的测试步骤
先执行when=’setup’ 返回setup 的执行结果
然后执行when=’call’ 返回call 的执行结果
最后执行when=’teardown’返回teardown 的执行结果
:return:
"""
# 获取钩子方法的调用结果,返回一个result对象
out = yield
# # 获取调用结果的测试报告,返回一个report对象, reportd对象的属性包括when(steup, call, teardown三个值)、nodeid(测试用例的名字)、outcome(用例的执行结果,passed,failed)
report = out.get_result()
print(out)
print(report)
print(report.when)
print(report.nodeid)
print(report.outcome)
运行结果:执行三次的原因是此钩子函数会在测试用例执行的不同阶段(setup, call, teardown)都会调用一次
testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest <pluggy.callers._Result object at 0x0000000004B7D828> <TestReport 'testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest' when='setup' outcome='passed'> setup testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest passed <pluggy.callers._Result object at 0x0000000002FAA748> <TestReport 'testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest' when='call' outcome='passed'> call testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest passed PASSED<pluggy.callers._Result object at 0x000000000303F5F8> <TestReport 'testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest' when='teardown' outcome='passed'> teardown testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest passed