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