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(xiàn)表示測試失敗念链,百分數(shù)表示執(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????????? ?????????????#斷言異常所在的行數(shù)

================================================ 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表達式

這將運行包含與給定字符串表達式匹配的名稱的測試梦重,表達式中使用文件名,類名和函數(shù)名作為變量亮瓷,使用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于置,它由模塊文件名和后跟說明符組成,這些說明符來自參數(shù)化的類名贞岭,函數(shù)名八毯,由::分隔,格式

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>

?????? 使用這種方法一個最大的好處是可以在命令行中傳入?yún)?shù)唉工。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末研乒,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子淋硝,更是在濱河造成了極大的恐慌雹熬,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谣膳,死亡現(xiàn)場離奇詭異竿报,居然都是意外死亡,警方通過查閱死者的電腦和手機继谚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門烈菌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事僧界∏揉郑” “怎么了?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵捂襟,是天一觀的道長。 經常有香客問我欢峰,道長葬荷,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任纽帖,我火速辦了婚禮宠漩,結果婚禮上,老公的妹妹穿的比我還像新娘懊直。我一直安慰自己扒吁,他們只是感情好,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布室囊。 她就那樣靜靜地躺著雕崩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪融撞。 梳的紋絲不亂的頭發(fā)上盼铁,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天,我揣著相機與錄音尝偎,去河邊找鬼饶火。 笑死,一個胖子當著我的面吹牛致扯,可吹牛的內容都是我干的肤寝。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼抖僵,長吁一口氣:“原來是場噩夢啊……” “哼鲤看!你這毒婦竟也來了?” 一聲冷哼從身側響起裆针,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤刨摩,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后世吨,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體澡刹,經...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年耘婚,在試婚紗的時候發(fā)現(xiàn)自己被綠了罢浇。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖嚷闭,靈堂內的尸體忽然破棺而出攒岛,到底是詐尸還是另有隱情,我是刑警寧澤胞锰,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布灾锯,位于F島的核電站,受9級特大地震影響嗅榕,放射性物質發(fā)生泄漏顺饮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一凌那、第九天 我趴在偏房一處隱蔽的房頂上張望兼雄。 院中可真熱鬧,春花似錦帽蝶、人聲如沸赦肋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽佃乘。三九已至,卻和暖如春麦锯,著一層夾襖步出監(jiān)牢的瞬間恕稠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工扶欣, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留鹅巍,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓料祠,卻偏偏與公主長得像骆捧,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子髓绽,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內容