一、環境配置
1、安裝Python依賴庫
pip install pytest pip install pytest-allure-adaptor # pytest-allure-adaptor庫可以替換為pytest-allure-adaptor2
2、安裝allure-commandline工具
下載的allure-commandline為ZIP壓縮包,需要將解壓文件中bin目錄設置為環境變量。
注意:不要將allure-commandline解壓在Program Files (x86)目錄中,否則運行會報錯
allure-commandline百度雲盤下載:
鏈接:https://pan.baidu.com/s/1o2ptbdOKq2qOQYkQ0z2pKg
提取碼:wevw
官網下載地址:
https://www.mvnjar.com/ru.yandex.qatools.allure/allure-commandline/1.5.3/detail.html
3、驗證安裝是否成功與問題解決方法
在命令行輸入pytest和allure,若提示如下錯誤信息,是因為我們沒有將python目錄下的Scripts目錄配置到環境變量中
將python目錄下的Scripts目錄配置到環境變量后,再次輸入pytest,返回如下信息,則表示pytest安裝成功。
但是當我們在命令行輸入allure,卻返回錯誤信息“此時不應有 \Python36-32\allure-commandline\bin\..”,產生問題的原因是我們把allure-commandline解壓到“Program Files (x86)”中,需要更換安裝路徑為“Program Files”。
當更換allure-commandline目錄后,再次輸入allure,可能還會返回錯誤信息“此時不應有\java\jdk1.8.0_17\lib”,此時可能需要安裝64位的JDK(我沒試過更換目錄,直接重新安裝的64位JDK,所以不清楚直接更換目錄是否有用)
當輸入allure返回如下界面,則表示allure安裝成功
二、生成html報告命令
1、使用pytest生成xml報告
pytest --alluredir [xml_report_path] # [xml_report_path]根據自己需要定義文件夾,作者定義為:/report/xml
用例執行完成之后會在[xml_report_path]目錄下生成了一堆xml的report文件,當然這不是我們最終想要的美觀報告。
運行結果如下:
2、使用 CommandTool 生成我們需要的美觀報告。
allure generate [xml_report_path] -o [html_report_path] # [xml_report_path]為第一步生成xml報告的路徑 # [html_report_path]為輸出html報告的路徑,作者定義為:/report/html
打開 index.html,測試報告就會呈現在你面前
注⚠️:直接用chrome瀏覽器打開報告,報告可能會是空白頁面。
解決辦法:
1、在pycharm中右擊index.html選擇打開方式Open in Browser就可以了。
2、使用Firefox直接打開index.html。

