pytest命令行方式運行用例


背景

用命令行方式調用用例是我們最常用的方式,這方面確實比java的TestNG框架要好用許多,至少不用寫xml文件,為了提供定制化運行用例的方式,pytest提供了許多運行命令以供定制化運行某一類測試用例或者某個測試用例等

pycharm里命令行運行用例

在pycharm里寫好了測試用例后如何運行呢?pycharm里好像並沒有像eclipse里提供TestNG用的插件一樣可以一鍵執行的方式,那么我們可以使用命令行的方式來進行,如下圖所示為一個用例文件:

 

 

 代碼如下:

 1 #-*- coding: utf-8 -*-
 2 import pytest
 3 
 4 
 5 class Test_simple():
 6 
 7     @pytest.mark.test
 8     def test_case1(self):
 9         print("testCase1")
10         tof = True
11         assert tof
12 
13     @pytest.mark.normal
14     @pytest.mark.test
15     def test_case2(self):
16         print("testCase2")
17         tof = False
18         assert tof
19 
20     def test_case3(self):
21         print("testCase3")
22         assert True
23 
24     @pytest.mark.test
25     def setup_class(self):
26         print("用於test組")
27 
28     @pytest.mark.normal
29     def setup_class(self):
30         print("用於normal組")

如上所示tests目錄下包含了多個模塊,在res_platform目錄下添加了一些測試用例即test_Simple和test_getRecity
想要運行用例時可以打開下方的Terminal窗口:

 

 

 會自動切換到當前工程目錄下,而后即可使用pytest的命令了

終端中使用pytest

在終端中使用pytest也是和在pycharm中類似,如下以windows系統為例:
先切換到用例所在工程或者目錄而后運行pytest即可,如下:

 

 

 linux系統中也是同樣的使用方法,只是如果沒有為pytest添加軟連接,則需要在pytest前面加上python命令;

用例全部運行

全部運行時不需要添加任何后綴,只需要添加命令pytest即可,此時打印的信息比較簡單:

 

 

 

打印詳情-v

如上圖所示,只顯示了用例時成功還是失敗,至於里邊的log則沒有打印,那么如果我們想要看運行詳細信息怎么辦呢?可以加上-v標簽,如下:

 1 (venv) D:\dev\gemini\tests\res_platform>pytest -v
 2 ======================================================================================================= test session starts =======================================================================================================
 3 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:\dev\venv\scripts\python.exe
 4 cachedir: .pytest_cache
 5 rootdir: D:\dev\gemini\tests\res_platform, inifile:
 6 plugins: allure-pytest-2.8.40, json-0.4.0
 7 collected 6 items                                                                                                                                                                                                                  
 8 
 9 test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                               [ 16%]
10 test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                               [ 33%]
11 test_Simple.py::Test_simple::test_case3 PASSED                                                                                                                                                                               [ 50%]
12 test_getRecCity.py::test_reccity[4-E2237250698--] PASSED                                                                                                                                                                     [ 66%]
13 test_getRecCity.py::test_reccity[4--1623811147711.43stfy-] PASSED                                                                                                                                                            [ 83%]
14 test_getRecCity.py::test_reccity[4---50772109410758277886] PASSED                                                                                                                                                            [100%]
15 
16 ============================================================================================================ FAILURES =============================================================================================================
17 _____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________
18 
19 self = <tests.res_platform.test_Simple.Test_simple object at 0x000001EFB493C048>
20 
21     @pytest.mark.normal
22     @pytest.mark.test
23     def test_case2(self):
24         print("testCase2")
25         tof = False
26 >       assert tof
27 E       assert False
28 
29 test_Simple.py:18: AssertionError
30 ------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------
31 testCase2
32 =============================================================================================== 1 failed, 5 passed in 0.39 seconds ================================================================================================

如上圖會把詳細信息都打印出來

 

指定組別

如果用例中包含多個分組,想要只運行其中一個組,則使用-m "組名"的方式,依然使用如上代碼,運行命令和結果如下:

 1 (venv) D:\dev\gemini\tests\res_platform>pytest -s -m "normal"
 2 ======================================================================================================= test session starts =======================================================================================================
 3 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1
 4 rootdir: D:\dev\gemini\tests\res_platform, inifile:
 5 plugins: allure-pytest-2.8.40, json-0.4.0
 6 collected 6 items / 5 deselected / 1 selected                                                                                                                                                                                      
 7 
 8 test_Simple.py 用於normal組
 9 testCase2
10 F
11 
12 ============================================================================================================ FAILURES =============================================================================================================
13 _____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________
14 
15 self = <tests.res_platform.test_Simple.Test_simple object at 0x000001B57B05E908>
16 
17     @pytest.mark.normal
18     @pytest.mark.test
19     def test_case2(self):
20         print("testCase2")
21         tof = False
22 >       assert tof
23 E       assert False
24 
25 test_Simple.py:18: AssertionError
26 ============================================================================================= 1 failed, 5 deselected in 0.16 seconds ==============================================================================================

 

使用表達式指定某些用例-k

-k選項允許我們設置表達式來運行某些用例,如下傳參就只運行了test_case1test_case2

(venv) D:\dev\gemini\tests\res_platform>pytest -v -k "case1 or case2"
======================================================================================================= test session starts =======================================================================================================
platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:\dev\venv\scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\dev\gemini\tests\res_platform, inifile:
plugins: allure-pytest-2.8.40, json-0.4.0
collected 6 items / 4 deselected / 2 selected                                                                                                                                                                                      

test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                               [ 50%]
test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                               [100%]

