pytest钩子函数

pytest 给我们开放了大量的 hook 函数,可以编写插件

pytest 可以识别到三种插件:

内置插件:从 pytest 内部 _pytest 目录加载的插件

外部插件:通过 pip 安装的插件(比如: pip install pytest-ordering )。

conftest.py 插件:测试目录中的 conftest.py 加载

钩子函数:

pytest hook 链接: https://docs.pytest.org/en/stable/reference.html?#hooks

pytest hook 函数也叫钩子函数,是pytest 框架开发者为用户预留的一些函,pytest 提供了大量的钩子函数,可以在用例的不同生命周期自动调用

使用:

执行完测试用例后,需要对结果进行汇总,用例总数,失败用例数,成功用例数等。

pytest有自带的一个钩子函数:pytest_terminal_summary

python 复制代码
# conftest.py

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    """
    :param terminalreporter: 报告汇总
    :param exitstatus: 退出状态
    :param config: 全局 Config 对象, 等同于 request.config
    :return:
    exitstatus:
        0 -- 用例全部通过
        1 -- 有用例失败
        2 -- 收集用i就失败了,还没执行
        3 -- 其他报错
        4 -- 其他报错
        5 -- 收集到0条用例
    """
    print(f'退出状态:{exitstatus}')

    # 获取环境地址
    base_url = config.option.base_url
    print(f'获取到的base_url: {base_url}')

    total = terminalreporter._numcollected
    if exitstatus in [0, 1]:
        passed = len([i for i in terminalreporter.stats.get('passed', []) if i.when != 'teardown'])
        failed = len([i for i in terminalreporter.stats.get('failed', []) if i.when != 'teardown'])
        skipped = len([i for i in terminalreporter.stats.get('skipped', []) if i.when != 'teardown'])
        error = len([i for i in terminalreporter.stats.get('error', []) if i.when != 'teardown'])
        successful = len(terminalreporter.stats.get('passed', [])) / terminalreporter._numcollected * 100
        duration = time.time() - terminalreporter._sessionstarttime
        markdown_text = f"""### 执行结果:  
        - 运行环境: 测试环境  
        - 运行base_url: {base_url}
        - 持续时间: {duration: .2f} 秒  

        ### 本次运行结果:  
        - 总用例数: {total}  
        - 通过用例:{passed}  
        - 跳过用例:{skipped}
        - 失败用例: {failed}  
        - 异常用例: {error}  
        - 通过率: {successful:.2f} % \n  
        """
        print(f'用例执行结果:{markdown_text}')
        with open("summary.json", 'w', encoding='utf-8') as fp:
            summary = {
                "total": total,
                "passed": passed,
                "failed": failed,
                "skipped": skipped,
                "error": error,
                "successful": f'{successful:.2f} %',
                "duration": duration
            }
            import json
            fp.write(json.dumps(summary, indent=4))
            
            """后面这里可以写发送邮件或者其他形式的通知"""
    else:
        print('未收集到测试用例')
python 复制代码
# test_allure_3.py

import allure
import requests
import json

class TestDemo:

    def test_x1(self):
        """输入正确的用户名和密码"""
        with allure.step('步骤1:登录前'):
            print('步骤1:登录前')
        allure.dynamic.feature('动态添加功能点1')
        print('x11111')
        allure.dynamic.title('动态添加title11')
        with allure.step('步骤2:登录后'):
            print('登录结束')


    def test_x2(self):
        """输入错误的用户名和密码"""
        with allure.step('步骤1:登录前'):
            print('步骤1:登录前')
        allure.dynamic.feature('动态添加功能点2')
        print('x222222')
        allure.dynamic.title('动态添加title22')
        with allure.step('步骤2:登录后'):
            print('登录结束')

参考:
https://blog.51cto.com/u_14844/6385694

相关推荐
java1234_小锋7 分钟前
一周学会Matplotlib3 Python 数据可视化-绘制自相关图
开发语言·python·信息可视化·matplotlib·matplotlib3
甄超锋8 分钟前
Java Maven更换国内源
java·开发语言·spring boot·spring·spring cloud·tomcat·maven
凢en39 分钟前
Perl——qw()函数
开发语言·perl
郝学胜-神的一滴1 小时前
基于C++的词法分析器:使用正则表达式的实现
开发语言·c++·程序人生·正则表达式·stl
Juchecar2 小时前
分析:将现代开源浏览器的JavaScript引擎更换为Python的可行性与操作
前端·javascript·python
科大饭桶2 小时前
昇腾AI自学Day2-- 深度学习基础工具与数学
人工智能·pytorch·python·深度学习·numpy
林开落L2 小时前
库的制作与原理
linux·开发语言·动静态库·库的制作
m0_480502643 小时前
Rust 入门 泛型和特征-特征对象 (十四)
开发语言·后端·rust
瓦特what?3 小时前
关于C++的#include的超超超详细讲解
java·开发语言·数据结构·c++·算法·信息可视化·数据挖掘
祁同伟.4 小时前
【C++】动态内存管理
开发语言·c++