钩子方法 pytest_runtest_makereport 可以清晰的了解用例的执行过程,并获取到每个用例的执行结果。

 

钩子方法 pytest_runtest_makereport 源码:

按照执行顺序,具体过程如下:

1、先判断,当 report.when == 'setup' 时,返回执行结果。

2、然后判断,当 report.when == 'call' 时,返回执行结果。

3、最后判断,当 report.when == 'teardown' 时,返回执行结果。

Python测试框架pytest(09)Hooks函数 - pytest_runtest_makereport获取用例执行结果_用例

 

示例一:conftest.py 添加 pytest_runtest_makereport

新建conftest.py文件

写pytest_runtest_makereport内容,打印运行过程与运行结果。

脚本代码:



#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
print('======')
# 获取钩子方法的调用结果
runResult = yield
print('用例执行结果:', runResult)
# 从钩子方法的调用结果中获取测试报告
report = runResult.get_result()
print('测试报告:%s' % report)
print('测试步骤:%s' % report.when)
print('nodeid:%s' % report.nodeid)
print('描述说明:%s' % str(item.function.__doc__))
print('运行结果:%s' % report.outcome)


创建test_case.py文件

脚本代码:



#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

def test_abc():
"""
测试用例:test_abc
"""
print("AllTests软件测试")


打开命令行,输入执行命令



pytest -s


运行结果:

执行用例经历了三个阶段:setup->call->teardown,并打印了返回的对象与对象属性。

Python测试框架pytest(09)Hooks函数 - pytest_runtest_makereport获取用例执行结果_用例_02

 

示例二:给用例添加前置(setup)和后置(teardown)操作

修改conftest.py文件

脚本代码:



#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
print('======')
# 获取钩子方法的调用结果
runResult = yield
print('用例执行结果:', runResult)
# 从钩子方法的调用结果中获取测试报告
report = runResult.get_result()
print('测试报告:%s' % report)
print('测试步骤:%s' % report.when)
print('nodeid:%s' % report.nodeid)
print('描述说明:%s' % str(item.function.__doc__))
print('运行结果:%s' % report.outcome)

@pytest.fixture(scope="session", autouse=True)
def fixture_setup_teardown():
print("前置操作:setup")
yield
print("后置操作:teardown")


打开命令行,输入执行命令



pytest -s


运行结果:

Python测试框架pytest(09)Hooks函数 - pytest_runtest_makereport获取用例执行结果_软件测试_03

 

示例三:setup 失败

修改conftest.py文件

脚本代码:



#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
print('======')
# 获取钩子方法的调用结果
runResult = yield
print('用例执行结果:', runResult)
# 从钩子方法的调用结果中获取测试报告
report = runResult.get_result()
print('测试报告:%s' % report)
print('测试步骤:%s' % report.when)
print('nodeid:%s' % report.nodeid)
print('描述说明:%s' % str(item.function.__doc__))
print('运行结果:%s' % report.outcome)

@pytest.fixture(scope="session", autouse=True)
def fixture_setup_teardown():
print("前置操作:setup")
assert 1 == 2
yield
print("后置操作:teardown")


打开命令行,输入执行命令



pytest -s


运行结果:

当setup执行失败时,setup执行结果为failed;后面的用例和teardown都不会执行。

用例的状态为error。

Python测试框架pytest(09)Hooks函数 - pytest_runtest_makereport获取用例执行结果_用例_04

 

示例四:teardown 失败

修改conftest.py文件

脚本代码:



#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
print('======')
# 获取钩子方法的调用结果
runResult = yield
print('用例执行结果:', runResult)
# 从钩子方法的调用结果中获取测试报告
report = runResult.get_result()
print('测试报告:%s' % report)
print('测试步骤:%s' % report.when)
print('nodeid:%s' % report.nodeid)
print('描述说明:%s' % str(item.function.__doc__))
print('运行结果:%s' % report.outcome)

@pytest.fixture(scope="session", autouse=True)
def fixture_setup_teardown():
print("前置操作:setup")
yield
print("后置操作:teardown")
assert 1 == 2


打开命令行,输入执行命令



pytest -s


运行结果:

用例的状态为1个passed、1个error(teardown运行结果为failed)。

Python测试框架pytest(09)Hooks函数 - pytest_runtest_makereport获取用例执行结果_pytest_05

 

示例五:call 失败

将conftest.py文件改为示例二的正常代码

修改test_case.py文件

脚本代码:



#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
def test_abc():
"""
测试用例:test_abc
"""
print("AllTests软件测试")
assert 1 == 2


打开命令行,输入执行命令



pytest -s


运行结果:

call运行结果为failed,用例状态为failed。

Python测试框架pytest(09)Hooks函数 - pytest_runtest_makereport获取用例执行结果_测试框架_06

 

示例六:只获取 call 的结果

根据示例二的conftest.py文件,pytest_runtest_makereport钩子方法执行了三次(setup、call、teardown),如只执行call,则添加条件判断if report.when == "call":即可。

修改conftest.py文件

脚本代码:



#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
print('======')
# 获取钩子方法的调用结果
runResult = yield
print('用例执行结果:', runResult)
# 从钩子方法的调用结果中获取测试报告
report = runResult.get_result()
if report.when == "call":
print('测试报告:%s' % report)
print('测试步骤:%s' % report.when)
print('nodeid:%s' % report.nodeid)
print('描述说明:%s' % str(item.function.__doc__))
print('运行结果:%s' % report.outcome)

@pytest.fixture(scope="session", autouse=True)
def fixture_setup_teardown():
print("前置操作:setup")
yield
print("后置操作:teardown")


并将test_case.py文件改为示例一的正常代码

打开命令行,输入执行命令



pytest -s


运行结果:

只获取call的结果信息

Python测试框架pytest(09)Hooks函数 - pytest_runtest_makereport获取用例执行结果_测试框架_07