目錄
1.1.1CaculateAdd.py類(定義了add()和jian() 兩個方法)
1.1.2TestPytestHtmlDemo.py類(pytest運行demo:注意是Test開頭)
-v: 豐富信息模式, 輸出更詳細(xì)的用例執(zhí)行信息
2.1.1配置allure,環(huán)境變量path配置:新增allure的bin目錄下的路徑
?3.2運行后(多了allurePackage/response文件夾)
3.3Pytest和allure結(jié)合生成html格式的測試報告
一、pytest簡介
**需要安裝pytest和pytest-html(生成html測試報告) **
**pip install pytest 和 pip install pytest-html **
**命名規(guī)則 **
**Pytest單元測試中的類名和方法名必須是以test開頭,執(zhí)行中只能找到test開頭的類和方法,比unittest更加嚴(yán)謹(jǐn) **
unittest:Setup>> setupclass teardown teardownclass
Pytest的setup, setup_class和teardown, teardown_class函數(shù)(和unittest執(zhí)行效果一樣) 運行于測試方法的始末,
**setup,teardown :即:運行一次測試函數(shù)會運行一次setup和teardown **
setup_class,teardown_class:運行于測試方法的始末,但是不管有多少測試函數(shù)都只執(zhí)行一次setup_class和 teardown_class
1.1運行成功則在命令行顯示 類名+.
1.1.1CaculateAdd.py類(定義了add()和jian() 兩個方法)
class CaculateAddClass:
def add(self,a,b):
c = a+b
return c
def jian(self,a,b):
d = a-b
return d
1.1.2TestPytestHtmlDemo.py類(pytest運行demo:注意是Test開頭)
from PyTest.CaculateAdd import CaculateAddClass
import pytest
class TestPyDemoHtmlClass:
def test_1(self):
a = CaculateAddClass()
c = a.add(1, 2)
assert c == 3
#先運行test_1,這個test_2一會兒在放開注釋
# def test_2(self):
# a = CaculateAddClass()
# d = a.jian(3, 2)
# assert d == 1
#程序主入口不寫不運行
if __name__ == '__main__':
pytest.main(['TestPytestHtmlDemo.py'])#['類名']運行這個類
運行結(jié)果:(因為1+2=3,assert c==3,符合程序運行結(jié)果,正確)
. 點號,表示用例通過
F 表示失敗 Failure
E 表示用例中存在異常 Error
1.1.3運行幾個成功類名后面就幾個.
from PyTest.CaculateAdd import CaculateAddClass
import pytest
class TestPyDemoHtmlClass:
def test_1(self):
a = CaculateAddClass()
c = a.add(1, 2)
assert c == 3
def test_2(self):
a = CaculateAddClass()
d = a.jian(3, 2)
assert d == 1
#程序主入口不寫不運行
if __name__ == '__main__':
pytest.main(['TestPytestHtmlDemo.py'])#['類名']運行這個類
1.1.4運行錯誤的展示F
from PyTest.CaculateAdd import CaculateAddClass
import pytest
class TestPyDemoHtmlClass:
def test_1(self):
a = CaculateAddClass()
c = a.add(1, 2)
assert c == 4
#先運行test_1,這個test_2一會兒在放開注釋
# def test_2(self):
# a = CaculateAddClass()
# d = a.jian(3, 2)
# assert d == 1
#程序主入口不寫不運行
if __name__ == '__main__':
pytest.main(['TestPytestHtmlDemo.py'])#['類名']運行這個類
運行結(jié)果:
1.1.5運行幾個錯誤 類名后就展示幾個F
1.2Pytest生成自帶的html測試報告
1.在Pycharm安裝pytest自帶的測試報告包:
pip install pytest-html2.直接執(zhí)行pytest.main() 【自動查找當(dāng)前目錄下,以test_開頭的文件或者以_test結(jié)尾的py文件】
pytest.main("模塊.py") 【運行指定模塊下,運行所有test開頭的類和測試用例】
**3.python自帶的插件 **
pytest.main(["--html=./report.html","test3.py"])
# 程序主入口不寫不運行if __name__ == '__main__': pytest.main(["--html=./report.html", "TestPytestHtmlDemo.py"]) # 第一個參數(shù)是html,第二個是['類名']
1.2.1運行代碼如下:
from PyTest.CaculateAdd import CaculateAddClass
import pytest
class TestPyDemoHtmlClass:
def setup(self):
print("========setup========start")
def test_1(self):
a = CaculateAddClass()
c = a.add(1, 2)
assert c == 3
def test_2(self):
a = CaculateAddClass()
d = a.jian(3, 2)
assert d == 1
def teardown(self):
print("========teardown========end")
# 程序主入口不寫不運行
if __name__ == '__main__':
pytest.main(["--html=./report.html", "TestPytestHtmlDemo.py"]) # 第一個參數(shù)是html,第二個是['類名'] # pytest.main(['-x','TestPytestHtmlDemo.py'])#['類名']運行這個類
運行結(jié)果:
1.2.2打開123.html
1.3 pytest -x的使用等
pytest.main(['-x','--html=./report.html','t12est000.py'])
-x出現(xiàn)一條測試用例失敗就退出測試
-v: 豐富信息模式, 輸出更詳細(xì)的用例執(zhí)行信息
-s:顯示print內(nèi)容
-q: 簡化結(jié)果信息,不會顯示每個用例的文件名
二、allure開源測試報告
** Allure是一款輕量級并且非常靈活的開源測試報告框架。 它支持絕大多數(shù)測試框架, 例如TestNG、Pytest、JUint等。它簡單易用,易于集成。**
2.1安裝allure
首先要在Pycharm安裝:allure-pytest是Pytest的一個插件,通過它我們可以生成Allure所需要的用于生成測試報告的數(shù)據(jù)
allure:
pip install allure-pytest
2.1.1配置allure,環(huán)境變量path配置:新增allure的bin目錄下的路徑
三、pytest和alluredir的生成測試報告json
import pytest
class TestAllureClass:
def test1(self):
print("我是第一個數(shù)據(jù)")
def test2(self):
print("我是第二個數(shù)據(jù)")
def test3(self):
print("我是第三個數(shù)據(jù)")
if __name__ == '__main__':
#第一個是allure文件夾;
#第二個是數(shù)據(jù)(json/txt)返回到這個目錄下response文件夾下;
#第三個類名
pytest.main(['--alluredir','allurePackage/response','TestAllure.py'])
# pytest.main(['--html=./321.html','TestAllure.py'])
3.1運行前
image
3.2運行后(多了allurePackage/response文件夾)
3.3Pytest和allure結(jié)合生成html格式的測試報告
pytest.main(['--alluredir','allurePackage/response','TestAllure.py'])
## 將測試報告轉(zhuǎn)為html格式# --html=../report.html
split = 'allure ' + 'generate ' + './allurePackage/response ' + '-o ' + './allurePackage/html ' + '--clean' os.system(split)#system函數(shù)可以將字符串轉(zhuǎn)化成命令在服務(wù)器上運行
運行前:
運行后:
3.4index.html頁面Allure測試報告
image
四、Allure常用的幾個特性
@allure.feature # 用于描述被測試產(chǎn)品需求
@allure.story # 用于描述feature的用戶場景,即測試需求
with allure.step(): # 用于描述測試步驟,將會輸出到報告中
**allure.attach # 用于向測試報告中輸入一些附加的信息,通常是一些測試數(shù)據(jù),截圖等 **
4.1 @allure.feature使用
import pytest,os,allure
class TestAllureClass:
@allure.feature("用戶登錄功能")
def test1(self):
print("我是第一個數(shù)據(jù)")
@allure.feature("用戶注冊功能")
def test2(self):
print("我是第二個數(shù)據(jù)")
@allure.feature("用戶注冊/登錄功能")
def test3(self):
print("我是第三個數(shù)據(jù)")
if __name__ == '__main__':
# pytest.main(['--html=./321.html','TestAllure.py'])
#生成測試報告json #第一個是allure文件夾;
#第二個是數(shù)據(jù)(json/txt)返回到這個目錄下response文件夾下;
#第三個類名
pytest.main(['--alluredir','allurePackage/response','TestAllure.py'])
## 將測試報告轉(zhuǎn)為html格式# --html=../report.html
split = 'allure ' + 'generate ' + './allurePackage/response ' + '-o ' + './allurePackage/html ' + '--clean' os.system(split)#system函數(shù)可以將字符串轉(zhuǎn)化成命令在服務(wù)器上運行
運行后:
4.2@allure.story
import pytest,os,allure
class TestAllureClass:
@allure.feature("用戶登錄功能")
@allure.story("步驟1")
def test1(self):
print("我是第一個數(shù)據(jù)")
@allure.feature("用戶注冊功能")
@allure.story("步驟2")
def test2(self):
print("我是第二個數(shù)據(jù)")
@allure.feature("用戶注冊/登錄功能")
@allure.story("步驟3")
def test3(self):
print("我是第三個數(shù)據(jù)")
if __name__ == '__main__':
# pytest.main(['--html=./321.html','TestAllure.py'])
#生成測試報告json
#第一個是allure文件夾;
#第二個是數(shù)據(jù)(json/txt)返回到這個目錄下response文件夾下;
#第三個類名
pytest.main(['--alluredir','allurePackage/response','TestAllure.py'])
## 將測試報告轉(zhuǎn)為html格式# --html=../report.html
split = 'allure ' + 'generate ' + './allurePackage/response ' + '-o ' + './allurePackage/html ' + '--clean' os.system(split)#system函數(shù)可以將字符串轉(zhuǎn)化成命令在服務(wù)器上運行
運行結(jié)果:
4.4with allure.step("用戶登錄"): # 用于描述測試步驟,將會輸出到報告中
allure.attach("GUOYING","用戶名") # 用于向測試報告中輸入一些附加的信息,通常是一些測試數(shù)據(jù),截圖等
import pytest,os,allure
class TestAllureClass:
@allure.feature("用戶登錄功能")
@allure.story("步驟1")
def test1(self):
with allure.step("用戶登錄"):
# 用于描述測試步驟,將會輸出到報告中
allure.attach("GUOYING","用戶名")
# 用于向測試報告中輸入一些附加的信息,通常是一些測試數(shù)據(jù),截圖等
with allure.step("商品查看"):
allure.attach("瑪莎拉蒂","MC20")
print("我是第一個數(shù)據(jù)")
@allure.feature("用戶注冊功能")
@allure.story("步驟2") def test2(self):
print("我是第二個數(shù)據(jù)")
@allure.feature("用戶注冊/登錄功能")
@allure.story("步驟3")
def test3(self):
print("我是第三個數(shù)據(jù)")
if __name__ == '__main__':
# pytest.main(['--html=./321.html','TestAllure.py'])
#生成測試報告json
#第一個是allure文件夾;
#第二個是數(shù)據(jù)(json/txt)返回到這個目錄下response文件夾下;
#第三個類名
pytest.main(['--alluredir','allurePackage/response','TestAllure.py'])
## 將測試報告轉(zhuǎn)為html格式# --html=../report.html
split = 'allure ' + 'generate ' + './allurePackage/response ' + '-o ' + './allurePackage/html ' + '--clean' os.system(split)#system函數(shù)可以將字符串轉(zhuǎn)化成命令在服務(wù)器上運行
運行結(jié)果: