
- pytest 教程
- pytest - 主頁
- pytest - 簡介
- pytest - 環境設定
- 識別測試檔案和函式
- pytest - 從基本測試開始
- pytest - 檔案執行
- 執行測試套件的子集
- 測試名稱的子字串匹配
- pytest - 分組測試
- pytest - 元件
- pytest - Conftest.py
- pytest - 引數化測試
- pytest - Xfail/跳過測試
- 在 N 次測試失敗後停止測試套件
- pytest - 並行執行測試
- 以 XML 形式顯示測試執行結果
- pytest - 摘要
- pytest - 結語
- pytest 有用資源
- pytest - 快速指南
- pytest - 有用資源
- pytest - 討論
pytest-在 N 次測試失敗後停止測試套件
在實際場景中,一旦新版本的程式碼準備就緒準備部署時,它將首先部署到預生產/過渡環境中。其上執行測試套件。
僅當測試套件透過時,該程式碼才有資格部署到生產環境中。如果測試失敗,無論是單次還是多次,則程式碼都未準備好進行生產。
因此,如果我們希望在 n 個測試失敗後立即停止測試套件的執行,該怎麼辦。這可以透過在 pytest 中使用 maxfail 來完成。
在 n 個測試失敗後立即停止測試套件執行的語法如下 −
pytest --maxfail = <num>
使用以下程式碼建立檔案 test_failure.py。
import pytest import math def test_sqrt_failure(): num = 25 assert math.sqrt(num) == 6 def test_square_failure(): num = 7 assert 7*7 == 40 def test_equality_failure(): assert 10 == 11
執行此測試檔案時,所有 3 個測試都將失敗。在這裡,我們將透過以下方式在第一次失敗後停止測試的執行 −
pytest test_failure.py -v --maxfail 1
test_failure.py::test_sqrt_failure FAILED =================================== FAILURES =================================== _______________________________________ test_sqrt_failure __________________________________________ def test_sqrt_failure(): num = 25 > assert math.sqrt(num) == 6 E assert 5.0 == 6 E + where 5.0 = <built-in function sqrt>(25) E + where <built-in function sqrt>= math.sqrt test_failure.py:6: AssertionError =============================== 1 failed in 0.04 seconds ===============================
在上面的結果中,我們可以看到執行在一個失敗後已停止。
廣告