pytest 執行測試用例的幾種方法

????使用pytest框架執行測試樣例的方法很多種,我們可以看下pytest的幫助命令

C:\Users\DELL>pytest-h

usage: pytest[options] [file_or_dir] [file_or_dir] [...]

?

positionalarguments:

? file_or_dir

?

general:

? -k EXPRESSION???????? only run tests which match the givensubstring

??????????????????????? expression. Anexpression is a python evaluatable

??????????????????????? expression where allnames are substring-matched

??????????????????????? against test names andtheir parent classes. Example:

??????????????????????? -k 'test_method ortest_other' matches all test

???????????? ???????????functions and classes whose namecontains

??????????????????????? 'test_method' or'test_other', while -k 'not

??????????????????????? test_method' matchesthose that don't contain

??????????????????????? 'test_method' in theirnames. -k 'not test_method and

??????????????????????? not test_other' willeliminate the matches.

??????????????????????? Additionally keywordsare matched to classes and

??????????????????????? functions containingextra names in their

??????????????????????? 'extra_keyword_matches'set, as well as functions

??????????????????????? which have namesassigned directly to them.

? -m MARKEXPR?????????? only run tests matching given markexpression.

??????????????????????? example: -m 'mark1 andnot mark2'.

….

pytest 有時也被稱為 py.test,是因為它使用的執行命令是?py.test。本文中我們使用?pytest?指代這個測試框架,py.test?特指運行命令。

假如在D:\pytestwork目錄下存在兩個測試文件,分別為

#D:\pytestwork\test_1.py

def test_f1():

assert 1==1

def notest_f1():

assert 2==2

?

#D:\pytestwork\test_2.py

def test_f2():

??? assert3==3

?? ?

def notest_f2():

??? assert4==4

?

#D:\pytestwork\subdir

class Test_c1:

??? deftest_instr(self):

???????x="in china"

???????assert 'china' in x

???????

??? defnotest_instr(self):

???????x="in china"

???????assert 'chinese' in x

???????

def test_sub1():

assert 3==1

?

以上述文件為例,看一下測試執行情況

直接執行pytest

????????這種方式pytest(或用py.test)程序默認從當前目錄中搜集測試用例,即在哪個目錄下運行pytest命令,則從哪個目錄及其子目錄當中搜索測試腳本。事先配置好環境變量。格式命令如

py.test?????????????????????????# run all tests below current dir

切換到測試文件所在路徑,執行pytest

C:\Users\DELL>D:

D:\>cd pytestwork

?

D:\pytestwork>pytest

=================================================test session starts =================================================????????????????????????? #一段會話就是pytest的一次調用

platform win32 -- Python 3.6.4,pytest-5.0.1, py-1.8.0, pluggy-0.12.0?????#測試平臺、python版本、pytest版本

rootdir: D:\pytestwork???????????????????????????????????????????????????#當前執行目錄

collected 4 items???????????????????????????????????????????????????????#表示收集到4個測試條目

?

test_1.py .???????????????????? #文件名+測試結果,點代表測試通過,F表示測試失敗,百分數表示執行到該文件時所執行的測試條目占總測試條目的百分比??????????????????????????????????????????????????????????????????????????????????????????????[ 25%]

test_2.py .???????????????????????????????????????????????????????????????????????????????????????????????????????????????????[ 50%]

subdir\test_s1.py .F??????????????????????????????????????????????????????????????????????????????????????????????????????????[100%]

?

=============================================================FAILURES ==============================================================

_____________________________________________________________test_sub1 _____________________________________________________________

?

???def test_sub1():

>?????? assert 3==1

E?????? assert 3 ==1

?

subdir\test_s1.py:11: AssertionError????????? ?????????????#斷言異常所在的行數

================================================ 1failed, 3 passed in 0.22 seconds=================================================

?

D:\pytestwork>

?????? 注意這種情況下,文件名稱必須以“test_”開頭或“_test”結尾,否則失敗,僅僅test開頭或結尾也不行。


指定路徑

?????? pytest命令后接測試文件路徑運行特定路徑下的測試文件,格式

py.test somepath????? # run all tests below somepath

D:\pytestwork>pytest subdir

collected 2 items

?

subdir\test_s1.py .F??????????????????????????????????????????????????????????????????????????????????????????????????????????[100%]

?

=============================================================FAILURES ==============================================================

_____________________________________________________________test_sub1 _____________________________________________________________

?

??? deftest_sub1():

>??????assert 3==1

E??????assert 3 == 1

