一、命名規(guī)則
類名和方法名必須以test開頭
二、pytest生成自帶的html測試報告
格式:pytest.main(["--html=./report.html","模塊.py"])
格式:pytest.main([‘--html=./report.html’,‘模塊.py::類::test_a_001']) 指明所調(diào)用的類和函數(shù)
格式:pytest.main([‘--html=./report.html’]) 無效的
-
格式:pytst.main(['-x','--html=./report.html','t12est000.py'])
-x:出現(xiàn)一條測試用例失敗就退出測試 -v:豐富信息模式, 輸出更詳細的用例執(zhí)行信息 -s:顯示print內(nèi)容 -q:簡化結(jié)果信息,不會顯示每個用例的文件名 @pytest.mark.skip() def test001(self): assert 2==2
三、pytest的運行方式
. 點號,表示用例通過
F 表示失敗 Failure
E 表示用例中存在異常 Error
四、文件讀取
-
讀取csv文件
先創(chuàng)建文件,然后讀取
import csv #導(dǎo)入csv模塊 class ReadCsv(): def read_csv(self): item =[] #定義一個空列表 c = csv.reader(open("../commonDemo/test1.csv","r")) #得到csv文件對象 for csv_i in c: item.append(csv_i) #將獲取的數(shù)據(jù)添加到列表中 return item r = ReadCsv() print(r.read_csv()) -
讀取xml文件
from xml.dom import minidom class Readxml(): def read_xml(self,filename,onename,twoname): root =minidom.parse(filename) firstnode =root.getElementsByTagName(onename)[0] secondnode=firstnode.getElementsByTagName(twoname)[0].firstChild.data return secondnode
五、Allure
Allure是一款輕量級并且非常靈活的開源測試報告框架,它支持絕大多數(shù)測試框架
1.下載、配置環(huán)境變量、驗證allure是否成功
2.安裝allure:pip install allure-pytest
- Allure常用的幾個特性
@allure.feature # 用于描述被測試產(chǎn)品需求
@allure.story # 用于描述feature的用戶場景,即測試需求
with allure.step(): # 用于描述測試步驟,將會輸出到報告中
allure.attach # 用于向測試報告中輸入一些附加的信息,通常是一些測試數(shù)據(jù),截圖等