Pytest+Allure使用示例教程
Allure是開源的免費的自動化測試報告,支持Java,Python,我們來看看如何使用Python pytest與Allure整合,輸出漂亮的測試報告。
一.Allure安裝
windows:下載,解壓,并配置環(huán)境變量:https://github.com/allure-framework/allure2/releases
mac:brew install allure
二.Allure報告結(jié)構(gòu)
1.Overview:整體數(shù)據(jù)顯示。
2.Suites:用例集合,按照套件和類分組的已執(zhí)行測試的標(biāo)準(zhǔn)結(jié)構(gòu)表示形式。
3.Behaviors:對于行為驅(qū)動的方法,此選項卡根據(jù)Epic、Feature和Story標(biāo)記對測試結(jié)果進行分組。
4.Categories:“類別”選項卡提供了創(chuàng)建自定義缺陷分類以應(yīng)用測試結(jié)果的方法。
5.Graphs:用圖表顯示測試數(shù)據(jù)中收集的不同統(tǒng)計數(shù)據(jù),狀態(tài)分解或嚴(yán)重性和持續(xù)時間圖。
6.Packages:軟件包選項卡表示測試結(jié)果的樹狀布局,按不同的包名分組。
7.Timeline:時間軸選項卡可視化測試執(zhí)行的回顧,allure適配器收集測試的精確時間,在這個選項卡上,它們相應(yīng)地按照順序或并行的時間結(jié)構(gòu)排列。
三.安裝Python依賴
windows:pip install allure-pytest mac:pip3 install allure-pytest
這將安裝Allure-pytest和Allure-python-commons包,以生成與Allure 2兼容的報告數(shù)據(jù)。
1.Windows
2.Mac
四.基本用法
Allure監(jiān)聽器在測試執(zhí)行期間會收集結(jié)果,只需添加alluredir選項,并選擇輸出的文件路徑即可。
pytest --alluredir=./allure-results
# 執(zhí)行此命令,將在默認(rèn)瀏覽器中顯示生成的報告 allure serve ./allure-results
五.Allure注解說明:
六.具體使用方法(以Mac系統(tǒng)為例)
基本報告:可以在Allure報告中看到所有默認(rèn)的pytest狀態(tài): 只有由于斷言錯誤而未成功的測試才會被標(biāo)記為失敗,任何其他異常都會導(dǎo)致測試狀態(tài)失敗。
import allure import pytest @allure.feature('test_success') def test_success(): """this test succeeds""" assert True @allure.feature('test_failure') def test_failure(): """this test fails""" assert False @allure.feature('test_skip') def test_skip(): """this test is skipped""" pytest.skip('for a reason!') @allure.feature('test_broken') def test_broken(): raise Exception('oops') if __name__ == '__main__': # pytest.main(["-s","allure-test.py"]) ''' -q: 安靜模式, 不輸出環(huán)境信息 -v: 豐富信息模式, 輸出更詳細(xì)的用例執(zhí)行信息 -s: 顯示程序中的print/logging輸出 ''' pytest.main(['-s', '-q','test_allure02.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
命令行輸入allure generate -c -o allure-reports,即可在當(dāng)前路徑下生成測試報告。(-c:清空歷史數(shù)據(jù),-o:指定輸出測試報告路徑),如果使用allure serve allure-results,則會在系統(tǒng)默認(rèn)目錄下生成測試報告
七.支持Pytest特性
Allure報告支持的一些常見Pytest特性,包括xfails、fixture和finalizers、marks、條件跳過和參數(shù)化。
1.xfail
功能未實現(xiàn)或者有Bug尚未修復(fù),當(dāng)測試通過時盡管會失敗,它是一個xpass,將在測試摘要中報告。
import allure import pytest @allure.feature('test_xfail_expected_failure') @pytest.mark.xfail(reason='該功能尚未實現(xiàn)') def test_xfail_expected_failure(): print("該功能尚未實現(xiàn)") assert False @allure.feature('test_xfail_unexpected_pass') @pytest.mark.xfail(reason='該Bug尚未修復(fù)') def test_xfail_unexpected_pass(): print("該Bug尚未修復(fù)") assert True if __name__ == '__main__': pytest.main(['-s', '-q','test_allure02.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
2.skipif
當(dāng)條件為True,跳過執(zhí)行
import sys import allure import pytest '''當(dāng)條件為True則跳過執(zhí)行''' @allure.feature("test_skipif") @pytest.mark.skipif("darwin" in sys.platform,reason="如果操作系統(tǒng)是Mac則跳過執(zhí)行") def test_skipif(): print("操作系統(tǒng)是Mac,test_skipif()函數(shù)跳過執(zhí)行") # --clean-alluredir:每次執(zhí)行前清空數(shù)據(jù),這樣在生成的報告中就不會追加,只顯示當(dāng)前執(zhí)行的用例 if __name__ == '__main__': pytest.main(['-s', '-q','test_allure02.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
3.parametrize
可以使用@Pytest.mark.paramtrize進行參數(shù)化,所有參數(shù)名稱和值都將被捕獲到報告中。
import os import allure import pytest @allure.step def simple_step(step_param1, step_param2 = None): pass @pytest.mark.parametrize('param1', [True, False], ids=['1', '2']) def test_parameterize_with_id(param1): simple_step(param1) @pytest.mark.parametrize('param1', [True, False]) @pytest.mark.parametrize('param2', ['1', '2']) def test_parametrize_with_two_parameters(param1, param2): simple_step(param1, param2) # --clean-alluredir:每次執(zhí)行前清空數(shù)據(jù),這樣在生成的報告中就不會追加,只顯示當(dāng)前執(zhí)行的用例 if __name__ == '__main__': pytest.main(['-s', '-q','test_allure03.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
4.feature
模塊名稱,功能點描述
5.step
Allure生成的測試報告最重要的一點就是,它允許對每個測試用例進行非常詳細(xì)的步驟說明
import os import allure import pytest @allure.step("步驟二") def passing_step(): pass @allure.step("步驟三") def step_with_nested_steps(): nested_step() @allure.step("步驟四") def nested_step(): nested_step_with_arguments(1, 'abc') @allure.step("步驟五") def nested_step_with_arguments(arg1, arg2): pass @allure.step("步驟一") def test_with_nested_steps(): passing_step() step_with_nested_steps() if __name__ == '__main__': pytest.main(['-s', '-q','test_allure04.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
6.attach
Allure測試報告可以顯示許多不同類型的附件,這些附件可以補充測試、步驟或結(jié)果??梢允褂胊llure.attach(body、name、attachment_type、extension)調(diào)用來創(chuàng)建附件。
- body:要顯示的內(nèi)容(附件)
- name:附件名字
- attachment_type:附件類型,是allure.attachment_type里面的其中一種
- extension:附件的擴展名
- source:文件路徑
語法一:allure.attach(body,name,attachment_type,extension)
語法二:allure.attach.file(source,name,attachment_type,extension)
import os import allure import pytest @pytest.fixture def attach_file_in_module_scope_fixture_with_finalizer(request): allure.attach('在fixture前置操作里面添加一個附件txt', 'fixture前置附件', allure.attachment_type.TEXT) def finalizer_module_scope_fixture(): allure.attach('在fixture后置操作里面添加一個附件txt', 'fixture后置附件',allure.attachment_type.TEXT) request.addfinalizer(finalizer_module_scope_fixture) def test_with_attacments_in_fixture_and_finalizer(attach_file_in_module_scope_fixture_with_finalizer): pass def test_multiple_attachments(): allure.attach('<head></head><body>html page</body>', 'Attach with HTML type', allure.attachment_type.HTML) allure.attach.file('/Users/mrjade/Downloads/happy-new-year.html', attachment_type=allure.attachment_type.HTML) if __name__ == '__main__': pytest.main(['-s', '-q','test_allure05.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
7.description
描述類信息,比如對函數(shù)的描述,說明這個函數(shù)的作用,如:注冊接口。
語法一:@allure.description()
語法二:@allure.description_html()
import os import allure import pytest @allure.description_html(""" <h1>這是html描述</h1> <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr align="center"> <td>jade</td> <td>mr</td> <td>18</td> </tr> <tr align="center"> <td>road</td> <td>Tester</td> <td>18</td> </tr> </table> """) def test_html_description(): assert True @allure.description("""多行描述""") def test_description_from_decorator(): assert 42 == int(6 * 7) def test_unicode_in_docstring_description(): """在函數(shù)下方描述也可""" assert 42 == int(6 * 7) if __name__ == '__main__': pytest.main(['-s', '-q','test_allure06.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
8.title
- 添加測試用例標(biāo)題,通俗來講就是將函數(shù)名直接改成我們想要的,使得測試用例的標(biāo)題更具有可讀性
- 支持占位符傳遞關(guān)鍵字參數(shù)(動態(tài)標(biāo)題,結(jié)合@pytest.mark.parametrize使用)
import os import allure import pytest @allure.title("斷言2+2=4") def test_with_a_title(): assert 2 + 2 == 4 @allure.title("動態(tài)標(biāo)題: {param1} + {param2} = {expected}") @pytest.mark.parametrize('param1,param2,expected', [(2, 2, 4),(1, 2, 5)]) def test_with_parameterized_title(param1, param2, expected): assert param1 + param2 == expected @allure.title("這是個動態(tài)標(biāo)題,會被替換") def test_with_dynamic_title(): assert 2 + 2 == 4 allure.dynamic.title('測試結(jié)束,做為標(biāo)題') if __name__ == '__main__': pytest.main(['-s', '-q','test_allure07.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
9.link
將測試報告與缺陷跟蹤或測試管理系統(tǒng)整合,@allure.link、@allure.issue和@allure.testcase。
import os import allure import pytest @allure.link('https://www.cnblogs.com/mrjade/') def test_with_link(): pass @allure.link('https://www.cnblogs.com/mrjade/', name='點擊進入mrjade博客園') def test_with_named_link(): pass @allure.issue('https://github.com/allure-framework/allure-python/issues/642', 'bug issue鏈接') def test_with_issue_link(): pass @allure.testcase("https://www.cnblogs.com/mrjade/", '測試用例地址') def test_with_testcase_link(): pass if __name__ == '__main__': pytest.main(['-s', '-q','test_allure08.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
10.Retries
Allure允許測試報告中顯示單個測試運行期間重新執(zhí)行的測試的信息,以及一段時間內(nèi)測試執(zhí)行的歷史,需與Pytest插件結(jié)合使用。
windows:pip install pytest-rerunfailures mac:pip3 install pytest-rerunfailures import os import allure import random import time import pytest @allure.step def passing_step(): pass @allure.step def flaky_broken_step(): if random.randint(1, 5) != 1: raise Exception('Broken!') """需安裝【pip3 install pytest-rerunfailures】""" @pytest.mark.flaky(reruns=3, reruns_delay=1) # 如果失敗則延遲1s后重試 def test_broken_with_randomized_time(): passing_step() time.sleep(random.randint(1, 3)) flaky_broken_step() if __name__ == '__main__': pytest.main(['-s', '-q','test_allure09.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
11.tags
我們希望對執(zhí)行的測試用例保持靈活性。Pytest允許使用標(biāo)記修飾符@pytest.mark,Allure提供了3種類型的標(biāo)記裝飾符
- BDD樣式的標(biāo)記裝飾器
- @allure.epic:敏捷里面的概念,定義史詩,往下是feature
- @allure.feature:功能點的描述,理解成模塊往下是story
- @allure.story:故事,往下是title
- 優(yōu)先級(嚴(yán)重程度)標(biāo)記裝飾器 根據(jù)嚴(yán)重程度來標(biāo)記你的測試。嚴(yán)重程度裝飾器,它使用一個allure.severity_level enum值作為參數(shù)。如:@allure.severity(allure.severity_level.TRIVIAL)
- 自定義標(biāo)記裝飾器
import os import allure import pytest @pytest.fixture(scope="session") def login_fixture(): print("前置登錄") @allure.step("步驟1") def step_1(): print("操作步驟1") @allure.step("步驟2") def step_2(): print("操作步驟2") @allure.step("步驟3") def step_3(): print("操作步驟3") @allure.step("步驟4") def step_4(): print("操作步驟4") @allure.epic("會員項目") @allure.feature("登錄") class TestAllureALL: @allure.testcase("https://www.cnblogs.com/mrjade/", '測試用例,點我一下') @allure.issue("https://github.com/allure-framework/allure-python/issues/642", 'Bug 鏈接,點我一下') @allure.title("用戶名錯誤") @allure.story("登錄測試用例1") @allure.severity(allure.severity_level.TRIVIAL) # 不重要的 # @allure.severity(allure.severity_level.MINOR) # 輕微的 # @allure.severity(allure.severity_level.BLOCKER) # 阻塞的 # @allure.severity(allure.severity_level.CRITICAL) # 嚴(yán)重的 # @allure.severity(allure.severity_level.NORMAL) # 普通的 def test_case_1(self): """ 公眾號:測試工程師成長之路 """ print("測試用例1") step_1() step_2() @allure.title("用戶名正確,密碼錯誤") @allure.story("登錄測試用例2") def test_case_2(self): print("測試用例2") step_1() step_3() @allure.epic("訂單項目") @allure.feature("支付") class TestAllureALL2: @allure.title("支付成功") @allure.story("支付測試用例例1") def test_case_1(self, login_fixture): print("支付測試用例例1") step_3() @allure.title("支付失敗") @allure.story("支付測試用例例2") def test_case_2(self, login_fixture): print("支付測試用例例2") step_4() if __name__ == '__main__': pytest.main(['-s', '-q','test_allure10.py','--clean-alluredir','--alluredir=allure-results']) os.system(r"allure generate -c -o allure-report")
12.environment:環(huán)境信息
- 新建environment.properties文件
- 編寫如下環(huán)境信息
systemVersion=mac pythonVersion=3.9 allureVersion=2.10.0 baseUrl=http://192.168.1.1:8080 projectName=SIT author=TesterRoad
將environment.properties放在項目根目錄或其它目錄,在執(zhí)行測試用例時將其復(fù)制到allure-results目錄下
# coding=utf-8 import pytest import os if __name__ == '__main__': pytest.main(['-s', '-q','./','--clean-alluredir','--alluredir=allure-results']) os.system('cp environment.properties ./allure-results/environment.properties') os.system("allure generate -c -o allure-report")
八.Pytest執(zhí)行所有.py文件
1.新建run.py文件,編寫如下代碼
# coding=utf-8 import pytest import os if __name__ == '__main__': pytest.main(['-s', '-q','./','--clean-alluredir','--alluredir=allure-results']) os.system('cp environment.properties ./allure-results/environment.properties') os.system("allure generate -c -o allure-report")
2.查看報告
九.參考鏈接
https://docs.qameta.io/allure-report/
到此這篇關(guān)于Pytest+Allure使用教程的文章就介紹到這了,更多相關(guān)Pytest Allure使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python自動化測試pytest中fixtureAPI簡單說明
這篇文章主要為大家介紹了Python自動化測試pytest中fixtureAPI的簡單說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10在Ubuntu 20.04中安裝Pycharm 2020.1的圖文教程
這篇文章主要介紹了在Ubuntu 20.04中安裝Pycharm 2020.1的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04python實現(xiàn)根據(jù)ip地址反向查找主機名稱的方法
這篇文章主要介紹了python實現(xiàn)根據(jù)ip地址反向查找主機名稱的方法,涉及Python使用socket解析IP的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04python strip() 函數(shù)和 split() 函數(shù)的詳解及實例
這篇文章主要介紹了 python strip() 函數(shù)和 split() 函數(shù)的詳解及實例的相關(guān)資料,需要的朋友可以參考下2017-02-02關(guān)于Python中的 oct 函數(shù)與 min 函數(shù)
本文主要介紹了Python oct 函數(shù)與 min 函數(shù);oct 函數(shù)是 Python 內(nèi)置函數(shù),主要將一個整數(shù)轉(zhuǎn)為八進制,與 ord 函數(shù) / chr 函數(shù) 有點類似;min 函數(shù)返回給定參數(shù)的最小值,參數(shù)可以為序列語法,感興趣的小伙伴請繼續(xù)閱讀下文2021-09-09