更多學習教程關注公眾號:程序員一凡
1.概述
基于JavaScript代碼庫的自動化測試框架坡垫,通過腳本語言傻铣,模擬用戶行為操作咽白,最接近用戶真實場景,實現(xiàn)對web自動測試篓足。
Selenium段誊,是目前的最火爆企業(yè)最主流的webUI自動化框架
pytest是一個非常成熟的全功能的Python測試框架,是unittest框架的擴展栈拖,主要特點有以下幾點:
[if !supportLists]l?[endif]1枕扫、簡單靈活,非常方便的組織自動化測試用例辱魁;
[if !supportLists]l?[endif]2烟瞧、支持參數化诗鸭,可以細粒度地控制要測試的測試用例;
[if !supportLists]l?[endif]3参滴、能夠支持簡單的單元測試和復雜的功能測試强岸,比如web端selenium/移動端appnium等自動化測試、request接口自動化測試
[if !supportLists]l?[endif]4砾赔、pytest具有很多第三方插件蝌箍,并且可以自定義擴展,比如測試報告生成暴心,失敗重運行機制
[if !supportLists]l?[endif]5妓盲、測試用例的skip和fail處理;
[if !supportLists]l?[endif]6专普、結合業(yè)界最美的測試報告allure+Jenkins悯衬,持續(xù)集成
pytest
pip install -Upytest-rerunfailures???? #重試運行cases
pip install? pytest-html??? #生成測試報告
pytest--version????? #會展示當前已安裝版本
編寫pytest測試樣例非常簡單,只需要按照下面的規(guī)則:
[if !supportLists]l?[endif]測試文件以test_開頭(以_test結尾也可以)
[if !supportLists]l?[endif]測試類以Test開頭檀夹,并且不能帶有init 方法
[if !supportLists]l?[endif]測試函數以test_開頭
[if !supportLists]l?[endif]斷言使用基本的assert即可
?import pytest #引入pytest包
??????? pytest.main(['-s', 'class01.py'])
可以包含一個或多個Test開頭的測試類筋粗,test_開頭的函數
? pytest.main(['-s', 'test.py'])
pytest.main(['-s','test.py','--html=./report/result.html'])
:表示當前路徑下生成report文件夾,result.html文件
5.2 批量運行用例:pytest-xdist
pytest.main(['-s','test.py','--html=./report/result.html','-n=2'])
運行用例:pytest.main(['-s', '-q', '--alluredir',
'./report/xml'])
????????????????pytest測試目標文件--alluredir 數據目錄
???????????????運行后的結果炸渡,是生成xml的數據集合
生成報告:allure generate --clean ./report/xml/ -o
./results/html/
當自動化用例越來越龐大的時候蚌堵,很多用例的數據可以共享买决,復用,讓用例腳本可讀性吼畏,維護性更高策州,比如登錄等
conftest.py 配置里可以實現(xiàn)數據共享,比如py跨文件共享前置
不需要import導入conftest.py够挂,pytest用例會自動查找
自從使用了pytest框架,愛了愛了藕夫,深深的感受到它的可擴展性和靈活性孽糖,老板再也不用擔心我的自動化效率低
更多學習教程關注公眾號:程序員一凡