============================================================================================================ FAILURES =============================================================================================================
_____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________

self = <tests.res_platform.test_Simple.Test_simple object at 0x0000026C2ECCE0B8>

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testCase2")
        tof = False
>       assert tof
E       assert False

test_Simple.py:18: AssertionError
------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------
testCase2
======================================================================================== 1 failed, 1 passed, 4 deselected in 0.21 seconds =========================================================================================

表達式的寫法有許多,可以用全稱如test_case1這樣也可以去掉test_,除了or外也可以使用not來指定那些用例不跑;

遇到失敗即停止運行-x

pytest的原本運行規則是每條用例均執行,不管是否有失敗,如果我們想在用例運行時遇到失敗即停止,則可以使用-x,如下所示,第二條用例失敗后則不再運行第三條用例:

(venv) D:\dev\gemini\tests\res_platform>pytest -v -x
======================================================================================================= test session starts =======================================================================================================
platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:\dev\venv\scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\dev\gemini\tests\res_platform, inifile:
plugins: allure-pytest-2.8.40, json-0.4.0
collected 6 items                                                                                                                                                                                                                  

test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                               [ 16%]
test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                               [ 33%]

============================================================================================================ FAILURES =============================================================================================================
_____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________

self = <tests.res_platform.test_Simple.Test_simple object at 0x000001BAD12ECCC0>

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testCase2")
        tof = False
>       assert tof
E       assert False

test_Simple.py:18: AssertionError
------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------
testCase2
=============================================================================================== 1 failed, 1 passed in 0.16 seconds ================================================================================================

 

指定運行某個測試py文件

指定運行某個py文件,只需要接上文件相對路徑即可:

 1 (venv) D:\dev\gemini\tests\res_platform>pytest -v res_platform/test_getRecCity.py
 2 ======================================================================================================= test session starts =======================================================================================================
 3 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:\dev\venv\scripts\python.exe
 4 cachedir: .pytest_cache
 5 rootdir: D:\dev\gemini\tests\res_platform, inifile:
 6 plugins: allure-pytest-2.8.40, json-0.4.0
 7 collecting ... 
 8 ================================================================================================== no tests ran in 0.01 seconds ===================================================================================================
 9 ERROR: file not found: res_platform/test_getRecCity.py
10 
11 
12 (venv) D:\dev\gemini\tests\res_platform>pytest -v test_Simple.py
13 ======================================================================================================= test session starts =======================================================================================================
14 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:\dev\venv\scripts\python.exe
15 cachedir: .pytest_cache
16 rootdir: D:\dev\gemini\tests\res_platform, inifile:
17 plugins: allure-pytest-2.8.40, json-0.4.0
18 collected 3 items                                                                                                                                                                                                                  
19 
20 test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                               [ 33%]
21 test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                               [ 66%]
22 test_Simple.py::Test_simple::test_case3 PASSED                                                                                                                                                                               [100%]
23 
24 ============================================================================================================ FAILURES =============================================================================================================

指定運行某個class

寫法為:py文件路徑::class名稱,范例如下:

 1 (venv) D:\dev\gemini\tests\res_platform>pytest -v test_Simple.py
 2 ======================================================================================================= test session starts =======================================================================================================
 3 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:\dev\venv\scripts\python.exe
 4 cachedir: .pytest_cache
 5 rootdir: D:\dev\gemini\tests\res_platform, inifile:
 6 plugins: allure-pytest-2.8.40, json-0.4.0
 7 collected 3 items                                                                                                                                                                                                                  
 8 
 9 test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                               [ 33%]
10 test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                               [ 66%]
11 test_Simple.py::Test_simple::test_case3 PASSED                                                                                                                                                                               [100%]
12 
13 ============================================================================================================ FAILURES =============================================================================================================
14 _____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________
15 
16 self = <tests.res_platform.test_Simple.Test_simple object at 0x0000024213191E80>
17 
18     @pytest.mark.normal
19     @pytest.mark.test
20     def test_case2(self):
21         print("testCase2")
22         tof = False
23 >       assert tof
24 E       assert False
25 
26 test_Simple.py:18: AssertionError
27 ------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------

指定運行某個方法:

寫法為:py文件路徑::class名稱::method名稱,范例如下:

 1 (venv) D:\dev\gemini\tests>pytest -v res_platform/test_Simple.py::Test_simple
 2 ======================================================================================================= test session starts =======================================================================================================
 3 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:\dev\venv\scripts\python.exe
 4 cachedir: .pytest_cache
 5 rootdir: D:\dev\gemini\tests, inifile:
 6 plugins: allure-pytest-2.8.40, json-0.4.0
 7 collected 3 items                                                                                                                                                                                                                  
 8 
 9 res_platform/test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                  [ 33%]
10 res_platform/test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                  [ 66%]
11 res_platform/test_Simple.py::Test_simple::test_case3 PASSED                                                                                                                                                                  [100%]
12 
13 ============================================================================================================ FAILURES =============================================================================================================
14 _____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________
15 
16 self = <tests.res_platform.test_Simple.Test_simple object at 0x0000014460FC2EB8>
17 
18     @pytest.mark.normal
19     @pytest.mark.test
20     def test_case2(self):
21         print("testCase2")
22         tof = False
23 >       assert tof
24 E       assert False
25 
26 res_platform\test_Simple.py:18: AssertionError
27 ------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------
28 testCase2
29 =============================================================================================== 1 failed, 2 passed in 0.11 seconds ================================================================================================

 

其他

pytest還包含許多其他用法,具體用法可以使用pytest --help來查看,如下:

 

 

 

 

  

 


免責聲明!

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



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