3.pytest命令行參數
3.1 pytest控制臺信息詳解
? ? 通常在運行pytest之后,會出現如下所示的控制臺信息:
C:\Users\Surpass\Documents\PycharmProjects\PytestStudy\Lesson01>pytest test_01.py
==================test session starts =======================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\Surpass\Documents\PycharmProjects\PytestStudy\Lesson01
collected 2 items
test_01.py .F [100%]
===================FAILURES =================================
__________________ test_add_02 ______________________________
def test_add_02():
> assert Add(3,4)==6
E assert 7 == 6
E + where 7 = Add(3, 4)
test_01.py:12: AssertionError
================ short test summary info ====================
FAILED test_01.py::test_add_02 - assert 7 == 6
=============== 1 failed, 1 passed in 0.56s ==================
- ===== test session starts =====
測試會話分隔符,代表啟動了一個新的調用
- platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
當前運行的平臺,Python、pytest、pytest包的版本等信息
- rootdir
當前起始目錄,pytest搜索測試代碼時使用的目錄
- collected 2 items
在搜索的目錄中找到的測試函數個數
- test_01.py .F
test_01.py代表測試文件
- FAILED
未通過的測試函數的失敗原因
- 1 failed, 1 passed in 0.14s
統計測試函數通過和未通過的數量及運行時間
3.2 Pytest運行結果類型
? ? 在Pytest中,測試函數可能會返回多種結果,不像傳統的單元測試框架,僅顯示通過和失敗。以下是可能返回的結果:
- PASSED(.)
測試通過。預期成功,實際成功
- F(FAILED)
表示測試未通過,也可能是XPASS狀態與strict選項造成的沖突。預期成功,實際失敗
- SKIPPED(s)
測試函數未執行,即該運行測試時跳過該測試函數,常用測試標記@pytest.mark.skip()或@pytest.mark.skipif()指定跳過測試的條件
- xfail(x)
期望測試失敗,而且確實失敗。常用測試標記@pytest.mark.xfail()來指定認為會失敗的測試函數。預期失敗,實際失敗
- XPASS(X)
期望測試失敗,但實際結果卻是運行通過,即不符合預期。預期失敗,實際成功
- ERROR(E)
運行測試函數時,出現異常或錯誤,可能由fixture引起
3.3 Pytest命令行參數
? ? 很多時候,運行單元測試框架并不需要界面,更多的時候采用命令行的形式執行,pytest也提供很多的命令行參數,獲取命令行參數幫助如下所示:
pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
positional arguments:
file_or_dir
general:
-x, --exitfirst exit instantly on first error or failed test.
? ? 下面來學習一些常用的命令行參數
3.3.1 --collect-only
? ? 該參數僅收集測試函數,但并不執行運行操作。如下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --collect-only
============================= test session starts ================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 3 items
<Module test_01.py>
<Function test_add_01>
<Function test_add_02>
<Module test_02.py>
<Function test_requestBaidu>
3.3.2 -k Expression
? ? -k選項允許使用表達式來指定期望運行的測試函數。常用于篩選要進行運行的測試函數。在以下示例中,僅運行當前及其子目錄中測試函數名含request或temp的測試函數,源代碼如下所示:
- test_01.py
import pytest
# First of sample function
def Add(x:int,y:int)->int:
return x+y
# First of test function
def test_add_01():
assert Add(2,3)==5
def test_add_02():
assert Add(3,4)==6
def test_request_01():
assert Add(4,3)==7
def test_temp_01():
assert Add(8,3)==11
- test_02.py
import requests
import pytest
url = "http://www.baidu.com"
port = 80
def test_requestBaidu():
testUrl=f"{url}:{port}"
r=requests.get(url=testUrl,timeout=10)
content=r.content.decode("utf8")
print(content)
assert "bai" in content
以下先驗證篩選的結果是否正確:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -k "request or temp" --collect-only
============================== test session starts =======================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 5 items / 2 deselected / 3 selected
<Module test_01.py>
<Function test_request_01>
<Function test_temp_01>
<Module test_02.py>
<Function test_requestBaidu>
=========================== 2 deselected in 2.36s =================================
實際運行結果:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -k "request or temp" -v
======================== test session starts ======================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- d:\program files\python\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 5 items / 2 deselected / 3 selected
test_01.py::test_request_01 PASSED [ 33%]
test_01.py::test_temp_01 PASSED [ 66%]
test_02.py::test_requestBaidu PASSED [100%]
====================== 3 passed, 2 deselected in 0.52s ==============================
3.3.3 -m MarkExpr
? ? 該選項常用于標記測試并分組,方便快速運行選中的測試函數,具體的標記可以自定義,可以使用裝飾器@pytest.mark。示例如下:
- test_01.py
import pytest
# First of sample function
def Add(x:int,y:int)->int:
return x+y
@pytest.mark.TestMark
# First of test function
def test_add_01():
assert Add(2,3)==5
def test_add_02():
assert Add(3,4)==6
@pytest.mark.RunThisTestFunc
def test_request_01():
assert Add(4,3)==7
def test_temp_01():
assert Add(8,3)==11
- test_01.py
import requests
import pytest
url = "http://www.baidu.com"
port = 80
@pytest.mark.RunThisTestFunc
def test_requestBaidu():
testUrl=f"{url}:{port}"
r=requests.get(url=testUrl,timeout=10)
content=r.content.decode("utf8")
print(content)
assert "bai" in content
運行結果如下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -m "RunThisTestFunc or not TestMark" --collect-only
=========================== test session starts ====================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 5 items / 1 deselected / 4 selected
<Module test_01.py>
<Function test_add_02>
<Function test_request_01>
<Function test_temp_01>
<Module test_02.py>
<Function test_requestBaidu>
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -m "RunThisTestFunc or not TestMark" -v
========================== test session starts =======================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- d:\program files\python\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 5 items / 1 deselected / 4 selected
test_01.py::test_add_02 FAILED [ 25%]
test_01.py::test_request_01 PASSED [ 50%]
test_01.py::test_temp_01 PASSED [ 75%]
test_02.py::test_requestBaidu PASSED [100%]
3.3.4 -x, --exitfirst
? ? 正常情況下,pytest會運行每一個搜索到的測試函數,如果某個測試函數斷言失敗或觸發了異常,則該測試函數的運行就停止了,此時pytest會將其標記為失敗,然后繼續運行后續的測試函數,這也是我們期望的運行模式。那如果我們希望在某一個測試函數在運行失敗,則中斷不再運行后續的測試函數,此時就可以使用-x選項。
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -x
========================== test session starts =========================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 6 items
test_01.py .F
=============================== FAILURES ==========================================
_____________________________________________________ test_add_02 _____________________________________________________
def test_add_02():
> assert Add(3,4)==6
E assert 7 == 6
E + where 7 = Add(3, 4)
test_01.py:13: AssertionError
==================== 1 failed, 1 passed, 3 warnings in 0.32s =========================
從輸出的信息可以看出,總共有6個測試函數,而在運行第2個測試函數,斷言失敗,則停止整個運行過程,因此,僅運行了2個測試函數,一個測試通過,一個測試失敗。
3.3.5 --maxfail=num
? ? -x選項的特點是一遇到失敗,就會直接全部停止。而如果想要pytest在失敗n次后再停止,則可通過選項-maxfail指定允許失敗的次數。
import pytest
# First of sample function
def Add(x:int,y:int)->int:
return x+y
@pytest.mark.TestMark
# First of test function
def test_add_01():
assert Add(2,3)==5
def test_add_02():
assert Add(3,4)==6
@pytest.mark.RunThisTestFunc
def test_request_01():
assert Add(4,3)==7
def test_temp_01():
assert Add(8,3)==12
運行結果如下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --maxfail=2
=============================== test session starts =====================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 6 items
test_01.py .F.F
================================= FAILURES ==========================================
_________________________________ test_add_02 __________________________________________
def test_add_02():
> assert Add(3,4)==6
E assert 7 == 6
E + where 7 = Add(3, 4)
test_01.py:13: AssertionError
________________________________ test_temp_01 _________________________________________
def test_temp_01():
> assert Add(8,3)==12
E assert 11 == 12
E + where 11 = Add(8, 3)
test_01.py:20: AssertionError
====================== 2 failed, 2 passed, 3 warnings in 0.35s =========================
如果--maxfail=1,則功能與-x一樣
3.3.6 --lf --last-failed
? ? 當一個或多個測試函數運行失敗后,我們常常希望能夠定位到最后一個運行失敗的測試函數,并且希望能重新運行,這時則可以使用--lf選項
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --lf
============================== test session starts ====================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 4 items / 2 deselected / 2 selected
run-last-failure: rerun previous 2 failures (skipped 3 files)
test_01.py FF [100%]
=================================== FAILURES ==========================================
____________________________ test_add_02 ________________________________________
def test_add_02():
> assert Add(3,4)==6
E assert 7 == 6
E + where 7 = Add(3, 4)
test_01.py:13: AssertionError
_____________________________________test_temp_01 _________________________________________
def test_temp_01():
> assert Add(8,3)==12
E assert 11 == 12
E + where 11 = Add(8, 3)
test_01.py:20: AssertionError
==================== 2 failed, 2 deselected, 2 warnings in 0.13s =============================
3.3.7 --ff --failed-first
? ? --ff選項與--lf選項功能基本相同,不同之處如下所示:
--ff 先運行上次失敗的測試函數再運行完剩余的測試函數
--lf 僅運行上次失敗的測試函數
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --ff
=============================== test session starts =======================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 6 items
run-last-failure: rerun previous 2 failures first
test_01.py FF.. [ 66%]
test_02.py . [ 83%]
test_03.py . [100%]
================================= FAILURES ======================================
______________________________________test_add_02 ____________________________________________
def test_add_02():
> assert Add(3,4)==6
E assert 7 == 6
E + where 7 = Add(3, 4)
test_01.py:13: AssertionError
_______________________________________ test_temp_01 _________________________________________
def test_temp_01():
> assert Add(8,3)==12
E assert 11 == 12
E + where 11 = Add(8, 3)
test_01.py:20: AssertionError
======================== 2 failed, 4 passed, 3 warnings in 1.88s ======================
3.3.8 -v --verbose
? ? 使用-v/--verbose,常用于輸出詳細的運行信息。最主要的區別是每個文件中的每個測試函數都占用一行(之前是每一個文件占用一行),測試的結果和名字都會顯示出來,而不再在是一個點或字符,如下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --ff --tb=no -v
======================== test session starts =====================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- d:\program files\python\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 6 items
run-last-failure: rerun previous 2 failures first
test_01.py::test_add_02 FAILED [ 16%]
test_01.py::test_temp_01 FAILED [ 33%]
test_01.py::test_add_01 PASSED [ 50%]
test_01.py::test_request_01 PASSED [ 66%]
test_02.py::test_requestBaidu PASSED [ 83%]
test_03.py::test_request PASSED [100%]
====================== 2 failed, 4 passed, 3 warnings in 0.93s =============================
如果運行是彩色終端,則FAILED為紅色字體,PASS為綠色字體。
3.3.9 -q --quiet
? ? 該選項與-v功能相反,該選項會輸出簡化信息,只保留核心信息。如下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --ff --tb=line -q
FF.... [100%]
======================================== FAILURES ====================================
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:13: assert 7 == 6
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:20: assert 11 == 12
2 failed, 4 passed, 3 warnings in 1.34s
3.3.10 --tb=style
? ? --tb參數主要決定捕捉到失敗后的輸出信息的顯示方式。其可選值為 auto/long/short/line/native/no,其常用模式解釋如下:
- auto模式:默認值,如果有多個測試函數失敗,僅打印第一個和最后一個用例的回溯信息
- long模式:輸出的信息最詳細
- short模式:僅輸出assert的一行及系統判定內容,不顯示上下文
- line模式:只使用一行輸出顯示所有的錯誤信息
- native模式:只輸出Python標準庫的回溯信息,不顯示額外信息
- no模式:直接屏蔽全部回溯信息
? ? 以上模式常用模式為:short/line/no
某個測試函數運行失敗后,pytest會列舉出失敗信息,包括失敗出現在哪一行,什么原因導致的失敗,該過程稱之為信息回溯
示例如下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -v --tb=line
============================ test session starts ================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- d:\program files\python\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 6 items
test_01.py::test_add_01 PASSED [ 16%]
test_01.py::test_add_02 FAILED [ 33%]
test_01.py::test_request_01 PASSED [ 50%]
test_01.py::test_temp_01 FAILED [ 66%]
test_02.py::test_requestBaidu PASSED [ 83%]
test_03.py::test_request PASSED [100%]
==================================== FAILURES ==========================================
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:13: assert 7 == 6
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:20: assert 11 == 12
========================= 2 failed, 4 passed, 3 warnings in 2.10s ===========================
3.3.11 --durations=N
? ? --durations=N 該選項不關注測試具體的運行過程,只統計測試過程中哪向個階段最慢,因此使用該選項可以加快測試分節奏,顯示運行最慢的N個階段,耗時越長越靠前,如果--durations=0,則將所有階段的耗時從長到短排序后顯示。示例如下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --durations=3 --tb=line -v
============================test session starts ==========================================
platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- d:\program files\python\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01
collected 6 items
test_01.py::test_add_01 PASSED [ 16%]
test_01.py::test_add_02 FAILED [ 33%]
test_01.py::test_request_01 PASSED [ 50%]
test_01.py::test_temp_01 FAILED [ 66%]
test_02.py::test_requestBaidu PASSED [ 83%]
test_03.py::test_request PASSED [100%]
=============================== FAILURES =========================================
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:13: assert 7 == 6
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:20: assert 11 == 12
=============================== warnings summary ======================================
================================ slowest 3 test durations =================================
0.50s call test_03.py::test_request
0.06s call test_02.py::test_requestBaidu
(0.00 durations hidden. Use -vv to show these durations.)
======================= 2 failed, 4 passed, 3 warnings in 0.93s ===========================
3.3.12 -h --help
? ? 以上僅僅是常用的一些選項,更多選項可通過-h或--help來進行查看,通過該選項不但能展示原生pytest的用法,還能展示新添加插件的選項和用法。