pytest使用
1、pytest簡介
pytest是一個(gè)非常成熟的全功能的Python測試框架改抡,主要有以下幾個(gè)特點(diǎn):
- 簡單靈活矢炼,容易上手,支持參數(shù)化阿纤。
- 能夠支持簡單的單元測試和復(fù)雜的功能測試句灌,還可以用來做selenium/appnium等自動化測試、接口自動化測試(pytest+requests)欠拾。
- pytest具有很多第三方插件胰锌,并且可以自定義擴(kuò)展,比較好用的如pytest-selenium(集成selenium)藐窄、pytest-html(完美html測試報(bào)告生成)资昧、pytest-rerunfailures(失敗case重復(fù)執(zhí)行)、pytest-xdist(多CPU分發(fā))等荆忍。
- 測試用例的skip和xfail處理格带。
- 可以很好的和jenkins集成,CICD使用刹枉。
- report框架--allure也支持了pytest叽唱。
安裝:
python3 -m pip install pytest
各種測試框架眾多,測試業(yè)務(wù)集成框架的難度不一微宝,排除框架已有便利不說棺亭,個(gè)人感覺關(guān)鍵在于下面幾點(diǎn):
- 業(yè)務(wù)相關(guān)核心代碼的封裝,提供統(tǒng)一的接口蟋软,使用例補(bǔ)充調(diào)用便捷镶摘,非web類測試業(yè)務(wù)尤甚。
- 清晰的調(diào)試過程信息輸出钟鸵,密切聯(lián)系業(yè)務(wù)邏輯钉稍,進(jìn)行關(guān)鍵性信息的埋點(diǎn)輸出,并提供信息等級配置等棺耍。
- 測試用例的補(bǔ)充贡未,斷言的合理性。
2蒙袍、如何使用
假定的我的目錄結(jié)構(gòu)為下圖所示:
|- msc_py # 根目錄
|- bvt_test # bvt自動化功能回歸(pytest)
- isr_test.py # 聽寫測試
isr_test.py
部分代碼如下:
@pytest.mark.aqc2vinfo
class TestAqc2vinfoClass(object):
def test_aqc2vinfo_equal_one(self):
print("\r\n")
res_kv = mac_test(
msc_params="sub=iat,ent=sms16k,aue=raw,prs=1,ars=1,auf=audio/L16;rate=16000,aqc=1,vinfo=1")
res_json_list = res_kv['result_json_list']
record_common_msg(res_kv)
assert "aqc" in res_json_list[0]
assert "vad" in res_json_list[0]
def test_vinfo_equal_one(self):
print("\r\n")
res_kv = mac_test(
msc_params="sub=iat,ent=sms16k,vad_speech_tail=3000,prs=1,ptt=0,rate=16000,rst=json,rse=utf8,aqc=1,vinfo=0")
res_json_list = res_kv['result_json_list']
record_common_msg(res_kv)
assert "aqc" in res_json_list[0]
assert "vad" not in res_json_list[0]
2.1 編寫pytest測試樣例
編寫pytest測試樣例非常簡單俊卤,只需要按照下面的規(guī)則:
- 測試文件以test_開頭(以_test結(jié)尾也可以)。
- 測試類以Test開頭害幅,并且不能帶有
init
方法消恍。 - 測試函數(shù)以test_開頭。
- 斷言使用基本的assert即可以现。
2.2 運(yùn)行模式
運(yùn)行時(shí)狠怨,直接切到測試用例目錄執(zhí)行pytest
即可约啊,下面是一些參數(shù)介紹。
2.2.1 運(yùn)行后生成測試報(bào)告(htmlReport)
生成html報(bào)告需要安裝一個(gè)插件佣赖。
python3 -m pip install pytest-html
運(yùn)行命令:
pytest --html=./report.html
2.2.2 運(yùn)行指定的用例
如需要運(yùn)行執(zhí)行的用例時(shí)恰矩,有幾種場景。
1憎蛤、直接運(yùn)行isr_test.py
下面所有用例外傅。
pytest isr_test.py
輸出如下:
(venv) F:\desktop\work\python\py36-32\batrec\bvt_test>pytest isr_test.py
================================================================================================ test session starts =================================================================================================
platform win32 -- Python 3.6.7, pytest-4.2.0, py-1.7.0, pluggy-0.8.1
rootdir: F:\desktop\work\python\py36-32\batrec\bvt_test, inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.5.5
collected 80 items
isr_test.py
……
不需要這些版本等信息的話,可以加參數(shù)如下俩檬,quiet模式執(zhí)行:
pytest -q isr_test.py
這里會全部用例挨個(gè)執(zhí)行萎胰,一個(gè)點(diǎn)一個(gè)點(diǎn)的打出來表示成功。
對應(yīng)的有打印詳細(xì)信息:
pytest -v isr_test.py
2棚辽、直接運(yùn)行isr_test.py
下TestAqc2vinfoClass
下的兩個(gè)用例技竟。
pytest isr_test.py::TestAqc2vinfoClass
這里可以看到類名上面有@pytest.mark.aqc2vinfo
這個(gè)裝飾器,這個(gè)的作用是選擇特定的用例執(zhí)行屈藐。
only run tests matching given mark expression.example: -m 'mark1 and not mark2'.
pytest isr_test.py -v -s -m 'aqc2vinfo'
效果等同上面指定類名運(yùn)行灵奖,但它可擴(kuò)展運(yùn)行更多的用例。
3估盘、直接運(yùn)行isr_test.py
下TestAqc2vinfoClass
下的test_aqc2vinfo_equal_one
方法用例。
pytest isr_test.py::TestAqc2vinfoClass::test_aqc2vinfo_equal_one
以上運(yùn)行時(shí)骡尽,不利于調(diào)試遣妥,類似print、log等打屏信息沒有輸出攀细,可加參數(shù)設(shè)置如下:
pytest --capture=no isr_test.py::TestAqc2vinfoClass::test_aqc2vinfo_equal_one
或短寫形式:
pytest -s isr_test.py::TestAqc2vinfoClass::test_aqc2vinfo_equal_one
2.2.3 多進(jìn)程運(yùn)行用例
當(dāng)用例過多時(shí)箫踩,運(yùn)行所有的用例也會用時(shí)過長,如果想縮短用例運(yùn)行時(shí)間谭贪,即要多進(jìn)程來運(yùn)行境钟。
安裝pytest-xdist
插件:
python3 -m pip install pytest-xdist
運(yùn)行模式:
pytest -v -s isr_test.py -n NUM
NUM為想要并發(fā)的進(jìn)程數(shù)目。
效果如下:
(venv) F:\desktop\work\python\py36-32\batrec\bvt_test>pytest -v -s -n 5 isr_test.py
================================================================================================ test session starts =================================================================================================
platform win32 -- Python 3.6.7, pytest-4.2.0, py-1.7.0, pluggy-0.8.1 -- f:\desktop\work\python\py36-32\venv\scripts\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.7', 'Platform': 'Windows-10-10.0.16299-SP0', 'Packages': {'pytest': '4.2.0', 'py': '1.7.0', 'pluggy': '0.8.1'}, 'Plugins': {'xdist': '1.26.1', 'metadata': '1.8.0', 'html': '1.20.0', 'forked
': '1.0.2', 'allure-pytest': '2.5.5'}, 'JAVA_HOME': 'E:\\software\\java\\jdk1.8'}
rootdir: F:\desktop\work\python\py36-32\batrec\bvt_test, inifile:
plugins: xdist-1.26.1, metadata-1.8.0, html-1.20.0, forked-1.0.2, allure-pytest-2.5.5
[gw0] win32 Python 3.6.7 cwd: F:\desktop\work\python\py36-32\batrec\bvt_test
[gw1] win32 Python 3.6.7 cwd: F:\desktop\work\python\py36-32\batrec\bvt_test
[gw2] win32 Python 3.6.7 cwd: F:\desktop\work\python\py36-32\batrec\bvt_test
[gw3] win32 Python 3.6.7 cwd: F:\desktop\work\python\py36-32\batrec\bvt_test
[gw4] win32 Python 3.6.7 cwd: F:\desktop\work\python\py36-32\batrec\bvt_test
[gw0] Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)]
[gw1] Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)]
[gw2] Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)]
[gw3] Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)]
[gw4] Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 12:45:02) [MSC v.1900 32 bit (Intel)]
gw0 [80] / gw1 [80] / gw2 [80] / gw3 [80] / gw4 [80]
scheduling tests via LoadScheduling
2.2.4 重試運(yùn)行用例
在做接口測試時(shí)俭识,有時(shí)會因?yàn)榫W(wǎng)絡(luò)等問題慨削,導(dǎo)致用例運(yùn)行失敗,這時(shí)可以通過重試運(yùn)行用例來解決套媚。
安裝pytest-rerunfailures
插件:
python3 -m pip install pytest-rerunfailures
運(yùn)行模式:
pytest -v -s isr_test.py -retuns NUM
NUM為想要重試的次數(shù)缚态。
3、常用插件介紹
推薦已經(jīng)使用過常用插件:
- pytest-html堤瘤,生成html格式報(bào)告玫芦,本地調(diào)試推薦使用。(已集成)
- pytest-xdist本辐,開啟多個(gè)worker進(jìn)程桥帆,同時(shí)執(zhí)行多個(gè)測試用例医增,達(dá)到并發(fā)運(yùn)行的效果,大大提升構(gòu)建效率老虫。(已集成)
- pytest-rerunfailures叶骨,自動重跑失敗用例。
- pytest-cache张遭,重跑上次失敗的用例邓萨,持續(xù)集成中很實(shí)用,提高分析效率菊卷。
- pytest-sugar缔恳,改變了pytest的默認(rèn)外觀,增加了一個(gè)進(jìn)度條洁闰,并立即顯示失敗的測試歉甚。(已集成)
- pytest-ordering,可指定一個(gè)測試套中的所有用例執(zhí)行順序。
- pytest-assume,多斷言扑眉,第一個(gè)斷言失敗纸泄,第二個(gè)仍會執(zhí)行。
- pytest-cov,單元測試過程中腰素,指標(biāo):行覆蓋率聘裁。
pytest插件及兼容性,這里可以看到更多的插件弓千。
4衡便、高階使用
4.1 pytest相關(guān)
4.1.1 pytest原生
此節(jié)段暫不適用,保留待補(bǔ)充洋访。
4.1.2 pytest插件
1镣陕、allsure使用
持續(xù)構(gòu)建,集成CICD時(shí)姻政,需要生成漂亮的allure報(bào)告呆抑,網(wǎng)上坑比較多,感謝pytest+allure配置使用汁展。
這里以平臺win為例鹊碍,其它平臺參見allsure官檔。
a食绿、安裝PowerShell (win10自帶有妹萨,其他系統(tǒng)自行安裝)。
b炫欺、打開PowerShell,輸入命令乎完。
set-executionpolicy remotesigned -s cu
有提示的話輸入[Y]。
再輸入:
iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
即scoop help
品洛,出現(xiàn)選項(xiàng)幫助即安裝成功树姨。
c摩桶、安裝allsure
.
scoop install allure
d、進(jìn)入isr_test.py
文件的目錄下帽揪,執(zhí)行硝清。
pytest --alluredir=reports
這里可能會報(bào)錯(cuò),不行就把reports
換成絕對路徑F:\desktop\work\python\py36-32\batrec\bvt_test\reports
转晰。
pytest --alluredir=F:\desktop\work\python\py36-32\batrec\bvt_test\reports
然后當(dāng)前目錄中會多一個(gè)reports文件夾芦拿,里面是各種txt和json文件。
繼續(xù)當(dāng)前目錄執(zhí)行:
allure generate reports
存放用例的目錄中會多一個(gè)allure-reports
文件夾查邢,更新allure-reports
文件夾內(nèi)容使用
allure generate reports --clean
最后復(fù)制allure-reports
目錄下index路徑蔗崎,火狐打開,不要用其它瀏覽器扰藕,會有問題缓苛。
同樣的allure也有一些用法,具體見上面的allure-pytest官檔邓深。
使用實(shí)例:
pytest -v -s -m aqc2vinfo --alluredir=F:\desktop\work\python\py36-32\batrec\bvt_test\reports isr_test.py
再allure生成在線報(bào)告未桥,它會在默認(rèn)的瀏覽器中打開:
allure serve reports
終極使用:
pytest -v -s -n 5 --alluredir=F:\desktop\work\python\py36-32\batrec\bvt_test\reports isr_test.py
4.2 奇技淫巧
這個(gè)命名,足以表明內(nèi)心很興奮的樣子了,即python必不可少的ide編輯器pycharm
芥备。它本身就不介紹了冬耿,下面是說它集成pytest。
打開設(shè)置:
File | Settings | Tools | Python Integrated Tools | default test runner | py.test
接下來就是很舒服的東西了萌壳,再次打開isr_test.py
代碼淆党,每個(gè)類,方法讶凉,它的左側(cè)就有一個(gè)可執(zhí)行的綠色三角按鈕,點(diǎn)一下山孔,就可以跑用例了懂讯,較與上面的pytest使用不遑多讓,用例補(bǔ)充調(diào)試可謂很爽了台颠。
5褐望、其它
腳本運(yùn)行期間如遇python拋錯(cuò),腳本添加下面類似代碼進(jìn)行運(yùn)行調(diào)試串前,找對應(yīng)的維護(hù)人員瘫里。
test = TestAqc2vinfoClass()
test.test_aqc2vinfo_equal_one()
再運(yùn)行:
python3 ist_test.py