三、定制報告
- Feature: 功能塊,feature功能分塊時比story大,即同時存在feature和story時,feature為父節點
- Story: 功能塊,具有相同feature或story的用例將規整到相同模塊下,執行時可用於篩選
- Severity: 標注測試用例的重要級別,包含blocker, critical, normal, minor, trivial 幾個不同的等級
- Step: 標注測試用例步驟
- Issue:問題標識,關聯標識已有BUG的問題,可為一個url鏈接地址
- TestCase: # 用例標識,關聯標識用例,可為一個url鏈接地址
- attach: 標注一些附加內容到測試報告中
- Environment: 標注環境Environment字段
1、Features定制詳解
- Feature: 功能塊,feature功能分塊時比story大,即同時存在feature和story時,feature為父節點
# coding=utf-8 import os import allure fileName = os.path.split(os.path.realpath(__file__))[1] @allure.feature(f"{fileName}文件feature標注的第一個用例") def test_case1(): a = 1 b = 1 c = a + b assert c == 2 @allure.feature(f"{fileName}文件feature標注的第二個用例") def test_case2(): a = "aaaaaaaaa" assert len(a) == 10
2、Story定制詳解
- Story: 功能塊,具有相同feature或story的用例將規整到相同模塊下,執行時可用於篩選
import os import allure fileName = os.path.split(os.path.realpath(__file__))[1] @allure.feature(f"{fileName}文件feature標注的用例") @allure.story(f"{fileName}文件story標注的第一個用例") def test_case1(): a = 1 b = 1 c = a + b assert c == 2 @allure.feature(f"{fileName}文件feature標注的用例") @allure.story(f"{fileName}文件story標注的第二個用例") def test_case2(): a = "aaaaaaaaa" assert len(a) == 10
3、用例標題和用例描述定制詳解
def的方法名稱為用例名稱,3引號注釋為用例描述
# coding=utf-8 import os import allure fileName = os.path.split(os.path.realpath(__file__))[1] @allure.feature(f"{fileName}") def test_caseName1(): # test_caseName1為用例title """ 用例描述:這是test_caseName1的用例描述 """ # 3引號注釋為用例描述 a = 1 b = 1 c = a + b assert c == 2 @allure.feature(f"{fileName}") def test_caseName2(): a = "aaaaaaaaa" assert len(a) == 10
4 、Severity定制詳解
- Severity: 標注測試用例的重要級別,包含blocker, critical, normal, minor, trivial 幾個不同的等級
1、 Blocker級別:中斷缺陷(客戶端程序無響應,無法執行下一步操作)
2、 Critical級別:臨界缺陷( 功能點缺失)
3、 Normal級別:普通缺陷(數值計算錯誤)
4、 Minor級別:次要缺陷(界面錯誤與UI需求不符)
5、 Trivial級別:輕微缺陷(必輸項無提示,或者提示不規范)
# coding=utf-8 import os import allure fileName = os.path.split(os.path.realpath(__file__))[1] @allure.feature(f"{fileName}") @allure.story("正向用力") @allure.severity('blocker') def test_case1(): a = "aaaaaaaaa" assert len(a) == 10 @allure.feature(f"{fileName}") @allure.story("正向用力") @allure.severity('critical') def test_case2(): a = "aaaaaaaaa" assert len(a) == 9 @allure.feature(f"{fileName}") @allure.story("反向用力") @allure.severity('normal') def test_case3(): a = "aaaaaaaaa" assert len(a) == 10 @allure.feature(f"{fileName}") @allure.story("反向用力") @allure.severity('minor') def test_case4(): a = "aaaaaaaaa" assert len(a) == 9 @allure.feature(f"{fileName}") @allure.story("反向用力") @allure.severity('trivial') def test_case5(): a = "aaaaaaaaa" assert len(a) == 9

5、Step定制詳解
- 標注測試用例步驟
# coding=utf-8 import os import allure fileName = os.path.split(os.path.realpath(__file__))[1] @allure.feature(f"{fileName}") @allure.story("正向用力") @allure.step('用例步驟') def test_case1(): """ 用例描述:計算兩步相加之和 """ with allure.step('step1:返回a的值'): a = 1 with allure.step('step2:返回b的值'): b = 2 with allure.step('step3:預期a+b=3'): assert a + b == 4
6、Issue和TestCase定制詳解
- Issue:問題標識,關聯標識已有BUG的問題,可為一個url鏈接地址
- TestCase: # 用例標識,關聯標識用例,可為一個url鏈接地址
# coding=utf-8 import os import allure fileName = os.path.split(os.path.realpath(__file__))[1] @allure.feature(f"{fileName}") @allure.story("正向用力") @allure.step('用例步驟') @allure.issue("http://www.baidu.com") @allure.testcase("http://www.testlink.com") def test_case1(): """ 用例描述:計算兩步相加之和 """ with allure.step('step1:返回a的值'): a = 1 with allure.step('step2:返回b的值'): b = 2 with allure.step('step3:預期a+b=3'): assert a + b == 4
7、Environment定制詳解
- Environment: 標注環境配置Environment字段
# coding=utf-8 import allure allure.environment(app_package='com.mobile.fm') allure.environment(test="hhhhhh") allure.environment(域名="127.0.0.1")
8、attach定制詳解
在報告中增加附件:allure.attach(’arg1’,’arg2’,’arg3’):
arg1
:是在報告中顯示的附件名稱arg2
:表示添加附件的內容arg3
:表示添加的類型(支持:HTML,JPG,PNG,JSON,OTHER,TEXTXML
)
# coding=utf-8 import os import allure fileName = os.path.split(os.path.realpath(__file__))[1] @allure.feature(f"{fileName}") @allure.story("正向用力") @allure.step('用例步驟') def test_case1(): """ 用例描述:計算兩步相加之和 """ with allure.step('step1:返回a的值'): allure.attach('背景1', '沒有背景') # attach可以打印一些附加信息 allure.attach('背景2', '沒有背景') a = 1 with allure.step('step2:返回b的值'): with open(r"C:\Users\Administrator\Desktop\qwe.jpg","rb") as f: f = f.read() allure.attach('圖片',f,allure.attach_type.JPG) b = 2 with allure.step('step3:預期a+b=3'): assert a + b == 4