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 基本使用介紹。
功能特色¶
關於失敗 assert 陳述式 的詳細資訊 (無需記住
self.assert*
名稱)自動探索 測試模組和函數
模組化 fixtures,用於管理小型或參數化的長期測試資源
可以開箱即用執行 unittest (包括 trial) 測試套件
Python 3.8+ 或 PyPy 3
豐富的插件架構,擁有超過 1300 個以上的外部插件和蓬勃發展的社群
文件說明¶
錯誤/請求¶
請使用 GitHub 問題追蹤器 提交錯誤或請求功能。
支持 pytest¶
Open Collective 是一個為開放和透明社群提供的線上募款平台。它提供工具來籌集資金並以完全透明的方式分享您的財務狀況。
對於想要直接向專案進行一次性或每月捐款的個人和公司來說,它是首選平台。
請參閱 pytest collective 以了解更多詳細資訊。
企業版 pytest¶
可作為 Tidelift 訂閱的一部分使用。
pytest 和數千個其他套件的維護者正與 Tidelift 合作,為您用於建構應用程式的開源依賴項提供商業支援和維護。節省時間、降低風險並改善程式碼健康狀況,同時向您使用的確切依賴項的維護者付費。
安全性¶
pytest 從未與安全漏洞相關聯,但在任何情況下,若要報告安全漏洞,請使用 Tidelift 安全聯絡方式。Tidelift 將協調修復和揭露。