Allure 是一款輕量級、支持多語言的開源自動化測試報告生成框架,由Java語言開發,可以集成到 Jenkins。 pytest 測試框架支持Allure 報告生成。
pytest也可以生成junit格式的xml報告和HTML報告,命令如下:
pytest test_demo.py --junitxml=report.xml
pytest test_demo.py --html=report.html #需要安裝插件:pip install pytest-html
Allure 報告更加靈活美觀,本文介紹如何使用pytest 生成 allure測試報告
環境安裝
安裝allure
- allure包下載:https://github.com/allure-framework/allure2/releases
- 解壓 -> 進入bin目錄 -> 運行allure.bat,
- 把bin目錄加入PATH環境變量

allure官網 : http://allure.qatools.ru/
allure文檔 : https://docs.qameta.io/allure/#
安裝 allure-pytest插件
pip install allure-pytest
生成Allure報告
運行
pytest [測試文件] -s -q --alluredir=./result #--alluredir用於指定存儲測試結果的路徑)
查看測試報告
方式一:直接打開默認瀏覽器展示報告
allure serve ./result/
方式二:從結果生成報告
-
生成報告
allure generate ./result/ -o ./report/ --clean(覆蓋路徑加--clean) -
打開報告
allure open -h 127.0.0.1 -p 8883 ./report/
實例代碼:https://docs.qameta.io/allure/#_pytest
test_allure.py:
import pytest
def test_success():
"""this test succeeds"""
assert True
def test_failure():
"""this test fails"""
assert False
def test_skip():
"""this test is skipped"""
pytest.skip('for a reason!')
def test_broken():
raise Exception('oops')
方法1
執行測試用例:
pytest test_allure.py --alluredir=./result/1

打開報告:
> allure serve ./result/1
Generating report to temp directory...
Report successfully generated to C:\Users\10287\AppData\Local\Temp\6968593833275403330\allure-report
Starting web server...
2020-10-25 20:59:42.368:INFO::main: Logging initialized @4873ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://169.254.57.162:60084/>. Press <Ctrl+C> to exit

方法2
allure generate ./result/1 -o ./report/1/ --clean
allure open -h 127.0.0.1 -p 8883 ./report/1
瀏覽器訪問地址 http://127.0.0.1:8883/ ,會顯示跟上圖一樣的報告。
allure特性—feature, storry, step
可以在報告中添加用例描述信息,比如測試功能,子功能或場景,測試步驟以及測試附加信息:
- @allure.feature(‘功能名稱’):相當於 testsuite
- @allure.story(’子功能名稱‘):對應這個功能或者模塊下的不同場景,相當於 testcase
- @allure.step('步驟'):測試過程中的每個步驟,放在具體邏輯方法中
- allure.step('步驟') 只能以裝飾器的形式放在類或者方法上面
- with allure.step:可以放在測試用例方法里面
- @allure.attach('具體文本信息')
- 附加信息:數據,文本,圖片,視頻,網頁
測試用例 test_feature_story_step.py:
import pytest
import allure
@allure.feature("登錄")
class TestLogin():
@allure.story("登錄成功")
def test_login_success(self):
print("登錄成功")
pass
@allure.story("密碼錯誤")
def test_login_failure(self):
with allure.step("輸入用戶名"):
print("輸入用戶名")
with allure.step("輸入密碼"):
print("輸入密碼")
print("點擊登錄")
with allure.step("登錄失敗"):
assert '1' == 1
print("登錄失敗")
pass
@allure.story("用戶名密碼錯誤")
def test_login_failure_a(self):
print("用戶名或者密碼錯誤,登錄失敗")
pass
@allure.feature("注冊")
class TestRegister():
@allure.story("注冊成功")
def test_register_success(self):
print("測試用例:注冊成功")
pass
@allure.story("注冊失敗")
def test_register_failure(self):
with allure.step("輸入用戶名"):
print("輸入用戶名")
with allure.step("輸入密碼"):
print("輸入密碼")
with allure.step("再次輸入密碼"):
print("再次輸入密碼")
print("點擊注冊")
with allure.step("注冊失敗"):
assert 1 + 1 == 2
print("注冊失敗")
pass
用例執行、生成報告
pytest test_feature_story.py --alluredir=./result/2
allure generate ./result/2 -o ./report/2/ --clean
allure open -h 127.0.0.1 -p 8883 ./report/2
報告:

