.. 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: 代码覆盖率状态
.. 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 状态
.. 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: 文档状态
.. 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 聊天
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's detailed assertion introspection, you can simply use plain assert statements. See getting-started <https://docs.pytest.org/en/stable/getting-started.html#our-first-test-run>_ 提供更多示例。
特性
-
关于失败
assert statements <https://docs.pytest.org/en/stable/how-to/assert.html>_ 的详细信息(无需记住self.assert*名称) -
自动发现 <https://docs.pytest.org/en/stable/explanation/goodpractices.html#python-test-discovery>_ 测试模块和函数 -
Modular fixtures <https://docs.pytest.org/en/stable/explanation/fixtures.html>_ 用于 管理小型或参数化的长期测试资源 -
开箱即用运行
unittest <https://docs.pytest.org/en/stable/how-to/unittest.html>_(或试验) 测试套件 -
Python 3.10+ 或 PyPy3
-
丰富的插件架构,拥有 1300+ 个
external plugins <https://docs.pytest.org/en/latest/reference/plugin_list.html>_ 和蓬勃发展的社区
文档
有关完整文档,包括安装、教程和 PDF 文档,请访问 https://docs.pytest.org/en/stable/。
错误/请求
请使用 GitHub issue tracker <https://github.com/pytest-dev/pytest/issues>_ 提交 bug 或请求新功能。
Changelog
查阅 Changelog <https://docs.pytest.org/en/stable/changelog.html>__ 页面以了解每个版本的修复和增强。
Support pytest
Open Collective_ 是一个面向开放和透明社区的在线资助平台。
它提供工具来筹集资金,并以完全透明的方式分享您的财务状况。
它是个人和公司选择直接向项目进行一次性或每月捐款的平台。
在 pytest collective_ 中查看更多详情。
.. _Open Collective: https://opencollective.com .. _pytest collective: https://opencollective.com/pytest
pytest for enterprise
作为 Tidelift 订阅的一部分提供。
pytest 的维护者以及数千个其他包的维护者正在与 Tidelift 合作,为您提供用于构建应用程序的开源依赖项的商业支持和 维护。 在支付您使用的确切依赖项的维护者的同时,节省时间、降低风险并改善代码健康状况。
Learn more. <https://tidelift.com/subscription/pkg/pypi-pytest?utm_source=pypi-pytest&utm_medium=referral&utm_campaign=enterprise&utm_term=repo>_
安全 ^^^^^^^^
如果您发现了一个您认为是安全漏洞的问题,请不要创建 issue —— 而是通过 new security advisory <https://github.com/pytest-dev/pytest/security/advisories/new>__ 进行报告。
许可证
版权所有 Holger Krekel 及其他人,2004 年。
pytest 根据 MIT_ 许可证条款分发,是免费且开源的软件。
.. _MIT: https://github.com/pytest-dev/pytest/blob/main/LICENSE