pytest(3):pytest運行參數介紹


前言

pytest 帶有很多參數,可以使用 pytest --help  來查看幫助文檔,下面介紹幾種常用的參數:  

無參數

讀取路徑下所有符合規則的文件,類,方法,函數全部執行。使用方法如下:

 pytest 或者 py.test 

-v 參數

打印詳細運行日志信息,一般在調試的時候加上這個參數,終端會打印出每條用例的詳細日志信息,方便定位問題。使用方法如下:

 pytest -v 

不加-v和加-v的對比:

 -s 參數

帶控制台輸出結果,當你的代碼里面有 print 輸出語句,如果想在運行結果中打印 print 輸出的代碼,在運行的時候可以添加 -s 參數,一般在調試的時候使用,使用方法如下: pytest -s  

-k 參數

跳過運行某個或者某些用例。

使用方法如下:

pytest -k '類名' 
pytest -k '方法名' 
pytest -k "類名 and not 方法名" //運行類里所有的方法,不包含某個方法
 1 # Author xuejie zeng
 2 # encoding utf-8
 3 #測試demo
 4 
 5 import pytest
 6 
 7 class Testone:
 8     def test_a(self):
 9         print("我是第一條用例")
10     def test_b(self):
11         print("我是第二條用例")
12     def c(self):
13         print("c")
14 
15 class Testtwo:
16     def test_d(self):
17         print("我是第三條用例")
pytest -k '類名',運行指定的類名下的用例 

pytest -k '方法名',運行指定的用例

pytest -k "類名 and not 方法名" ,只運行了類里面的第二條用例,不包括第一條用例。

-x 參數

遇到用例失敗立即停止運行,一旦發現有失敗的用例即中止運行

使用方法如下:

 pytest -x 
# Author xuejie zeng
# encoding utf-8


class Testone:
    def test_a(self):
        print("我是第一條用例")
        a = 1
        b = 2
        assert a == b
    def test_b(self):
        print("我是第二條用例")
        a = 1
        b = 1
        assert a == b
    def c(self):
        print("c")

class Testtwo:
    def test_d(self):
        print("我是第三條用例")
        a = 10
        b = 20
        assert a != b

 用例遍歷到了3條,但是由於第一條就失敗了所以后面的2條就不執行了。

--maxfail 參數

用例失敗個數達到閥值停止運行。具體用法: 

pytest --maxfail=[num]  當失敗數等於設置的閾值時,后面的2條用例就不執行了。

 1 (venv) D:\Tools\pycharm_2020\New_2020\pytest_doc\test>pytest --maxfail=1 testdemo.py  2 
 3 collected 3 items                                                                                                                                         
 4 
 5 testdemo.py F
 6 
 7 ======================================================================== FAILURES ========================================================================
 8 _____________________________________________________________________ Testone.test_a _____________________________________________________________________
 9 
10 self = <pytest_doc.test.testdemo.Testone object at 0x000001AEB0EB0EB8>
11 
12     def test_a(self):
13         print("我是第一條用例")
14         a = 1
15         b = 2
16 >       assert a == b
17 E       assert 1 == 2
18 
19 testdemo.py:12: AssertionError
20 ------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------
21 我是第一條用例
22 ================================================================ short test summary info =================================================================
23 FAILED testdemo.py::Testone::test_a - assert 1 == 2
24 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
25 =================================================================== 1 failed in 0.05s ====================================================================

當失敗數小於設置的閾值時,后面的2條用例繼續執行。

 1 (venv) D:\Tools\pycharm_2020\New_2020\pytest_doc\test>pytest --maxfail=2 testdemo.py  2 
 3 collected 3 items                                                                                                                                         
 4 
 5 testdemo.py F..                                                                                                                                     [100%]
 6 
 7 ======================================================================== FAILURES ========================================================================
 8 _____________________________________________________________________ Testone.test_a _____________________________________________________________________
 9 
10 self = <pytest_doc.test.testdemo.Testone object at 0x000001DD0EADFEF0>
11 
12     def test_a(self):
13         print("我是第一條用例")
14         a = 1
15         b = 2
16 >       assert a == b
17 E       assert 1 == 2
18 
19 testdemo.py:12: AssertionError
20 ------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------
21 我是第一條用例
22 ================================================================ short test summary info =================================================================
23 FAILED testdemo.py::Testone::test_a - assert 1 == 2
24 ============================================================== 1 failed, 2 passed in 0.05s ===============================================================

-m 參數

將運行有 @pytest.mark.[標記名] 這個標記的測試用例。

使用方法如下:

 pytest -m [標記名]   
 1 # Author xuejie zeng
 2 # encoding utf-8
 3 
 4 
 5 import pytest
 6 
 7 class Testone:
 8     def test_a(self):
 9         print("我是第一條用例")
10         a = 1
11         b = 2
12         assert a == b
13     def test_b(self):
14         print("我是第二條用例")
15         a = 1
16         b = 1
17         assert a == b
18 
19 class Testtwo:
20     @pytest.mark.d
21     def test_d(self):
22         print("我是第三條用例")
23         a = 10
24         b = 20
25         assert a != b

 用例遍歷到了3條用例,2條被忽略,1條被選中,也就是加了標記的被選中了。

 

可以關注個人公眾號:測試開發進階之路


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM