一、allure簡介、使用
allure是一個生成測試報告的插件,支持常見語言,如python、Java等,生成美觀的測試報告。
下載、安裝: https://github.com/allure-framework/allure2/releases
配置環境變量:
打開終端輸入:open -t ~/.bash_profile
配置:
PATH="/Users/soft/allure-2.14.0/bin:${PATH}"
export PATH
保存后關閉文件,
設置立即生效:source ~/.bash_profile
檢查:allure --version
1.1 生成json
安裝
pip install allure-pytest
使用
在pytest的配置文件的命令行參數中加上如下代碼(指定報告生成的文件夾):
--alluredir ./report
例:
項目目錄:
# test_login.py
import pytest
class TestLogin(object):
def test_f1(self):
print("--- f1 ---")
assert 1
pytest配置文件:
[pytest]
# 添加命令行參數
addopts = -s --alluredir ./report/jsonfile
# 文件搜索路徑
testpaths = ./scripts
在terminal運行 pytest
命令即可在report目錄下生成一個json文件,里面包含了執行測試腳本的相關信息
1.2 將json轉成html
安裝
官網下載allure的zip包,解壓,將解壓包里面的 bin 目錄配置到系統環境變量 path 中,以Mac電腦為例:
如果在terminal中輸入 allure --version
,正確打印版本信息即可:
使用
在上面生成json文件的基礎上,在terminal 中輸入如下命令(注意在項目的根目錄下執行),即可在report目錄下生成html報告:
allure generate report/jsonfile -o report/html --clean
在瀏覽器打開報告:
1.3 參數與命令詳解
pytest配置文件中 addopts = -s --alluredir ./report/jsonfile
的 --alluredir ./report/jsonfile
是什么意思?
--alluredir 后面的 ./report/jsonfile 是執行腳本后相關的測試數據輸出的目錄名稱。目錄名稱不固定的,可以自定義,如 ./result/aaa
allure generate report/jsonfile -o report/html --clean
是什么意思?
report/jsonfile 表示執行測試后json數據的輸出目錄;
-o 表示output 輸出;
report/html 表示html文件輸出的目錄;
--clean 清除緩存,加上這個參數 相當於刪除原有的html文件,再創建新的
二、allure與pytest結合使用
給報告添加詳細的說明信息,增加可讀性。
2.1 添加測試步驟和測試描述
使用
添加測試步驟: 給測試方法添加@allure.step("測試步驟1") 或者 使用with allure.step("測試步驟1")
添加測試描述: 支持文本、圖片等。在需要添加描述的方法之前添加 allure.attach("描述詳細內容", name="描述標題")
例:
# test_login.py
import pytest
import allure
import json
# 模擬數據
url = "http://127.0.0.1"
method = "post"
class TestLogin(object):
@allure.step("執行登錄成功")
@pytest.mark.parametrize(("username", "pwd"), [("zhangsan", "123321"), ("lisi", "32322")])
def test_login_succ(self, username, pwd):
route = "/api/login"
data = {"username": username, "password": pwd}
# 添加文本描述
allure.attach(str(url + route), name="請求URL") # 方式1
allure.attach(json.dumps(data), "請求數據", allure.attachment_type.TEXT) # 方式2
# 添加圖片描述
allure.attach("./1.png", "圖片", allure.attachment_type.PNG)
assert 1
def test_add_employee(self):
data = {"name": "wangwu"}
route = "/api/login"
# 使用 with 添加測試步驟
with allure.step("執行新增員工"):
# 添加文本描述
allure.attach(str(url + route), name="請求URL")
allure.attach(json.dumps(data), name="請求數據")
assert 0
[pytest]
# 添加命令行參數
addopts = -s --alluredir ./report/jsonfile
# 文件搜索路徑
testpaths = ./scripts
在terminal 一次執行 pytest
和 allure generate report/jsonfile -o report/html --clean
,在瀏覽器打開測試報告:
2.2 添加測試級別
應用場景
給測試用例添加嚴重級別,出了bug后方便辨認哪些需要優先解決。
使用
給測試方法添加裝飾器 @allure.severity(allure.severity_level.BLOCKER)
總共5個級別:
BLOCKER 最嚴重
CRITICAL 嚴重
NORMAL 普通
MINOR 不嚴重
TRIVIAL 最不嚴重
例:
# test_login.py
import pytest
import allure
import json
# 模擬數據
url = "http://127.0.0.1"
method = "post"
class TestLogin(object):
@allure.step("執行登錄成功")
@allure.severity(allure.severity_level.CRITICAL)
@pytest.mark.parametrize(("username", "pwd"), [("zhangsan", "123321"), ("lisi", "32322")])
def test_login_succ(self, username, pwd):
route = "/api/login"
data = {"username": username, "password": pwd}
# 添加文本描述
allure.attach(str(url + route), name="請求URL") # 方式1
allure.attach(json.dumps(data), "請求數據", allure.attachment_type.TEXT) # 方式2
# 添加圖片描述
allure.attach("./1.png", "圖片", allure.attachment_type.PNG)
assert 1
@allure.severity(allure.severity_level.BLOCKER)
def test_add_employee(self):
data = {"name": "wangwu"}
route = "/api/login"
# 使用 with 添加測試步驟
with allure.step("執行新增員工"):
# 添加文本描述
allure.attach(str(url + route), name="請求URL")
allure.attach(json.dumps(data), name="請求數據")
assert 0
@allure.severity(allure.severity_level.NORMAL)
def test_f1(self):
assert 1
@allure.severity(allure.severity_level.MINOR)
def test_f2(self):
assert 0
@allure.severity(allure.severity_level.TRIVIAL)
def test_f3(self):
assert 1
def test_f4(self):
assert 1
[pytest]
# 添加命令行參數
addopts = -s --alluredir ./report/jsonfile
# 文件搜索路徑
testpaths = ./scripts
在terminal 一次執行 pytest
和 allure generate report/jsonfile -o report/html --clean
,在瀏覽器打開測試報告: