目錄
- 一、前提準備
- 二、pytest生成自帶的html測試報告
- 三、pytest運行方式
- 四、allure
一、前提準備
1、前提:需要安裝pytest和pytest-html(生成html測試報告)
pip install pytest 和 pip install pytest-html
安裝插件:pip install 插件名
2、命名規(guī)范
Pytest單元測試中的類名和方法名必須是以test開頭,執(zhí)行中只能找到test開頭的類和方法,比unittest更加嚴謹
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
如果不安裝pytest-html會報:
![](/d/20211017/d5cf08d04bf33e50b2d91cfe90cd8207.gif)
案例: 1)
pytest.main("模塊.py")【運行指定模塊下,運行所有test開頭的類和測試用例】
pytest.main(["--html=./report.html","模塊.py"])
import pytest
class Test():
def test1(self):
print("這是測試1")
def test1(self):
print("這是測試2")
if __name__ == '__main__':
pytest.main(["--html=./report.html", "test_004.py"])
結果:
![](/d/20211017/1a43d8d74c35e4b0ceaabd3d3cd9f955.gif)
![](/d/20211017/040bdd1730528344dc41a318d9274bb7.gif)
2)運行指定模塊指定類指定用例,冒號分割,并生成測試報告
pytest.main([‘--html=./report.html',‘模塊.py::類::test_a_001'])
import pytest
class Test():
def test1(self):
print("這是測試1")
def test2(self):
print("這是測試2")
if __name__ == '__main__':
pytest.main(["--html=./report.html", "test_004.py::Test::test1"])
結果:
![](/d/20211017/c6146f983c67783d261493bb312e7e0e.gif)
3)直接執(zhí)行pytest.main() 【自動查找當前目錄下,以test 開頭的文件或者以test結尾的py文件】
pytest.main([‘--html=./report.html'])
語句: pytst.main(['-x','--html=./report.html','t12est000.py'])
-x出現(xiàn)一條測試用例失敗就退出測試
-s:顯示print內(nèi)容
三、pytest運行方式
. 點號,表示用例通過
F 表示失敗 Failure
E 表示用例中存在異常 Error
四、allure
Allure是一款輕量級并且非常靈活的開源測試報告框架。 它支持絕大多數(shù)測試框架, 例如TestNG、Pytest、JUint等。它簡單易用,易于集成
1、Allure常用的幾個特性
@allure.feature # 用于描述被測試產(chǎn)品需求
@allure.story # 用于描述 feature 的用戶場景,即測試需求
with allure.step (): # 用于描述測試步驟,將會輸出到報告中
allure.attach # 用于向測試報告中輸入一些附加的信息,通常是一些測試數(shù)據(jù),截圖等
案例1:關于pytest與Allure生成html測試用例 rr.csv
2,3,5
5,6,11
readCsv
import csv # 導入csv模塊
class ReadCsv():
def read_csv(self):
item = [] # 定義一個空列表
c = csv.reader(open("../dataDemo/rr.csv", "r")) # 得到csv文件對象
for csv_i in c:
item.append(csv_i) # 將獲取的數(shù)據(jù)添加到列表中
return item
r = ReadCsv()
print(r.read_csv())
開發(fā)代碼:
class Cale():
def jia(self,a,b):
c=a+b
return c
def jian(self,a,b):
c=a-b
return c
def cheng(self,a,b):
c=a*b
return c
def chu(self,a,b):
c=a/b
return c
生成html代碼:
import pytest
from pytest01.readDemo.readCsv import ReadCsv
from pytest01.demo.cale import Cale
import os
import allure
r=ReadCsv()
cc=r.read_csv()
d=Cale()
class Test():
@allure.story("加法函數(shù)測試正確")
def test001(self):
for i in cc:
dd=d.jia(int(i[0]),int(i[1]))
assert dd==int(i[2])
if __name__ == '__main__':
pytest.main(['--alluredir', 'report/result', 'test_02.py'])
split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
os.system(split)
![](/d/20211017/e913391bac4b77dc3992747757dafa29.gif)
![](/d/20211017/437605f722f476edf3016988d9fb5008.gif)
到此這篇關于python單元測試之pytest的使用的文章就介紹到這了,更多相關pytest的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python測試框架unittest和pytest區(qū)別
- Python 測試框架unittest和pytest的優(yōu)劣
- 詳解Pytest測試用例的執(zhí)行方法
- 詳解如何使用Pytest進行自動化測試
- Pytest測試框架基本使用方法詳解
- Pytest執(zhí)行unittest TestSuite(測試套件)的實現(xiàn)方法