Allure介紹
- allure是一個(gè)輕量級(jí)但惶,靈活的,支持多語言的測(cè)試報(bào)告工具;多平臺(tái)的,奢華的report框架;
- 可以為dev/qa提供詳盡的的測(cè)試報(bào)告耳鸯、測(cè)試步驟、log;也可以為管理理層提供high level統(tǒng)計(jì)報(bào)告;
- Java語言開發(fā)的,支持pytest膀曾,JaveScriipt县爬,PHP, ruby等
- 可以集成到Jenkins
Allure報(bào)表預(yù)覽
- 預(yù)覽網(wǎng)址:https://demo.qameta.io/allure/#
Allure安裝
- windows / mac通用安裝方法
- https://github.com/allure-framework/allure2/releases下載allure2.7.zip包
- 解壓->進(jìn)入bin目錄->運(yùn)行allure.bat
- 把bin目錄加入PATH環(huán)境變量
- Mac可以使用brew安裝:
brew install allure
- 官網(wǎng):http://allure.qatools.ru/
- 文檔:https://docs.qameta.io/allure/#
- 驗(yàn)證安裝:
allure --version
- 查看幫助文檔:
allure
針對(duì) Pytest 測(cè)試框架安裝
- 針對(duì) Pytest 測(cè)試框架還需安裝
allure-pytest
第三方庫(kù):pip install allure-pytest
- 用法:
pytest --alluredir=/tmp/my_allure_results
添谊,將測(cè)試報(bào)告文件存放到/tmp/my_allure_results
目錄下 - 查看報(bào)告:
allure serve /tmp/my_allure_results
- 測(cè)試文件
test_pytest.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')
- 在pycharm的terminal中切換到當(dāng)前路徑财喳,運(yùn)行
pytest test_allure.py --alluredir=./results/1
,就能在目錄中生成測(cè)試報(bào)告文件如下,一個(gè)測(cè)試用例對(duì)應(yīng)一個(gè).json
文件:
- 在terminal中運(yùn)行
allure_practice>allre serve ./results/1
就能自動(dòng)打開網(wǎng)頁(yè)查看報(bào)告
使用 Allure2 生成精美報(bào)告
- 安裝allure-pytest插件
pip install allure-pytest
- 運(yùn)行:
- 在測(cè)試執(zhí)行期間收集結(jié)果
-
pytest [測(cè)試文件] -s -q --alluredir=./ result/
(--alluredir 這個(gè)選項(xiàng)用于指定存儲(chǔ)測(cè)試結(jié)果的路徑)
-
- 在測(cè)試執(zhí)行期間收集結(jié)果
- 查看測(cè)試報(bào)告
- 方式一:測(cè)試完成后查看實(shí)際報(bào)告耳高,在線看報(bào)告扎瓶,會(huì)直接打開默認(rèn)瀏覽器展示當(dāng)前報(bào)告
-
allure serve ./result/
(注意這里的serve書寫)
-
- 方式二:從結(jié)果生成報(bào)告,這是一個(gè)啟動(dòng) tomcat 的服務(wù)泌枪,可以永久打開概荷,需要兩個(gè)步驟:生成報(bào)告,打開報(bào)告
- 生成報(bào)告
-
allure generate ./result/ -o ./report/ --clean
(注意:覆蓋路徑加 --clean )
-
- 打開報(bào)告
- `alure open -h 127.0.0.1 -p 8883 ./report/
- 生成報(bào)告
- 方式一:測(cè)試完成后查看實(shí)際報(bào)告耳高,在線看報(bào)告扎瓶,會(huì)直接打開默認(rèn)瀏覽器展示當(dāng)前報(bào)告
- 關(guān)閉報(bào)告:在terminal中使用
ctrl+c
使用上面的例子
- 在pycharm的terminal中切換到當(dāng)前路徑碌燕,運(yùn)行
pytest test_allure.py --alluredir=./results/1
- 在terminal 中運(yùn)行
allure generate ./results/1 -o ./report/1 --clean
误证,會(huì)在report目錄下生成報(bào)告文件如下,包括了.json .txt .html .js .css .ico
等文件:
Allure常用特性
- 場(chǎng)景:
- 希望在報(bào)告中看到測(cè)試功能陆蟆,子功能或場(chǎng)景雷厂,測(cè)試步驟,包括測(cè)試附加信息
- 解決:
- @Feature叠殷,@story改鲫,@step,@attach
- 步驟:
- import allure
- 功能上加 @allure.feature('功能名稱')
- 子功能上加@allure.story('子功能名稱')
- 步驟上加@allure.step('步驟細(xì)節(jié)')
- @allure.attach('具體文本信息)林束,需要附加的信息像棘,可以是數(shù)據(jù),文本壶冒,圖片缕题,視頻,網(wǎng)頁(yè)
- 如果只測(cè)試登錄功能運(yùn)行的時(shí)候可以加限制過濾:
-
pytest [文件名] --allure_features '購(gòu)物車功能' --allure-stories '加入購(gòu)物車'
(注意這里—allure_features中間是下劃線)
-
- 實(shí)例
test_feature_story.py
代碼如下
import pytest
import allure
# 標(biāo)識(shí)整個(gè)模塊都用來進(jìn)行登錄
@allure.feature('登錄模塊')
class TestLogin():
@allure.story('登錄成功')
def test_login_success(self):
print("這是登錄:測(cè)試用例胖腾,登陸成功")
pass
@allure.story('登錄失敗')
def test_login_fail(self):
print("這是登錄:測(cè)試用例烟零,登陸失敗")
pass
@allure.story('用戶名缺失')
def test_login_lostname(self):
print("用戶名缺失")
pass
@allure.story('密碼缺失')
def test_login_lostsec(self):
# 對(duì)一些關(guān)鍵步驟進(jìn)行標(biāo)記
with allure.step("點(diǎn)擊用戶名"):
print("輸入用戶名")
with allure.step("點(diǎn)擊密碼"):
print("輸入密碼")
print("點(diǎn)擊登錄")
with allure.step("點(diǎn)擊登錄之后登錄失敗"):
assert "1" == 1
print("登錄失敗")
pass
@allure.story("登錄失敗")
def test_login_failure(self):
pass
- Terminal中輸入
pytest test_feature_story.py --alluredir=./results/2
,生成報(bào)告文件 - Terminal中輸入
allure serve ./results/2
咸作,打開測(cè)試報(bào)告
- 指定 feature 運(yùn)行锨阿,執(zhí)行
pytest -v test_feature_story.py --allure-features '登錄模塊'
,-v
為打印詳細(xì)信息 - 指定 story 運(yùn)行记罚,執(zhí)行
pytest -v test_feature_story.py --allure-stories '登錄成功'
Allure特性-feature/story
- 注解@allure.feature 與@allure.store的關(guān)系
- feature相當(dāng)于一個(gè)功能墅诡,一個(gè)大的模塊,將case分類到某個(gè)feature中桐智,報(bào)告中Behaviors 中顯示末早,相當(dāng)于testsuite
- story相當(dāng)于對(duì)應(yīng)這個(gè)功能或者模塊下的不同場(chǎng)景、分支功能说庭,屬于feature之下的結(jié)構(gòu)然磷,報(bào)告在
features中顯示,相當(dāng)于testcase - feature與story類似于父子關(guān)系
Allure特性-step
- 測(cè)試過程中每個(gè)步驟刊驴,一般放在具體邏輯方法中
- 可以放在關(guān)鍵步驟中样屠,在報(bào)告中顯示
- 在app, web自動(dòng)測(cè)試當(dāng)中,建議每切換到一個(gè)新的頁(yè)面當(dāng)做一個(gè)step用法:
- @allure.step() 只能以裝飾器的形式放在類或者方法上面
- with allure.step() 可以放在測(cè)試用例方法里面,但測(cè)試步驟的代碼需要被該語句包含
Allure特性-issue痪欲,testcase
- 關(guān)聯(lián)測(cè)試用例(可以直接給測(cè)試用例的地址鏈接)
- 關(guān)聯(lián)bug
- 執(zhí)行的時(shí)候需要加個(gè)參數(shù)
- `--allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
- 執(zhí)行的時(shí)候需要加個(gè)參數(shù)
- 加入鏈接示例:
@allure.link("www.baidu.com")
import allure
@allure.link("www.baidu.com")
def test_with_link():
print("這是一條加了鏈接的測(cè)試")
pass
-
執(zhí)行結(jié)果報(bào)告
- 給鏈接取別名:
@allure.link("www.baidu.com", name="百度一下")
import allure
@allure.link("www.baidu.com", name="百度一下")
def test_with_link():
print("這是一條加了鏈接的測(cè)試")
pass
-
執(zhí)行結(jié)果報(bào)告
- 加入測(cè)試用例的鏈接:
@allure.testcase(TST_CASE_LINK, '登錄用例')
import allure
TST_CASE_LINK ='https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637'
@allure.testcase(TST_CASE_LINK, '登錄用例')
def test_with_testcase_link():
print("這是一條測(cè)試用例的鏈接,鏈接到測(cè)試用例里面")
pass
-
執(zhí)行結(jié)果報(bào)告
- 通過傳入的bugID生成bug鏈接:
@allure.issue('BugID', 鏈接名')
攻礼,運(yùn)行時(shí)指定鏈接地址
import allure
# 測(cè)試bug鏈接业踢,前面為bugID
"""
在運(yùn)行時(shí)加入bug鏈接: --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
中括號(hào)里面就會(huì)傳bugID
"""
@allure.issue('140', '這是一個(gè)issue')
def test_with_issue_link():
pass
- 在terminal中運(yùn)行時(shí)需要加入bug鏈接:
pytest test_link_issue.py --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{} --alluredir=./results/6
,花括號(hào)內(nèi)會(huì)自動(dòng)傳入BugID -
報(bào)告運(yùn)行結(jié)果
按重要性級(jí)別進(jìn)行一定范圍的測(cè)試
- 場(chǎng)景
- 通常測(cè)試有P0礁扮、冒煙測(cè)試知举、驗(yàn)證上線測(cè)試。按重要性級(jí)別來分別執(zhí)行的太伊,比如上線要把主流程和重要模塊都跑一遍
- 解決:
- 通過附加
py.test.mark
標(biāo)記 - 通過
allure.feature
雇锡,allure.story
- 也可以通過
allure.severity
來附加標(biāo)記- 級(jí)別:Trivial不重要,Minor不太重要僚焦,Normal正常問題锰提,Critical嚴(yán)重,Blocker阻塞
- 通過附加
- 步驟:
- 在方法芳悲、函數(shù)和類上面加
@allure.severity(allure.severity_level.TRIVIAL)
- 執(zhí)行時(shí)
py.test -s -v [文件名] --allure-severities norma,critical
- 在方法芳悲、函數(shù)和類上面加
- 缺陷等級(jí)
名稱 | 描述 |
---|---|
Blocker級(jí)別 | 中斷缺陷(客戶端程序無響應(yīng)立肘,無法執(zhí)行下一步操作) |
Critical級(jí)別 | 臨界缺陷(功能點(diǎn)缺失) |
Normal級(jí)別 | 普通缺陷(數(shù)值計(jì)算錯(cuò)誤) |
Minor級(jí)別 | 次要缺陷(界面錯(cuò)誤與UI需求不符) |
Trivial級(jí)別 | 輕微缺陷(必輸項(xiàng)無提示,或者提示不規(guī)范) |
- 執(zhí)行嚴(yán)重級(jí)別為 normal和critical的測(cè)試用例:
pytest -v test_severity.py --allure-severities normal,critical --alluredir=./results/7
import allure
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_normal_severity_test_class(self):
pass
@allure.severity(allure.severity_level.CRITICAL)
def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self):
pass
-
執(zhí)行結(jié)果報(bào)告
在pycharm中配置默認(rèn)的allure參數(shù)
配置以后直接鼠標(biāo)右鍵-> run 'pytest for ...' 即可
前端自動(dòng)化測(cè)試-截圖
- 場(chǎng)景:
- 前端自動(dòng)化測(cè)試經(jīng)常需要附加圖片或html名扛,在適當(dāng)?shù)牡胤搅履辏m當(dāng)?shù)臅r(shí)機(jī)截圖
- 解決:
-
@allure.attach
顯示許多不同類型的提供的附件,可以補(bǔ)充測(cè)試肮韧,步驟或測(cè)試結(jié)果融蹂。
-
- 步驟:
- 在測(cè)試報(bào)告里附加網(wǎng)頁(yè):
allure.attach(body(內(nèi)容), name, attachment_type, extension)
allure.attach('<head></head><body>首頁(yè)</body>,'這是錯(cuò)誤頁(yè)的結(jié)果信息',allure.attachment_type.HTML)
- 在測(cè)試報(bào)告里附加圖片:
allure.attach.file(source, name, attachment_type, extension)
allure.attach.file("./ result/b.png" , attachment_type=allure.attachment_type.PNG)
- 在測(cè)試報(bào)告里附加網(wǎng)頁(yè):
- 代碼示例
import allure
import pytest
def test_attach_text():
allure.attach("這是一個(gè)純文本", attachment_type=allure.attachment_type.TEXT)
def test_attach_htlm():
allure.attach("<body>這是一段html body塊</body>", 'html測(cè)試塊', attachment_type=allure.attachment_type.HTML)
def test_attach_photo():
allure.attach(r"D:\Programs\DevOps\Python_Practice\allure_practice\resource\1.png",
name="這是一個(gè)圖片", attachment_type=allure.attachment_type.PNG)
-
運(yùn)行結(jié)果報(bào)告
- 運(yùn)行結(jié)果圖片展示出來弄企,是因?yàn)檎{(diào)用的方法
allure.attach(r"D:\Programs\DevOps\Python_Practice\allure_practice\resource\1.png", name="這是一個(gè)圖片", attachment_type=allure.attachment_type.PNG)
有誤超燃,應(yīng)該調(diào)用allure.attach.file()
方法 - 修改后的代碼:
import allure
import pytest
def test_attach_text():
allure.attach("這是一個(gè)純文本", attachment_type=allure.attachment_type.TEXT)
def test_attach_htlm():
allure.attach("<body>這是一段html body塊</body>", 'html測(cè)試塊', attachment_type=allure.attachment_type.HTML)
# 調(diào)用圖片要使用file方法
def test_attach_photo():
allure.attach.file(r"D:\Programs\DevOps\Python_Practice\allure_practice\resource\1.png",
name="這是一個(gè)圖片", attachment_type=allure.attachment_type.PNG)
-
修改后的運(yùn)行結(jié)果
實(shí)戰(zhàn):pytest+allure+selenium
- 代碼
import time
import allure
import pytest
from selenium import webdriver
@allure.testcase("http://www/github.com", "測(cè)試用例地址")
@allure.feature("百度搜索")
@pytest.mark.parametrize('test_data', ['allure', 'pytest', 'unittest'])
def test_steps_demo(test_data):
with allure.step("打開百度網(wǎng)頁(yè)"):
driver = webdriver.Chrome()
driver.maximize_window() # 瀏覽器最大化
driver.get("http://www.baidu.com")
with allure.step("輸入搜索詞"):
driver.find_element_by_id("kw").send_keys(test_data)
time.sleep(2)
driver.find_element_by_id("su").click()
time.sleep(2)
with allure.step("保存圖片"):
driver.save_screenshot("./resource/b.png")
allure.attach.file("./resource/b.png", attachment_type=allure.attachment_type.PNG)
allure.attach('<head></head><body>首頁(yè)</body>','Attach with HTML type',allure.attachment_type.HTML)
with allure.step("關(guān)閉瀏覽器"):
driver.quit()
-
運(yùn)行結(jié)果