pytest-dev/pytest
摘要
pytest 是一个成熟的 Python 测试框架,可以轻松编写简单的测试,同时能够扩展以支持应用程序和库的复杂功能测试。
查看缓存全文
缓存时间: 2026/06/15 00:55
pytest-dev/pytest
来源:https://github.com/pytest-dev/pytest
.. image:: https://github.com/pytest-dev/pytest/raw/main/doc/en/img/pytest_logo_curves.svg :target: https://docs.pytest.org/en/stable/ :align: center :height: 200 :alt: pytest
.. image:: https://img.shields.io/pypi/v/pytest.svg :target: https://pypi.org/project/pytest/
.. image:: https://img.shields.io/conda/vn/conda-forge/pytest.svg :target: https://anaconda.org/conda-forge/pytest
.. image:: https://img.shields.io/pypi/pyversions/pytest.svg :target: https://pypi.org/project/pytest/
.. image:: https://codecov.io/gh/pytest-dev/pytest/branch/main/graph/badge.svg :target: https://codecov.io/gh/pytest-dev/pytest :alt: Code coverage Status
.. image:: https://github.com/pytest-dev/pytest/actions/workflows/test.yml/badge.svg :target: https://github.com/pytest-dev/pytest/actions?query=workflow%3Atest
.. image:: https://results.pre-commit.ci/badge/github/pytest-dev/pytest/main.svg :target: https://results.pre-commit.ci/latest/github/pytest-dev/pytest/main :alt: pre-commit.ci status
.. image:: https://www.codetriage.com/pytest-dev/pytest/badges/users.svg :target: https://www.codetriage.com/pytest-dev/pytest
.. image:: https://readthedocs.org/projects/pytest/badge/?version=latest :target: https://pytest.readthedocs.io/en/latest/?badge=latest :alt: Documentation Status
.. image:: https://img.shields.io/badge/Discord-pytest–dev-blue :target: https://discord.com/invite/pytest-dev :alt: Discord
.. image:: https://img.shields.io/badge/Libera%20chat-%23pytest-orange :target: https://web.libera.chat/#pytest :alt: Libera chat
pytest 框架可以轻松编写小型测试,同时也能扩展以支持应用程序和库的复杂功能测试。
一个简单测试的例子:
.. code-block:: python
文件 test_sample.py 的内容
def inc(x): return x + 1
def test_answer(): assert inc(3) == 5
执行它::
$ pytest
============================= test session starts =============================
collected 1 items
test_sample.py F
================================== FAILURES ===================================
_________________________________ test_answer _________________________________
def test_answer():
> assert inc(3) == 5
E assert 4 == 5
E + where 4 = inc(3)
test_sample.py:5: AssertionError
========================== 1 failed in 0.04 seconds ===========================
得益于 pytest 详细的断言内省,你可以直接使用普通的 assert 语句。更多示例请参阅 入门指南 <https://docs.pytest.org/en/stable/getting-started.html>_。
特性
- 失败
断言语句 <https://docs.pytest.org/en/stable/how-to/assert.html>_ 的详细信息(无需记忆self.assert*名称) - 测试模块和函数的
自动发现 <https://docs.pytest.org/en/stable/explanation/goodpractices.html#test-discovery>_ 模块化夹具 <https://docs.pytest.org/en/stable/explanation/fixtures.html>_ 用于管理小型或参数化的长期测试资源- 可直接运行
unittest <https://docs.pytest.org/en/stable/how-to/unittest.html>_(或 trial)测试套件 - Python 3.10+ 或 PyPy3
- 丰富的插件架构,拥有 1300+
外部插件 <https://docs.pytest.org/en/stable/reference/plugin_list.html>_ 和蓬勃发展的社区
文档
完整文档(包括安装、教程和 PDF 文档)请参阅 https://docs.pytest.org/en/stable/。
Bug / 请求
请使用 GitHub issue 追踪器 <https://github.com/pytest-dev/pytest/issues>_ 提交 bug 或请求功能。
更新日志
各版本的修复和增强请查阅 更新日志 <https://docs.pytest.org/en/stable/changelog.html>__ 页面。
支持 pytest
Open Collective_ 是一个面向开放透明社区的资金募集在线平台。它提供了筹集资金并完全透明地分享财务状况的工具。希望一次性或每月直接向项目捐款的个人和公司首选该平台。更多详情见 pytest collective_。
.. _Open Collective: https://opencollective.com .. _pytest collective: https://opencollective.com/pytest
企业级 pytest
作为 Tidelift 订阅的一部分提供。pytest 及其他数千个软件包的维护者正与 Tidelift 合作,为您构建应用所使用的开源依赖提供商业支持与维护。节省时间、降低风险、改善代码健康,同时为您使用的确切依赖的维护者付费。了解更多。 <https://tidelift.com/subscription/pkg/pypi-pytest?utm_source=pypi-pytest&utm_medium=referral&utm_campaign=enterprise&utm_term=repo>_
安全 ^^^^^^^
如果您发现了可能属于安全漏洞的问题,请不要创建 issue —— 而是通过 新的安全公告 <https://github.com/pytest-dev/pytest/security/advisories/new>__ 报告。
许可证
版权所有 Holger Krekel 及其他贡献者,2004 年。pytest 在 MIT_ 许可证条款下分发,是免费开源软件。
.. _MIT: https://github.com/pytest-dev/pytest/blob/main/LICENSE
相似文章
用于定位不稳定Python测试原因的工具
pytest-leak-finder 是一个 pytest 插件,它使用二分查找来识别当一起运行时,哪个更早的测试导致后续测试失败。
插件案例研究:Pluggy
一篇博客文章,探讨了Pluggy——这是一个最初源自pytest的Python库,用于构建插件系统。内容包括其工作原理以及如何将其与一个玩具级HTML转换工具配合使用。
PrefectHQ/prefect
Prefect 是一个开源的工作流编排框架,用于在 Python 中构建和自动化数据管道。
@freeCodeCamp:测试是帮助你在发布新功能后保持软件可靠性的关键。在本课程中,Beau 将教你软件…
freeCodeCamp 宣布推出一个关于使用 Playwright 进行软件测试的综合课程,涵盖测试金字塔、TDD、API 测试等基础知识,以及像 KaneAI 这样的 AI 驱动工具。
cypress-io/cypress
Cypress 是一个快速、简单且可靠的端到端测试框架,适用于任何在浏览器中运行的内容,作为 GitHub 上的开源项目提供。