一:環境准備
- 1.python3.6
- 2.windows環境
- 3.pycharm
- 4.allure-pytest
- 5.allure2.8.0
- 6.java1.8
allure-pytest快速安裝
在cmd中輸入 pip install allure-pytest,回車
二:報告生成
第1步:下載allure.zip,下載地址:allure-github: https://github.com/allure-framework/allure2 ,找到對應版本,並下載
第2步:解壓allure.zip,將路徑添加環境變量,path中,記得需要重啟電腦
第3步:驗證allure,在cmd中輸入allure,然后回車,如果可以看到一下,說明配置完成
第4步:運行測試用例 pytest.main(["-m","login","-s","-q","--alluredir=./report"])
"-m": 標記用例
"login": 被標記需要執行用例
"-s":允許終端在測試運行時輸出某些結果,例如你想輸入print的內容,可以加上-s
"-q"簡化輸出結果
"--alluredir": 生成allure指定語法
"./report":生成報告的路徑
"--clean-alluredir" :因為這個插件庫allure-pytest生成的報告文件,你第二次運行時候不會清理掉里面的東西,所以你需要刪除這個report文件夾,然后運行重新新建reoprt文件夾
說明:運行后,會在report文件夾里面生成文件
三.allure定制化報告
第1步:一些詞語解釋
一、feature: 標注主要功能模塊。
二、story: 標注Features功能模塊下的分支功能。
三、severity: 標注測試用例的重要級別。
1)blocker級別:中斷缺陷(客戶端程序無響應,無法執行下一步操作)
2)critical級別:臨界缺陷(功能點缺失)
3)normal級別:正常 默認為這個級別
4)minor級別:次要缺陷(界面錯誤與UI需求不符)
5)trivial級別:輕微缺陷(必輸項無提示,或者提示不規范)
四、step: 標注測試用例的重要步驟。
五、attach:用於向測試報告中輸入一些附加的信息,通常是一些測試數據信息。
1)name就是附件名稱,body就是數據,attachment_type就是傳類型
2)附件支持的類型(TEXT,HTML,XML,PNG,JPG,JSON,OTHER)
六、issue:這里傳的是一個連接,記錄的是你的問題。
七、testcase:這里傳的是一個連接,記錄的是你的用例。
八、description:描述用例信息
第2步:代碼展示+報表展示
feature方法的演示
代碼:
1 import pytest,allure 2 @allure.feature("測試") #標記代碼 3 class Test_Demo(): 4
5 # @allure.story("test_demo_1")
6 # @allure.severity("trivial")
7 def test_demo_1(self): 8 # """
9 # 用例描述:22222222222222
10 # """
11 #allure.MASTER_HELPER.description("11111111111111")
12 assert 1 == 1
報告展示:
story方法的演示
代碼:
1 import pytest,allure 2 @allure.feature("測試") 3 class Test_Demo(): 4 5 @allure.story("test_demo_1") #標記代碼 6 # @allure.severity("trivial") 7 def test_demo_1(self): 8 # """ 9 # 用例描述:22222222222222 10 # """ 11 #allure.MASTER_HELPER.description("11111111111111") 12 assert 1 == 1 13 @allure.story("test_demo_4") 標記代碼 14 #@allure.severity("minor") 15 def test_demo_4(self): 16 assert 3 == 3
報告展示:
severity方法的演示:
代碼:
1 import pytest,allure 2 @allure.feature("測試") 3 class Test_Demo(): 4 5 #@allure.story("test_demo_1") 6 @allure.severity("trivial") #標記代碼 7 def test_demo_1(self): 8 # """ 9 # 用例描述:22222222222222 10 # """ 11 #allure.MASTER_HELPER.description("11111111111111") 12 assert 1 == 1 13 #@allure.story("test_demo_4") 14 @allure.severity("minor") #標記代碼 15 def test_demo_4(self): 16 assert 3 == 3
報告展示:
setp方法的演示:
1.總的步驟備注
代碼:
1 import pytest,allure 2 @allure.feature("測試") 3 class Test_Demo(): 4 5 #@allure.story("test_demo_1") 6 #@allure.severity("trivial") 7 @allure.step("這是兩個負數的比較") #標記代碼 8 def test_demo_1(self): 9 # """ 10 # 用例描述:22222222222222 11 # """ 12 #allure.MASTER_HELPER.description("11111111111111") 13 assert -1 == -1 14 #@allure.story("test_demo_4") 15 #@allure.severity("minor") 16 @allure.step("這是兩個整數的比較") #標記代碼 17 def test_demo_4(self): 18 assert 3 == 3
報告展示:
2.分步驟備注
代碼:
1 import pytest,allure 2 @allure.feature("測試") 3 class Test_Demo(): 4 5 #@allure.story("test_demo_1") 6 #@allure.severity("trivial") 7 #@allure.step("這是兩個負數的比較") 8 def test_demo_1(self): 9 # """ 10 # 用例描述:22222222222222 11 # """ 12 #allure.MASTER_HELPER.description("11111111111111") 13 assert -1 == -1 14 #@allure.story("test_demo_4") 15 #@allure.severity("minor") 16 #@allure.step("這是兩個整數的比較") 17 def test_demo_4(self): 18 with allure.step("賦值一個變量a"): #標記代碼 19 a=1 20 with allure.step("賦值一個變量b"): #標記代碼 21 b=1 22 assert a == b
報表展示:
attach方法的演示:
代碼:
1 import pytest,allure 2 @allure.feature("測試") 3 class Test_Demo(): 4 5 #@allure.story("test_demo_1") 6 #@allure.severity("trivial") 7 #@allure.step("這是兩個負數的比較") 8 def test_demo_1(self): 9 # """ 10 # 用例描述:22222222222222 11 # """ 12 #allure.MASTER_HELPER.description("11111111111111") 13 assert -1 == -1 14 #@allure.story("test_demo_4") 15 #@allure.severity("minor") 16 #@allure.step("這是兩個整數的比較") 17 def test_demo_4(self): 18 a = 1 19 allure.attach("{0}".format(a),"預期結果") #標記代碼 第一個參數是body,第二個參數是name 20 b=1 21 allure.attach("{0}".format(b),"實際結果") #標記代碼 22 assert a == b
報告展示:
圖片附件形式:
代碼:
1 import pytest,allure 2 @allure.feature("測試") 3 class Test_Demo(): 4 5 @allure.story("test_demo_1") 6 @allure.severity("trivial") 7 def test_demo_1(self): 8 assert 1 == 1 9 10 def test_demo_3(self): 11 """ 12 用例描述:這里是兩個數字是否相等 13 """ 14 a = 3 15 b = 3 16 with open(r"G:\Web_automation\Learn_pytest\test_cases\img\2.jpg","rb") as file: #標記代碼,需要先打開圖片 17 file=file.read() #標記代碼,讀取圖片 18 allure.attach(file,"預期結果",attachment_type=allure.attachment_type.JPG) #標記代碼 19 assert a == b
報告展示:
issue和testcase方法的演示:
代碼:
1 import pytest,allure 2 @allure.feature("測試") 3 class Test_Demo(): 4 5 @allure.story("test_demo_1") 6 @allure.testcase("https://home.cnblogs.com/") 7 def test_demo_1(self): 8 assert 1 == 1 9 10 @allure.testcase("https://home.cnblogs.com/","測試用例地址請點擊跳轉") #標記代碼,你可以指定連接的名字,報告里面就會現在這個名字的連接 11 @allure.issue("http://www.baidu.com") #標記代碼,哪個寫在后,在報告里面就會顯示在前面 12 def test_demo_4(self): 13 assert 3 == 3
報表展示:
description方法的演示:
代碼:(這兩種備注方式不能同時是存在,如果同時存在他會先使用description的,而不取3引號的)
1 import pytest,allure 2 @allure.feature("測試") 3 class Test_Demo(): 4 5 6 @allure.story("test_demo_1") 7 @allure.testcase("https://home.cnblogs.com/") 8 def test_demo_1(self): 9 assert 1 == 1 10 11 @allure.description("這里是兩個3的比較") #標記代碼,優先取這個 12 def test_demo_4(self): 13 """ 14 用例描述:這里是兩個數字是否相等 #如果沒有description,那么就取3引號的 15 """ 16 assert 3 == 3
報告演示:
四.報告顯示
報告顯示方法一:
第1步:以上運行之后,可以在CMD中運行命令
allure generate report -o html --clean
report是alluredir生成的xml目錄,html是最終生成html的目錄
第2步:運行命令后,可以在html路徑下看到生成的數據,其中index.html就是我們要的allure報告,你可以在pycharm里面打開,報告展示如下
報告顯示方法二(這種相當於是調試):
第1步:以上運行之后,可以在CMD中運行命令
allure serve report (report是alluredir生成的xml目錄)
運行后,瀏覽器會自動跳轉到allure report界面
四.pytest+allure+jenkins集成
參考我另外的一個博客文章:https://www.cnblogs.com/hao2018/p/11135180.html
注意:在本地運行的時候需要手動把代碼拷貝到jenkins目錄下面的workspace文件夾里面;如果在git或svn上運行,jekins會直接把代碼拷貝到workspace目錄 ,如果jenkins是指定工作目錄就不用管了