pytest文档33-Hooks函数获取用例执行结果(pytest_runtest_makereport)
前言 pytest提供的很多钩子(Hooks)方法方便我们对测试用例框架进行二次开发,可以根据自己的需求进行改造。 先学习下pytest_runtest_makereport这个钩子方法,可以更清晰的了解用例的执行过程,并获取到每个用例的执行结果。 pytest_runtest_makereport 先看下相关的源码,在 _pytest/runner.py 下,可以导入之后,点进去查看 from _pytest import runner # 对应源码 def pytest_runtest_makereport(item, call): """ return a :py:class:`_pytest.runner.TestReport` object for the given :py:class:`pytest.Item` and :py:class:`_pytest.runner.CallInfo`. """ 这里item是测试用例,call是测试步骤,具体执行过程如下: 先执行when='setup' 返回setup 的执行结果 然后执行when='call' 返回call 的执行结果 最后执行when='teardown'返回teardown 的执行结果 运行案例 conftest.py 写 pytest_runtest_makereport 内容,打印运行过程和运行结果