用法和調用
通過調用python -m pytest
進行測試
我們可以從命令行通過Python解釋器調用測試:
python -m pytest [...]
這基本上等同於直接調用命令行腳本,出了那在python中調用,也可將將之添加到sys.path中
退出代碼的可能及編號
運行pytest
可以產生六種不同的退出代碼
退出代碼爲0: 收集併成功通過所有測試
退出代碼爲1: 收集並運行測試但一些測試失敗
退出代碼爲2: 用戶終端了測試執行
退出代碼爲3: 執行測試時發生內部錯誤
退出代碼爲4: pytest命令行使用錯誤
退出代碼爲5: 沒有收集任何測試
它們由_pytest.main.ExitCode枚舉表示.可以使用以下命令直接導入和訪問作爲公共API一部分的退出代碼
from pytest import ExitCode
Pytest支持集中從命令行運行和選擇測試的方法
在模塊中運行測試
pytest test_mod.py
在目錄中運行測試
pytest testing/
按關鍵字表達式運行測試
pytest -k "MyClass and not method"
這將運行包括與給定字符串表達式匹配的名稱的測試,該表達式可以包括使用文件名,類名和函數名作爲變量的Python運算符.
以上示例將運行TestMyClass.test_something
但不會運行TestMyClass.test_method_simple
.
按照節點ID運行測試
每個收集的測試都分配一個唯一的nodeid,包括模塊文件名,後面跟說明符,比如類名,函數名和參數化參數,用::
字符分隔
在模塊中運行特定測試:
pytest test_mod.py::test_func
另一個在命令中指定測試方法的示例:
pytest test_mod.py::TestClass::test_method
通過標記表達式運行測試
pytest -m slow
將運行@pytest.mark.slow
裝飾器修飾的所有測試.
從包中運行測試
pytest --pyargs.pkg.testing
這將導入pkg.testing
並使用其文件系統位置來查找和運行測試
修改Python回溯打印
修改回溯打印的示例:
pytest --showlocals # show local variables in tracebacks
pytest -l # show local variables (shortcut)
pytest --tb=auto # (default) 'long' tracebacks for the first and last
# entry, but 'short' style for the other entries
pytest --tb=long # exhaustive, informative traceback formatting
pytest --tb=short # shorter traceback format
pytest --tb=line # only one line per failure
pytest --tb=native # Python standard library formatting
pytest --tb=no # no traceback at all
詳細的總結報告
-r
標誌可用於在測試會話結束時顯示’簡短測試摘要信息’,使大型測試套件可以輕鬆獲得所有故障,跳過,xfails等的清晰圖像.
例:
# content of test_example.py
import pytest
@pytest.fixture
def error_fixture():
assert 0
def test_ok():
print("ok")
def test_fail():
assert 0
def test_error(error_fixture):
pass
def test_skip():
pytest.skip("skipping this test")
def test_xfail():
pytest.xfail("xfailing this test")
@pytest.mark.xfail(reason="always xfail")
def test_xpass():
pass
測試後
==================================================== test session starts =====================================================
platform darwin -- Python 2.7.10, pytest-4.6.5, py-1.8.0, pluggy-0.13.0
rootdir: /Users/admin/Desktop/pytest_zzz
collected 6 items
xfails_test.py .FEsxX [100%]
=========================================================== ERRORS ===========================================================
________________________________________________ ERROR at setup of test_error ________________________________________________
@pytest.fixture
def error_fixture():
> assert 0
E assert 0
xfails_test.py:7: AssertionError
========================================================== FAILURES ==========================================================
_________________________________________________________ test_fail __________________________________________________________
def test_fail():
> assert 0
E assert 0
xfails_test.py:15: AssertionError
================================================== short test summary info ===================================================
SKIPPED [1] /Users/admin/Desktop/pytest_zzz/xfails_test.py:24: skipping this test
XFAIL xfails_test.py::test_xfail
reason: xfailing this test
XPASS xfails_test.py::test_xpass always xfail
ERROR xfails_test.py::test_error - assert 0
FAILED xfails_test.py::test_fail - assert 0
======================== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.04 seconds ========================
描述-r
在它之後選項接收數量的字符,使用a
的意思是’所有排除被通過’
這裏是可以使用的可用字符的完整列表
f
===>失敗E
===>錯誤s
===>跳過x
===>xfailedX
===>xpassedp
===>過去了p
===>通過輸出a
===>除了pP
A
===>全部
可以使用多個字符,例如,只能查看失敗和跳過的測試,我們可以執行
$ pytest -rfs
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 6 items
test_example.py .FEsxX [100%]
================================== ERRORS ==================================
_______________________ ERROR at setup of test_error _______________________
@pytest.fixture
def error_fixture():
> assert 0
E assert 0
test_example.py:6: AssertionError
================================= FAILURES =================================
________________________________ test_fail _________________________________
def test_fail():
> assert 0
E assert 0
test_example.py:14: AssertionError
========================= short test summary info ==========================
FAILED test_example.py::test_fail - assert 0
SKIPPED [1] $REGENDOC_TMPDIR/test_example.py:23: skipping this test
== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===
使用p
列表通過測試,同時P
添加額外的’PASSES’部分,其中包含已通過但已捕獲輸出的測試:
$ pytest -rpP
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 6 items
test_example.py .FEsxX [100%]
================================== ERRORS ==================================
_______________________ ERROR at setup of test_error _______________________
@pytest.fixture
def error_fixture():
> assert 0
E assert 0
test_example.py:6: AssertionError
================================= FAILURES =================================
________________________________ test_fail _________________________________
def test_fail():
> assert 0
E assert 0
test_example.py:14: AssertionError
================================== PASSES ==================================
_________________________________ test_ok __________________________________
--------------------------- Captured stdout call ---------------------------
ok
========================= short test summary info ==========================
PASSED test_example.py::test_ok
== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===
在PDB(Python Debugger) 開始測試時進行刪除
pytest
允許用戶通過命令行選項在每次測試開始時立即進入PDB提示符:
pytest --trace
這將在每次測試開始時調用Python調試器
設置斷點
要在代碼中設置斷點,請在代碼中使用本機Python調用,pytest會自動禁用該測試的輸出捕獲:import pdb;pdb.set_trace()
- 其他測試中的輸出不受影響
- 任何先前的測試輸出已經被捕獲並將被處理
- 結束調試器會話時(通過continue命令),輸出捕獲將恢復.
分析測試執行持續時間
要獲得最慢的10個測試持續時間的列表:
pytest --durations=10