?

subdir\test_s1.py:11:AssertionError

================================================1 failed, 1 passed in 0.04 seconds=================================================

?

D:\pytestwork>


更進一步,可以指定到具體執行的某個文件。這種情況下,文件的名字就可以不以test_作為前綴或以_test作為后綴,但文件內部的測試例仍需要按照規范書寫。如在subdir下增加一個文件

D:\pytestwork\subdir\notest_s2.py

def test_f3():

??? assert3==3

???

def notest_f3():

??? assert4==4

?????? 運行

D:\pytestwork>pytest subdir\notest_s2.py

…..

collected 1 item

?

subdir\notest_s2.py .?????????????????????????????????????????????????????????????????????????????????????????????????????????[100%]

?

=====================================================1 passed in 0.13 seconds ======================================================

?

D:\pytestwork>


-k表達式

這將運行包含與給定字符串表達式匹配的名稱的測試,表達式中使用文件名,類名和函數名作為變量,使用and、or、not作為運算符,格式

py.test -k stringexpr # only run tests with namesthat match the

????????????????????? # the "stringexpression", e.g. "MyClass and not method"

????????????????????? # will select TestMyClass.test_something

????????????????????? # but notTestMyClass.test_method_simple

?????? 如

D:\pytestwork>pytest subdir\test_s1.py -k"not instr and sub1"

….

collected 2 items / 1 deselected / 1 selected

?

subdir\test_s1.py F?????????????????????????????????? ?????????????????????????????????????????????????????????????????????????[100%]

?

=============================================================FAILURES ==============================================================

_____________________________________________________________test_sub1 _____________________________________________________________

?

??? deftest_sub1():

>??????assert 3==1

E??????assert 3 == 1

?

subdir\test_s1.py:11:AssertionError

==============================================1 failed, 1 deselected in 0.05 seconds===============================================

?

D:\pytestwork>

?????? 上述"not instr and

sub1"表達式需要使用雙引號,不能使用單引號。該表達式的意思是pytest收集到的兩個測試項(Test_c1::test_instr、test_sub1)只有test_sub1匹配該表達式。

?????? 又如" instr and sub1"將不會匹配到任何測試項,因為沒有那個測試項的名稱中同時存在instr和sub1字符串。

D:\pytestwork>pytest subdir\test_s1.py -k "instr and sub1"

….

collected 2 items / 2 deselected

?

===================================================2 deselected in 0.01 seconds====================================================

?

D:\pytestwork>

nodeid

每個收集的測試都分配了一個唯一的nodeid,它由模塊文件名和后跟說明符組成,這些說明符來自參數化的類名,函數名,由::分隔,格式

py.test test_mod.py::test_func # only run teststhat match the "node ID",

??????????????????????????? ?????? # e.g "test_mod.py::test_func"will select

?????????????????????????????? # only test_funcin test_mod.py

?????? 如,

D:\pytestwork>py.test?"subdir\test_s1.py::Test_c1::test_instr"

….

collected 1 item

?

subdir\test_s1.py .???????????????????????????????????????????????????????????????????????????????????????????????????????????[100%]

?

=====================================================1 passed in 0.02 seconds ======================================================

?

D:\pytestwork>

?????? 上述命令就是執行test_s1模塊中Test_cl類中的test_instr方法。

main()方法

?????? 前面介紹的運行方法,都需要指定使用“pytest文件名”去運行,實際上我們可以直接運行某個文件,只不過需要在測試文件中倒入pytest,即聲明使用pytest框架運行這個文件,然后,使用main()方法調用。如修改test_s1.py文件

import pytest

?

class Test_c1:

??? deftest_instr(self):

???????x="in china"

???????assert 'china' in x

???????

??? defnotest_instr(self):

???????x="in china"

???????assert 'chinese' in x

???????

def test_sub1():

??? assert3==1

???

if __name__ == "__main__":

???pytest.main(["-q", "test_s1.py"])?????????? #-q表示減少冗余的輸出

?????? 測試如下

D:\pytestwork\subdir>test_s1.py

.F????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????[100%]

=============================================================FAILURES ==============================================================

_____________________________________________________________test_sub1 _____________________________________________________________

?

??? deftest_sub1():

>??????assert 3==1

E??????assert 3 == 1

?

test_s1.py:13:AssertionError

1 failed, 1 passed in 0.17 seconds

?

D:\pytestwork\subdir>

?????? 使用這種方法一個最大的好處是可以在命令行中傳入參數。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容