3.pytest命令行參數
3.1 pytest控制檯信息詳解
一般在運行pytest以後,會出現以下所示的控制檯信息:html
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 =====
測試會話分隔符,表明啓動了一個新的調用python
- platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
當前運行的平臺,Python、pytest、pytest包的版本等信息微信
- rootdir
當前起始目錄,pytest搜索測試代碼時使用的目錄session
- 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的用法,還能展現新添加插件的選項和用法。
原文地址:https://www.cnblogs.com/surpassme/p/13252310.html
本文同步在微信訂閱號上發佈,如各位小夥伴們喜歡個人文章,也能夠關注個人微信訂閱號:woaitest,或掃描下面的二維碼添加關注: