Pytest命令行選項的具體使用
pytest 是一個流行的 Python 測試框架,它提供了許多命令行選項,可以幫助用戶更好地控制測試執(zhí)行過程。在使用 pytest 進行測試時,熟悉 pytest 的命令行選項非常重要,這將有助于減少錯誤和提高測試效率。本文將詳細介紹 pytest 的命令行選項,并給出示例。
一、pytest 命令行選項分類
pytest 的命令行選項可以分為三類:
- 環(huán)境配置選項:這些選項用于設置 pytest 的環(huán)境配置,例如設置日志級別、覆蓋配置文件、設置測試模式等。
- 測試過濾選項:這些選項用于過濾和選擇測試用例,例如指定測試目錄、選擇特定測試模塊、運行指定測試函數(shù)等。
- 測試執(zhí)行選項:這些選項用于控制 pytest 的測試執(zhí)行過程,例如重試失敗測試、生成測試報告、并行執(zhí)行測試等。
下面,我們將逐一介紹這些命令行選項的具體用法。
二、環(huán)境配置選項
--version:打印 pytest 版本號。
-h, --help:顯示 pytest 命令行選項和使用說明。
--verbose, -v:增加測試結(jié)果輸出的詳細程度。
--quiet, -q:減少測試結(jié)果輸出的詳細程度。
--traceconfig:顯示解析和加載配置文件時的詳細信息。
--pdb:當測試失敗或者發(fā)生異常時,進入 Python 調(diào)試器。
--pdbcls:自定義調(diào)試器的類型。
--capture:設置 pytest 的標準輸出流捕獲方式,有三個值可選:sys、fd、no。
--norecursedirs:設定哪些目錄不搜索測試用例。
--rootdir:設置 pytest 的根目錄。
--maxfail=n:設定在第 n 次測試失敗后停止測試執(zhí)行。
--junit-xml=PATH:將測試結(jié)果輸出到 JUnit XML 文件中。
三、測試過濾選項
path:指定測試目錄或文件,可以是相對路徑或絕對路徑。例如:pytest tests/。
-m:選擇特定標記的測試用例進行執(zhí)行。例如:pytest -m "slow"。
-k:選擇包含某個關鍵字的測試用例進行執(zhí)行。例如:pytest -k "add or subtract"。
-x:遇到一條測試用例失敗就停止測試執(zhí)行。
--pdb:當測試失敗或者發(fā)生異常時,進入 Python 調(diào)試器。
--lf:只重跑上次測試失敗的測試用例。
--ff:只重跑上次測試失敗的測試用例,并在全部測試結(jié)束后再重新運行一遍這些測試用例。
--sw:重跑上次修改過的測試模塊。
--last-failed-no-failures:只重跑上次失敗的測試用例(如果沒有失敗的測試用例,則不執(zhí)行測試)。
--collect-only:只執(zhí)行測試用例的收集階段,不運行測試用例的執(zhí)行階段。
--pdbcls:自定義調(diào)試器的類型。
--trace:顯示 pytest 的內(nèi)部跟蹤信息。
--count:運行指定數(shù)量的測試用例。例如:pytest -v --count=10。
四、測試執(zhí)行選項
-n:并行運行測試用例,可以在后面跟一個數(shù)字指定并發(fā)度。例如:pytest -n 4。
-x:遇到一條測試用例失敗就停止測試執(zhí)行。
--maxfail=n:設定在第 n 次測試失敗后停止測試執(zhí)行。
--last-failed:只重跑上次失敗的測試用例。
--failed-first:先運行之前失敗的測試用例。
--reruns=n:在測試用例失敗的情況下,重新運行 n 次測試。
--pdb:當測試失敗或者發(fā)生異常時,進入 Python 調(diào)試器。
--pdbcls:自定義調(diào)試器的類型。
--junit-xml=PATH:將測試結(jié)果輸出到 JUnit XML 文件中。
--html=PATH:將測試結(jié)果輸出到 HTML 文件中。
--tb=long/short/line/native/no:設置輸出錯誤信息的格式。
--capture=no:禁止捕獲標準輸出和標準錯誤,直接將它們輸出到終端。
--capture=sys/stdout/stderr:設置 pytest 的標準輸出流捕獲方式,有三個值可選:sys、fd、no。
--show-capture=all/failed/no:控制是否顯示捕獲的標準輸出流。
--disable-warnings:禁用 pytest 的警告信息。
以上是 pytest 的命令行選項介紹,通過使用這些選項,可以更好地控制 pytest 的測試執(zhí)行過程,減少錯誤和提高測試效率。接下來我將給出部分命令行選項的代碼示例及運行效果。
五、代碼示例及運行效果
1.--version:打印 pytest 版本號。
該選項可以通過執(zhí)行 pytest --version 命令來使用,代碼示例如下:
$ pytest --version This is pytest version 5.0.1, imported from /usr/local/lib/python3.6/site-packages/pytest.py
2.-h, --help:顯示 pytest 命令行選項和使用說明。
該選項可以通過執(zhí)行 pytest --help 命令來使用,代碼示例如下:
$ pytest --help usage: pytest [options] [file_or_dir] [file_or_dir] [...] positional arguments: file_or_dir test file or root directory to search for tests optional arguments: -h, --help show this help message and exit --version display pytest version and information about plugins ...
3.--verbose, -v:增加測試結(jié)果輸出的詳細程度。
該選項可以通過執(zhí)行 pytest -v 命令來使用,代碼示例如下:
$ pytest -v test_example.py::test_add PASSED [ 50%] test_example.py::test_subtract PASSED [100%] ============================ 2 passed in 0.03s =============================
4.--quiet, -q:減少測試結(jié)果輸出的詳細程度。
該選項可以通過執(zhí)行 pytest -q 命令來使用,代碼示例如下:
$ pytest -q .. 2 passed in 0.03s
5.--traceconfig:顯示解析和加載配置文件時的詳細信息。
該選項可以通過執(zhí)行 pytest --traceconfig 命令來使用,代碼示例如下:
$ pytest --traceconfig using: pytest-5.0.1, py-1.8.0, pluggy-0.12.0 active plugins: * pytest_cov-2.7.1 at /Users/user/anaconda3/envs/test/lib/python3.6/site-packages/pytest_cov/plugin.py disabled plugins: collector: <_pytest.main.Session object at 0x107cbe3c8>
6.--pdb:當測試失敗或者發(fā)生異常時,進入 Python 調(diào)試器。
該選項可以通過執(zhí)行 pytest --pdb 命令來使用,在測試用例出現(xiàn)錯誤時會自動進入 pdb 調(diào)試器,代碼示例如下:
$ pytest --pdb
test_example.py::test_add PASSED [ 50%]
test_example.py::test_subtract FAILED [100%]
=================================== FAILURES ===================================
________________________________ test_subtract ________________________________
def test_subtract():
assert subtract(4, 3) == 1
> assert subtract(2, 3) == -1
E assert 2 == -1
E + where 2 = subtract(2, 3)
test_example.py:10: AssertionError
----------------------------- PDB -----------------------------
> /Users/user/example/test_example.py(10)test_subtract()
...
(Pdb) 7.--capture:設置 pytest 的標準輸出流捕獲方式,有三個值可選:sys、fd、no。
該選項可以通過執(zhí)行 pytest --capture=sys 命令來使用,將標準輸出流和標準錯誤輸出重定向到 pytest 的日志系統(tǒng)中,代碼示例如下:
$ pytest --capture=sys
test_example.py::test_add PASSED [ 50%]
test_example.py::test_subtract FAILED [100%]
=================================== FAILURES ===================================
________________________________ test_subtract ________________________________
def test_subtract():
assert subtract(4, 3) == 1
> assert subtract(2, 3) == -1
E assert 2 == -1
E + where 2 = subtract(2, 3)
test_example.py:10: AssertionError
[Capture] capturing logcall output to 'log': sys8.--norecursedirs:設定哪些目錄不搜索測試用例。
該選項可以通過執(zhí)行 pytest --norecursedirs=examples 命令來使用,將 examples 目錄從搜索測試用例的路徑中排除,代碼示例如下:
$ pytest --norecursedirs=examples test_example.py::test_add PASSED [ 50%] test_example.py::test_subtract PASSED [100%] ============================ 2 passed in 0.03s =============================
9.--rootdir:設置 pytest 的根目錄。
該選項可以通過執(zhí)行 pytest --rootdir=/path/to/project 命令來使用,將 pytest 的根目錄設置為 /path/to/project,代碼示例如下:
$ pytest --rootdir=/path/to/project test_example.py::test_add PASSED [ 50%] test_example.py::test_subtract PASSED [100%] ============================ 2 passed in 0.03s =============================
10.--maxfail=n:設定在第 n 次測試失敗后停止測試執(zhí)行。
該選項可以通過執(zhí)行 pytest --maxfail=1 命令來使用,當?shù)谝粭l測試用例失敗時會停止測試執(zhí)行,代碼示例如下:
$ pytest --maxfail=1
test_example.py::test_add PASSED [ 50%]
test_example.py::test_subtract FAILED [100%]
=================================== FAILURES ===================================
________________________________ test_subtract ________________________________
def test_subtract():
assert subtract(4, 3) == 1
> assert subtract(2, 3) == -1
E assert 2 == -1
E + where 2 = subtract(2, 3)
test_example.py:10: AssertionError
========================== 1 failed, 1 passed in 0.04s ==========================11.--junit-xml=PATH:將測試結(jié)果輸出到 JUnit XML 文件中。
該選項可以通過執(zhí)行 pytest --junit-xml=test_results.xml 命令來使用,將測試結(jié)果輸出到 test_results.xml 文件中,代碼示例如下:
$ pytest --junit-xml=test_results.xml test_example.py::test_add PASSED [ 50%] test_example.py::test_subtract PASSED [100%] ============================ 2 passed in 0.03s =============================
12.path:指定測試目錄或文件,可以是相對路徑或絕對路徑。例如:pytest tests/。
該選項可以通過執(zhí)行 pytest tests/ 命令來使用,只運行 tests 目錄下的測試用例,代碼示例如下:
$ pytest tests/ test_example.py::test_add PASSED [ 50%] test_example.py::test_subtract PASSED [100%] ============================ 2 passed in 0.03s =============================
13.-m:選擇特定標記的測試用例進行執(zhí)行。例如:pytest -m "slow"。
該選項可以通過在測試用例中使用 @pytest.mark 標記來進行使用,例如:
import pytest
@pytest.mark.slow
def test_slow():
pass
def test_not_slow():
pass
然后執(zhí)行 pytest -m "slow" 命令來只運行標有 @pytest.mark.slow 標記的測試用例,代碼示例如下:
$ pytest -m "slow" test_example.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.slow - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html @pytest.mark.slow no tests ran in 0.00s PytestUnknownMarkWarning: Unknown pytest.mark.slow - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
14.-k:選擇包含某個關鍵字的測試用例進行執(zhí)行。例如:pytest -k "add or subtract"。
該選項可以通過執(zhí)行 pytest -k "add or subtract" 命令來使用,只運行測試用例名稱中包含 "add" 或者 "subtract" 字符串的測試用例,代碼示例如下:
$ pytest -k "add or subtract" test_example.py::test_add PASSED [ 50%] test_example.py::test_subtract PASSED [100%] ============================ 2 passed in 0.03s =============================
15.-x:遇到一條測試用例失敗就停止測試執(zhí)行。
該選項可以通過執(zhí)行 pytest -x 命令來使用,在第一條測試用例失敗時會停止測試執(zhí)行,代碼示例如下:
$ pytest -x
test_example.py::test_add PASSED [ 50%]
test_example.py::test_subtract FAILED [100%]
=================================== FAILURES ===================================
________________________________ test_subtract ________________________________
def test_subtract():
assert subtract(4, 3) == 1
> assert subtract(2, 3) == -1
E assert 2 == -1
E + where 2 = subtract(2, 3)
test_example.py:10: AssertionError
========================== 1 failed, 1 passed in 0.04s ==========================16.--lf:只重跑上次測試失敗的測試用例。
該選項可以通過執(zhí)行 pytest --lf 命令來使用,在上次測試用例出現(xiàn)錯誤時會自動重新運行失敗的測試用例,代碼示例如下:
$ pytest --lf
test_example.py::test_add PASSED [ 50%]
test_example.py::test_subtract FAILED [100%]
=================================== FAILURES ===================================
______________________ test_subtract[2-3] ______________________
x = 2
y = 3
def test_subtract(x, y):
assert subtract(x, y) == -1
tests/test_example.py:10: AssertionError
------------------------------ rerun test call -------------------------------
test_example.py::test_subtract[x17.--ff:只重跑最后一次測試失敗的測試用例。
該選項可以通過執(zhí)行 pytest --ff 命令來使用,在上次測試用例出現(xiàn)錯誤時會自動重新運行最后一個失敗的測試用例,代碼示例如下:
$ pytest --ff
test_example.py::test_add PASSED [ 50%]
test_example.py::test_subtract FAILED [100%]
=================================== FAILURES ===================================
________________________________ test_subtract ________________________________
def test_subtract():
assert subtract(4, 3) == 1
> assert subtract(2, 3) == -1
E assert 2 == -1
E + where 2 = subtract(2, 3)
test_example.py:10: AssertionError
------------------------------- Captured stdout --------------------------------
subtraction of 2 and 3 is -1
========================== 1 failed, 1 passed in 0.04s ==========================18.--failed-first:在所有測試用例之前先運行上次測試失敗的測試用例。
該選項可以通過執(zhí)行 pytest --failed-first 命令來使用,會先運行上次測試失敗的測試用例,然后再運行剩下的測試用例,代碼示例如下:
$ pytest --failed-first
test_example.py::test_subtract FAILED [100%]
test_example.py::test_add PASSED
=================================== FAILURES ===================================
________________________________ test_subtract ________________________________
def test_subtract():
assert subtract(4, 3) == 1
> assert subtract(2, 3) == -1
E assert 2 == -1
E + where 2 = subtract(2, 3)
test_example.py:10: AssertionError
----------------------------- Captured stdout -----------------------------
subtraction of 2 and 3 is -1
========================== 1 failed, 1 passed in 0.03s ==========================19.-n, --numprocesses:指定并發(fā)進程數(shù)量。
該選項可以通過執(zhí)行 pytest -n 4 命令來使用,將測試用例分配到 4 個并發(fā)進程中運行,代碼示例如下:
$ pytest -n 4 test_example.py::test_add PASSED [ 25%] test_example.py::test_add PASSED [ 50%] test_example.py::test_subtract PASSED [ 75%] test_example.py::test_subtract PASSED [100%] ============================ 4 passed in 0.02s =============================
20.-d, --dist:啟用分布式測試。
該選項可以通過執(zhí)行 pytest --dist=loadfile 命令來使用,通過加載多個 Python 進程并在所有進程中分發(fā)測試運行,代碼示例如下:
$ pytest --dist=loadfile test_example.py::test_add PASSED [ 25%] test_example.py::test_add PASSED [ 50%] test_example.py::test_subtract PASSED [ 75%] test_example.py::test_subtract PASSED [100%] ============================ 4 passed in 0.02s =============================
21.--durations=n:按運行時間對測試用例進行排序,并顯示最慢 n 條測試用例的運行時長。
該選項可以通過執(zhí)行 pytest --duration=2 命令來使用,顯示運行時間最慢的兩條測試用例的運行時長,代碼示例如下:
$ pytest --duration=2 test_example.py::test_add PASSED [ 50%] test_example.py::test_subtract PASSED [100%] ======================== slowest 2 test durations ========================= 0.01s call test_example.py::test_subtract 0.01s call test_example.py::test_add ============================ 2 passed in 0.02s =============================
22.--show-capture:在結(jié)果中顯示測試用例標準輸出流和標準錯誤輸出。
該選項可以通過執(zhí)行 pytest --show-capture 命令來使用,將測試用例的標準輸出流和標準錯誤輸出顯示在測試結(jié)果中,代碼示例如下:
$ pytest --show-capture test_example.py::test_add PASSED [ 50%] test_example.py::test_subtract PASSED [100%] ================================== Captured stdout ================================= subtraction of 4 and 3 is 1 subtraction of 2 and 3 is -1 ============================ 2 passed in 0.03s =============================
以上就是 pytest 命令行選項的詳細介紹和相應的代碼示例,希望可以幫助你更好地使用 pytest 進行測試。更多相關Pytest命令行選項內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python猜解網(wǎng)站數(shù)據(jù)庫管理員密碼的腳本
這篇文章主要和大家分享一個Python腳本,可以實現(xiàn)猜解網(wǎng)站數(shù)據(jù)庫管理員的密碼。文中的示例代碼講解詳細,需要的小伙伴可以參考一下2022-02-02
Python直接使用plot()函數(shù)畫圖的方法實例
Python非常簡單而又非常強大,它的功能之一就是畫出漂亮的圖表,實現(xiàn)數(shù)據(jù)的可視化,下面這篇文章主要給大家介紹了關于Python直接使用plot()函數(shù)畫圖的相關資料,需要的朋友可以參考下2022-05-05
yolov5訓練時參數(shù)workers與batch-size的深入理解
最近再學習YOLOv3與YOLOv5訓練數(shù)據(jù)集的具體步驟,幾經(jīng)波折終于實現(xiàn)了很好的效果,這篇文章主要給大家介紹了關于yolov5訓練時參數(shù)workers與batch-size的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-03-03
python GUI庫圖形界面開發(fā)之PyQt5表單布局控件QFormLayout詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QFormLayout詳細使用方法與實例,需要的朋友可以參考下2020-03-03