allure特性—link, issue, testcase
可以在測試報告中添加鏈接、bug地址、測試用例地址。
關聯bug需要在用例執行時添加參數:
- --allure-link-pattern=issue:[bug地址]{}
- 例如:--allure-link-pattern=issue:http://www.bugfree.com/issue/{}
test_allure_link_issue.py:
import allure
@allure.link("http://www.baidu.com", name="baidu link")
def test_with_link():
pass
@allure.issue("140","this is a issue")
def test_with_issue_link():
pass
TEST_CASE_LINK = 'https://github.com'
@allure.testcase(TEST_CASE_LINK, 'Test case title')
def test_with_testcase_link():
pass
用例執行:
pytest test_allure_link_issue.py --allure-link-pattern=issue:http://www.bugfree.com/issue/{} --alluredir=./result/3
allure serve ./result/3
報告:

點擊 this is a issue,頁面會跳轉到bug頁面:http://www.bugfree.com/issue/140
allure特性—severity
有時候在上線前,由於時間關系,我們只需要把重要模塊測試一遍,在這樣的場景下我們怎么實現呢?主要有三種方法:
-
可以使用pytest.mark來標記用例,Pytest測試框架(一):pytest安裝及用例執行 介紹了這種方法。
@pytest.mark.webtest # 添加標簽 @pytest.mark.sec pytest -m "webtest and not sec" -
通過 allure.feature, allure.story來實現
pytest test_feature_story_step.py --allure-features "登錄" //只運行登錄模塊 pytest test_feature_story_step.py --allure-stories "登錄成功" //只運行登錄成功子模塊 -
通過 allure.severity按重要性級別來標記,有5種級別:
-
Blocker級別:阻塞
-
Critical級別:嚴重
-
Normal級別:正常
-
Minor級別:不太重要
-
Trivial級別:不重要
-
test_allure_severity.py:
import allure
import pytest
def test_with_no_severity_label():
pass
@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
pass
@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
pass
@allure.severity(allure.severity_level.NORMAL)
class TestclassWithNormalSeverity(object):
def test_inside_the_normalseverity_test_class(self):
pass
@allure.severity(allure.severity_level.CRITICAL)
def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self):
pass
用例執行:
pytest test_allure_severity.py --alluredir=./result/4 --allure-severities normal,critical
allure serve ./result/4
結果:

allure.attach()
可以在報告中附加文本、圖片以及html網頁,用來補充測試步驟或測試結果,比如錯誤截圖或者關鍵步驟的截圖。
test_allure_attach.py:
import allure
import pytest
def test_attach_text():
allure.attach("純文本", attachment_type=allure.attachment_type.TEXT)
def test_attach_html():
allure.attach("<body>這是一段htmlbody塊</body>", "html頁面", attachment_type=allure.attachment_type.HTML)
def test_attach_photo():
allure.attach.file("test.jpg", name="圖片", attachment_tye=allure.attachment_type.JPG)
用例執行:
pytest test_allure_attach.py --alluredir=./result/5
allure serve ./result/5
結果:

pytest+selenium+allure報告
測試步驟:
- 打開百度
- 搜索關鍵詞
- 搜索結果截圖,保存到報告中
- 退出瀏覽器
test_allure_baidu.py:
import allure
import pytest
from selenium import webdriver
import time
@allure.testcase("http://www.github.com")
@allure.feature("百度搜索")
@pytest.mark.parametrize('test_data1', ['allure', 'pytest', 'unittest'])
def test_steps_demo(test_data1):
with allure.step("打開百度網頁"):
driver = webdriver.Chrome("D:/testing_tools/chromedriver85/chromedriver.exe")
driver.get("http://www.baidu.com")
with allure.step("搜索關鍵詞"):
driver.find_element_by_id("kw").send_keys(test_data1)
time.sleep(2)
driver.find_element_by_id("su").click()
time.sleep(2)
with allure.step("保存圖片"):
driver.save_screenshot("./result/b.png")
allure.attach.file("./result/b.png", attachment_type=allure.attachment_type.PNG)
allure.attach('<head></head><body>首頁</body>', 'Attach with HTML type', allure.attachment_type.HTML)
with allure.step("退出瀏覽器"):
driver.quit()
用例執行:
pytest test_allure_baidu.py --alluredir=./result/6
allure serve ./result/6
結果:

文章標題:Pytest測試框架(五):pytest + allure生成測試報告
本文作者:hiyo
本文鏈接:https://www.cnblogs.com/hiyong/p/14163298.html
歡迎關注公眾號:「測試開發小記」及時接收最新技術文章!
