????使用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ù)唉工。