pytest:帮助你编写更好的程序

pytest 框架使编写小型、可读的测试变得容易,并且可以扩展以支持应用程序和库的复杂功能测试。

pytest 需要:Python 3.8+ 或 PyPy3。

PyPI 包名pytest

一个快速示例

# content of test_sample.py
def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5

要执行它

$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 1 item

test_sample.py F                                                     [100%]

================================= FAILURES =================================
_______________________________ test_answer ________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_sample.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_sample.py::test_answer - assert 4 == 5
============================ 1 failed in 0.12s =============================

由于 pytest 详细的断言自省功能,只需使用普通的 assert 语句。有关使用 pytest 的基本介绍,请参阅开始

特性

  • 失败的 断言语句 的详细信息 (无需记住 self.assert* 名称)

  • 测试模块和函数的自动发现

  • 用于管理小型或参数化长生命周期测试资源的模块化 fixture

  • 开箱即用,可运行 unittest (包括 trial) 测试套件

  • Python 3.8+ 或 PyPy 3

  • 丰富的插件架构,拥有超过 1300+ 个外部插件和蓬勃发展的社区

文档

  • 开始 - 在短短二十分钟内安装 pytest 并掌握其基础知识

  • 操作指南 - 循序渐进的指南,涵盖广泛的用例和需求

  • 参考指南 - 包括完整的 pytest API 参考、插件列表等

  • 解释 - 背景信息、关键主题讨论、高级问题解答

错误/请求

请使用 GitHub 问题追踪器 提交错误或请求功能。

支持 pytest

Open Collective 是一个为开放透明社区提供在线资金支持的平台。它提供工具来筹集资金并完全透明地分享您的财务状况。

对于希望直接向项目进行一次性或每月捐赠的个人和公司来说,它是首选平台。

更多详情请参见 pytest 社区

企业版 pytest

作为 Tidelift 订阅的一部分提供。

pytest 和数千个其他软件包的维护者正与 Tidelift 合作,为您用于构建应用程序的开源依赖项提供商业支持和维护。在支付您所使用的确切依赖项维护者的同时,节省时间、降低风险并改善代码健康状况。

了解更多。

安全

pytest 从未与安全漏洞相关联,但无论如何,要报告安全漏洞,请使用 Tidelift 安全联系方式。Tidelift 将协调修复和披露。