前提:需要安裝pytest和pytest-html(生成html測(cè)試報(bào)告)
pip install pytest 和 pip install pytest-html
一友雳、命名規(guī)則
Pytest單元測(cè)試中的類(lèi)名和方法名必須是以test開(kāi)頭,執(zhí)行中只能找到test開(kāi)頭的類(lèi)和方法,比unittest更加嚴(yán)謹(jǐn)
案例
importpytestfromxml.domimportminidomclassTestPy01():deftestPy001(self):print("第一個(gè)pytest")assert1==1deftestPy002(self):print("第二個(gè)pytest")assert1==2deftestPy003(self):print("第三個(gè)pytest")assert1==1if__name__=='__main__':pytest.main()
unittest:Setup>>? setupclass , teardown >> teardownclass(課堂作業(yè))Pytest:? setup, setup_class和teardown, teardown_class函數(shù)(和unittest執(zhí)行效果一樣)
運(yùn)行于測(cè)試方法的始末饺藤,即:運(yùn)行一次測(cè)試函數(shù)會(huì)運(yùn)行一次setup和teardown
運(yùn)行于測(cè)試方法的始末,但是不管有多少測(cè)試函數(shù)都只執(zhí)行一次setup_class和 teardown_class
二流礁、Pytest生成自帶的html測(cè)試報(bào)告
前提條件:需要下載pytest-html模塊(python自帶的生成測(cè)試報(bào)告模塊)
pip install pytest-html
2.1 方式一
格式
pytest.main("模塊.py")【運(yùn)行指定模塊下神帅,運(yùn)行所有test開(kāi)頭的類(lèi)和測(cè)試用例】
pytest.main(["--html=./report.html","模塊.py"])
代碼:
pytest.main(["--html=../report1.html", "test_01.py"])
image-20210129181935523.png
2.2 方式二
格式
運(yùn)行指定模塊指定類(lèi)指定用例,冒號(hào)分割元镀,并生成測(cè)試報(bào)告
pytest.main([‘--html=./report.html’,‘模塊.py::類(lèi)::test_a_001'])
運(yùn)行指定模塊指定類(lèi)指定用例霎桅,冒號(hào)分割,并生成測(cè)試報(bào)告
代碼
pytest.main(["--html=../report1.html", "test_01.py::TestPy01::testPy001"])
2.3 方式三(無(wú)效)
格式
直接執(zhí)行pytest.main() 【自動(dòng)查找當(dāng)前目錄下遇革,以test_開(kāi)頭的文件或者以_test結(jié)尾的py文件】(課堂練習(xí)_test)
pytest.main([‘--html=./report.html’])
代碼:
pytest.main(["--html=../report1.html"])
2.4 方式四
Pytest調(diào)用語(yǔ)句
pytst.main(['-x','--html=./report.html','t12est000.py'])
-x:出現(xiàn)一條測(cè)試用例失敗就退出測(cè)試-v:豐富信息模式, 輸出更詳細(xì)的用例執(zhí)行信息-s:顯示print內(nèi)容-q:簡(jiǎn)化結(jié)果信息澳淑,不會(huì)顯示每個(gè)用例的文件名
擴(kuò)充:跳過(guò)
使用@pytest.mark.skip()跳過(guò)該用例(函數(shù))
@pytest.mark.skip()deftest001(self):assert2==2
三插佛、Pytest的運(yùn)行方式
.點(diǎn)號(hào)量窘,表示用例通過(guò)F表示失敗 FailureE表示用例中存在異常 Error
image.png
思考:實(shí)際開(kāi)發(fā)中是直接assert 1==2嗎?
四锨侯、文件讀取
4.1 讀取csv文件
先創(chuàng)建文件,然后讀取
importcsv#導(dǎo)入csv模塊classReadCsv():defread_csv(self):item=[]#定義一個(gè)空列表c=csv.reader(open("../commonDemo/test1.csv","r"))#得到csv文件對(duì)象forcsv_iinc:item.append(csv_i)#將獲取的數(shù)據(jù)添加到列表中returnitem? ? ? ? ? ? r=ReadCsv()print(r.read_csv())
4.2 讀取xml文件
fromxml.domimportminidomclassReadxml():defread_xml(self,filename,onename,twoname):root=minidom.parse(filename)firstnode=root.getElementsByTagName(onename)[0]secondnode=firstnode.getElementsByTagName(twoname)[0].firstChild.datareturnsecondnode
五叁怪、Allure
Allure是一款輕量級(jí)并且非常靈活的開(kāi)源測(cè)試報(bào)告框架深滚。 它支持絕大多數(shù)測(cè)試框架痴荐, 例如TestNG、Pytest生兆、JUint等鸦难。它簡(jiǎn)單易用,易于集成合蔽。
首先配置allure的環(huán)境變量
image.png
驗(yàn)證allure是否配置成功
image.png
其次要安裝allure
pip install allure-pytest
allure-pytest是Pytest的一個(gè)插件辈末,通過(guò)它我們可以生成Allure所需要的用于生成測(cè)試報(bào)告的數(shù)據(jù)
5.1 Allure常用的幾個(gè)特性
@allure.feature# 用于描述被測(cè)試產(chǎn)品需求@allure.story# 用于描述feature的用戶(hù)場(chǎng)景挤聘,即測(cè)試需求with allure.step():# 用于描述測(cè)試步驟,將會(huì)輸出到報(bào)告中allure.attach# 用于向測(cè)試報(bào)告中輸入一些附加的信息组去,通常是一些測(cè)試數(shù)據(jù)从隆,截圖等
5.1.1 allure.feature
@allure.feature # 用于描述被測(cè)試產(chǎn)品需求
5.1.2 allure.story
@allure.story # 用于描述feature的用戶(hù)場(chǎng)景,即測(cè)試需求
案例
實(shí)現(xiàn)用戶(hù)登錄功能键闺,場(chǎng)景為登錄成功和登錄失敗
importpytest,allure,osclassTestClass005():@allure.feature("用戶(hù)登錄功能")#用于定義被測(cè)試的功能辛燥,被測(cè)產(chǎn)品的需求點(diǎn)@allure.story("登錄成功")#用于定義被測(cè)功能的用戶(hù)場(chǎng)景缝其,即子功能點(diǎn)deftest_success(self):assert1==1@allure.feature("用戶(hù)登錄功能")#用于定義被測(cè)試的功能徘六,被測(cè)產(chǎn)品的需求點(diǎn)@allure.story("登錄失敗")#用于定義被測(cè)功能的用戶(hù)場(chǎng)景,即子功能點(diǎn)deftest_fail(self):assert1==2if__name__=='__main__':pytest.main(['--alluredir','report/result','test_06.py'])#生成json類(lèi)型的測(cè)試報(bào)告split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'#將測(cè)試報(bào)告轉(zhuǎn)為html格式os.system(split)# system函數(shù)可以將字符串轉(zhuǎn)化成命令在服務(wù)器上運(yùn)行
Pytest和allure效果展示
image-20210129185417915.png
5.1.3 with allure.step()
用于描述測(cè)試步驟漠其,將會(huì)輸出到報(bào)告中
5.1.4 allure.attach
用于向測(cè)試報(bào)告中輸入一些附加的信息辉懒,通常是一些測(cè)試數(shù)據(jù)谍失,截圖等
案例
實(shí)現(xiàn)產(chǎn)品信息展示,車(chē)展中的各種車(chē)的品牌
importpytest,os,allureclassTestShop():@allure.feature("購(gòu)物車(chē)")@allure.story("產(chǎn)品展示")deftestshow(self):withallure.step("查看哈吉利系列車(chē)信息"):allure.attach("博越","吉利")withallure.step("查看哈弗系列車(chē)信息"):allure.attach("H7","哈弗")if__name__=='__main__':pytest.main(['--alluredir','report/result','test_07.py'])split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'os.system(split)
Pytest和allure效果展示