前提:需要安裝pytest和pytest-html(生成html測試報告)
pip install pytest 和 pip install pytest-html
一肘习、命名規(guī)則
Pytest單元測試中的類名和方法名必須是以test開頭,執(zhí)行中只能找到test開頭的類和方法专执,比unittest更加嚴(yán)謹(jǐn)
案例
importpytestfromxml.domimportminidomclassTestPy01():deftestPy001(self):print("第一個pytest")assert1==1deftestPy002(self):print("第二個pytest")assert1==2deftestPy003(self):print("第三個pytest")assert1==1if__name__=='__main__':pytest.main()
unittest:Setup>>? setupclass , teardown >> teardownclass(課堂作業(yè))Pytest:? setup, setup_class和teardown, teardown_class函數(shù)(和unittest執(zhí)行效果一樣)
運行于測試方法的始末猜谚,即:運行一次測試函數(shù)會運行一次setup和teardown
運行于測試方法的始末,但是不管有多少測試函數(shù)都只執(zhí)行一次setup_class和 teardown_class
二紫新、Pytest生成自帶的html測試報告
前提條件:需要下載pytest-html模塊(python自帶的生成測試報告模塊)
pip install pytest-html
2.1 方式一
格式
pytest.main("模塊.py")【運行指定模塊下,運行所有test開頭的類和測試用例】
pytest.main(["--html=./report.html","模塊.py"])
代碼:
pytest.main(["--html=../report1.html", "test_01.py"])
image-20210129181935523.png
2.2 方式二
格式
運行指定模塊指定類指定用例恼琼,冒號分割炬太,并生成測試報告
pytest.main([‘--html=./report.html’,‘模塊.py::類::test_a_001'])
運行指定模塊指定類指定用例,冒號分割隆敢,并生成測試報告
代碼
pytest.main(["--html=../report1.html", "test_01.py::TestPy01::testPy001"])
2.3 方式三(無效)
格式
直接執(zhí)行pytest.main() 【自動查找當(dāng)前目錄下发皿,以test_開頭的文件或者以_test結(jié)尾的py文件】(課堂練習(xí)_test)
pytest.main([‘--html=./report.html’])
代碼:
pytest.main(["--html=../report1.html"])
2.4 方式四
Pytest調(diào)用語句
pytst.main(['-x','--html=./report.html','t12est000.py'])
-x:出現(xiàn)一條測試用例失敗就退出測試-v:豐富信息模式, 輸出更詳細(xì)的用例執(zhí)行信息-s:顯示print內(nèi)容-q:簡化結(jié)果信息,不會顯示每個用例的文件名
擴充:跳過
使用@pytest.mark.skip()跳過該用例(函數(shù))
@pytest.mark.skip()deftest001(self):assert2==2
三拂蝎、Pytest的運行方式
.點號穴墅,表示用例通過F表示失敗 FailureE表示用例中存在異常 Error
image.png
思考:實際開發(fā)中是直接assert 1==2嗎?
四、文件讀取
4.1 讀取csv文件
先創(chuàng)建文件玄货,然后讀取
importcsv#導(dǎo)入csv模塊classReadCsv():defread_csv(self):item=[]#定義一個空列表c=csv.reader(open("../commonDemo/test1.csv","r"))#得到csv文件對象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是一款輕量級并且非常靈活的開源測試報告框架。 它支持絕大多數(shù)測試框架誉结, 例如TestNG鹅士、Pytest、JUint等惩坑。它簡單易用掉盅,易于集成。
首先配置allure的環(huán)境變量
image.png
驗證allure是否配置成功
image.png
其次要安裝allure
pip install allure-pytest
allure-pytest是Pytest的一個插件以舒,通過它我們可以生成Allure所需要的用于生成測試報告的數(shù)據(jù)
5.1 Allure常用的幾個特性
@allure.feature# 用于描述被測試產(chǎn)品需求@allure.story# 用于描述feature的用戶場景趾痘,即測試需求with allure.step():# 用于描述測試步驟,將會輸出到報告中allure.attach# 用于向測試報告中輸入一些附加的信息蔓钟,通常是一些測試數(shù)據(jù)永票,截圖等
5.1.1 allure.feature
@allure.feature # 用于描述被測試產(chǎn)品需求
5.1.2 allure.story
@allure.story # 用于描述feature的用戶場景,即測試需求
案例
實現(xiàn)用戶登錄功能滥沫,場景為登錄成功和登錄失敗
importpytest,allure,osclassTestClass005():@allure.feature("用戶登錄功能")#用于定義被測試的功能侣集,被測產(chǎn)品的需求點@allure.story("登錄成功")#用于定義被測功能的用戶場景,即子功能點deftest_success(self):assert1==1@allure.feature("用戶登錄功能")#用于定義被測試的功能兰绣,被測產(chǎn)品的需求點@allure.story("登錄失敗")#用于定義被測功能的用戶場景世分,即子功能點deftest_fail(self):assert1==2if__name__=='__main__':pytest.main(['--alluredir','report/result','test_06.py'])#生成json類型的測試報告split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'#將測試報告轉(zhuǎn)為html格式os.system(split)# system函數(shù)可以將字符串轉(zhuǎn)化成命令在服務(wù)器上運行
Pytest和allure效果展示
image-20210129185417915.png
5.1.3 with allure.step()
用于描述測試步驟,將會輸出到報告中
5.1.4 allure.attach
用于向測試報告中輸入一些附加的信息缀辩,通常是一些測試數(shù)據(jù)臭埋,截圖等
案例
實現(xiàn)產(chǎn)品信息展示,車展中的各種車的品牌
importpytest,os,allureclassTestShop():@allure.feature("購物車")@allure.story("產(chǎn)品展示")deftestshow(self):withallure.step("查看哈吉利系列車信息"):allure.attach("博越","吉利")withallure.step("查看哈弗系列車信息"):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效果展示