pytest 執(zhí)行測試用例的幾種方法

????使用pytest框架執(zhí)行測試樣例的方法很多種,我們可以看下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,是因為它使用的執(zhí)行命令是?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

?

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

直接執(zhí)行pytest

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

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

切換到測試文件所在路徑,執(zhí)行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???????????????????????????????????????????????????#當前執(zhí)行目錄

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

?

test_1.py .???????????????????? #文件名+測試結果,點代表測試通過,F表示測試失敗,百分數表示執(zhí)行到該文件時所執(zhí)行的測試條目占總測試條目的百分比??????????????????????????????????????????????????????????????????????????????????????????????[ 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>


更進一步,可以指定到具體執(zhí)行的某個文件。這種情況下,文件的名字就可以不以test_作為前綴或以_test作為后綴,但文件內部的測試例仍需要按照規(guī)范書寫。如在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>

?????? 上述命令就是執(zhí)行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>

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

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,362評論 6 537
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 99,013評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,346評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,421評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,146評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,534評論 1 325
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,585評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,767評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 49,318評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,074評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,258評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,828評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,486評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,916評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,156評論 1 290
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,993評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,234評論 2 375

推薦閱讀更多精彩內容