博客
关于我
pytest框架快速入门-pytest运行时参数说明,pytest详解,pytest.ini详解
阅读量:799 次
发布时间:2023-03-05

本文共 1339 字,大约阅读时间需要 4 分钟。

Pytest简介与应用指南

Pytest 是一个成熟的全功能 Python 测试框架,广泛应用于单元测试、功能测试以及自动化测试等领域。以下将从安装、基础使用、运行参数以及配置等方面详细介绍 Pytest 的使用方法。

Pytest 安装

安装 Pytest 及其常用插件,可以通过以下步骤完成:

  • 安装依赖

    将以下内容写入 require-install.txt 文件:

    pytestpytest-htmlpytest-xdistpytest-rerunfailures

    在命令提示符下运行:

    pip install -r require-install.txt

    验证安装结果:

    pytest --version

    如果出现版本冲突,可以降低 Python 或 Pytest 版本。

  • 示例环境配置

    作者推荐的环境为:

    • 操作系统:Windows 7
    • Python 版本:3.7.3
    • Pytest 版本:5.4.1
  • Pytest 基础应用

    测试用例编写

  • 函数测试

    定义测试函数,命名为 test_ 开头:

    import pytestdef test_01():    print('测试用例一')
  • 类测试

    测试类需以 Test 开头,且不包含 __init__ 方法:

    class TestCase:    def test_03(self):        print('测试用例三')    def defg(self):        print('测试用例四')
  • 运行方式

    • 主函数方式
      if __name__ == '__main__':    pytest.main(['-s', 'pytest-demo.py'])
    • 命令行方式
      在 Pycharm 中打开终端,运行:
      pytest pytest-demo.py
  • 运行时参数说明

    • -s:启用打印输出。
    • :::指定测试用例。
    • --html=./report.html:生成 HTML 测试报告(需安装 pytest-html)。
    • --maxfail=1:允许 1 个失败后停止测试。
    • -n=2:使用多线程运行测试(需安装 pytest-xdist)。
    • --reruns=2:重试失败的测试用例(需安装 pytest-rerunfailures)。

    测试配置文件

    Pytest 的 pytest.ini 文件位于项目根目录下,配置方式如下:

    [pytest]addopts = -s --html=./report.htmltestpaths = ../pytestprojectpython_files = test*.pypython_classes = Test*python_functions = test*

    示例配置:

    [pytest]addopts = -s --html=./report.htmltestpaths = ..python_files = test_*.pypython_classes = Test*

    总结

    Pytest 是一个强大的测试框架,支持灵活的配置和丰富的插件扩展。通过合理的测试用例编写和配置文件设置,可以实现高效的测试自动化。希望本文能为您的测试工作提供帮助,祝您测试顺利!

    转载地址:http://ynafk.baihongyu.com/

    你可能感兴趣的文章
    printf()函数
    查看>>
    PyTorch中的自定义权重初始化
    查看>>
    printf格式字符串和输出列表个数及类型不匹配案例
    查看>>
    printf的格式控制字符串
    查看>>
    PrintStream概述
    查看>>
    Prismix:Prisma 架构混合器,为复杂项目而生
    查看>>
    pritunl服务安装及配置
    查看>>
    Private Destructor
    查看>>
    private和protected能同时修饰成员变量吗_天天用注解你了解注解是怎么实现的吗?...
    查看>>
    privoxy Invalid header received from client.
    查看>>
    Privoxy代码下载
    查看>>
    Probabilistic-Programming-and-Bayesian-Methods-for-Hackers
    查看>>
    Problem F: 质心算法
    查看>>
    Problem N HDU 2612 Find a way (两次BFS求最值)
    查看>>
    Process /usr/libexec/gdu-notification-daemon was killed by signal 6 (SIGABRT)
    查看>>
    process.env.VUE_APP_BASE_API 获取不到
    查看>>
    Process.run() 和 Process.start() 之间的区别
    查看>>
    Processes
    查看>>
    Processing通过编程实现艺术设计_实现艺术和现实的交互---数据设计分析002
    查看>>
    ProcessOnLoading
    查看>>