前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python测试框架pytest(11)Hooks函数 - pytest_terminal_summary统计测试结果

Python测试框架pytest(11)Hooks函数 - pytest_terminal_summary统计测试结果

作者头像
wangmcn
发布2022-07-26 14:22:46
4730
发布2022-07-26 14:22:46
举报
文章被收录于专栏:AllTests软件测试

Python测试框架pytest(11)

Hooks函数

pytest_terminal_summary统计测试结果

当用例执行完成后,希望获取到执行的结果,方便了解用例的执行情况,这时候就可以使用 pytest_terminal_summary 来进行测试结果的统计(可以拿到所有的执行结果)。

pytest_terminal_summary 源码:

参数:

  • terminalreporter(内部使用的终端测试报告对象)
  • exitstatus(返回给操作系统的返回码)
  • config(pytest 的 config 对象)

示例一:正常情况

创建conftest.py文件,pytest_terminal_summary函数用于收集测试结果。

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import time

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    """
    收集测试结果
    """
    print(terminalreporter.stats)
    print("total:", terminalreporter._numcollected)
    print('passed:', len([i for i in terminalreporter.stats.get('passed', []) if i.when != 'teardown']))
    print('failed:', len([i for i in terminalreporter.stats.get('failed', []) if i.when != 'teardown']))
    print('error:', len([i for i in terminalreporter.stats.get('error', []) if i.when != 'teardown']))
    print('skipped:', len([i for i in terminalreporter.stats.get('skipped', []) if i.when != 'teardown']))
    print('成功率:%.2f' % (len(terminalreporter.stats.get('passed', []))/terminalreporter._numcollected*100)+'%')
    # terminalreporter._sessionstarttime 会话开始时间
    duration = time.time() - terminalreporter._sessionstarttime
    print('total times:', duration, 'seconds')

创建test_a.py文件

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

def test_a1():
    print("测试用例test_a1")
    assert 1 == 1

def test_a2():
    print("测试用例test_a2")

@pytest.mark.skip("跳过test_a3")
def test_a3():
    print("测试用例test_a3")
    assert 1 == 1

def test_a4():
    print("测试用例test_a4")
    assert 1 == 2

创建test_b.py文件

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

def test_b1():
    print("测试用例test_b1")

def test_b2():
    print("测试用例test_b2")
    assert 1 == 2

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

代码语言:javascript
复制
pytest -s

运行结果:

获取到的测试用例执行状态打印到控制台。

示例二:setup 异常情况

修改test_b.py文件

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytest

@pytest.fixture(scope="function")
def my_setup():
    assert 1 == 2

def test_b1(my_setup):
    print("测试用例test_b1")

def test_b2():
    print("测试用例test_b2")
    assert 1 == 2

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

代码语言:javascript
复制
pytest -s

运行结果:

setup报错,结果标记为error。

示例三:teardown 异常情况

修改test_b.py文件

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytest

@pytest.fixture(scope="function")
def my_teardown():
    yield
    assert 1 == 2

def test_b1(my_teardown):
    print("测试用例test_b1")

def test_b2():
    print("测试用例test_b2")
    assert 1 == 2

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

代码语言:javascript
复制
pytest -s

运行结果:

用例总数是6,但是测试报告状态结果为2 failed, 3 passed, 1 skipped, 1 error,合计为7。

从获取的terminalreporter.stats信息可以看出:

passed里when = 'call'时,统计一次test_b1用例

<TestReport 'test_b.py::test_b1' when='call' outcome='passed'>

error里when = 'teardown'时,又统计一次test_b1用例

<TestReport 'test_b.py::test_b1' when='teardown' outcome='failed'>

所以报告结果合计为7。

但因为when='teardown'是测试用例的后置操作,一般用于数据的清理等操作,如报错不影响测试用例的执行结果,所以在conftest.py文件里

pytest_terminal_summary函数获取测试结果进行了忽略统计。

注:

when='teardown'是测试用例的后置操作,一般用于数据的清理等操作,如报错不影响测试用例的执行结果,可以忽略。

但是如果想与测试报告结果保持一致(即报错都要统计)

修改conftest.py文件

脚本代码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import time

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    """
    收集测试结果
    """
    print(terminalreporter.stats)
    print("total:", terminalreporter._numcollected)
    print('passed:', len(terminalreporter.stats.get('passed', [])))
    print('failed:', len(terminalreporter.stats.get('failed', [])))
    print('error:', len(terminalreporter.stats.get('error', [])))
    print('skipped:', len(terminalreporter.stats.get('skipped', [])))
    # terminalreporter._sessionstarttime 会话开始时间
    duration = time.time() - terminalreporter._sessionstarttime
    print('total times:', duration, 'seconds')

再次运行用例,运行结果:

抓取的测试结果与用例执行后的状态结果一致。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-08-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AllTests软件测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档