2022-02-17 11:25:30
一、安裝allure環境
1、下載allure.zip 地址:https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/ (沒有配置這個不會生成allure報告) https://github.com/allure-framework/allure2/releases
2、解壓allure.zip放到python的lib目錄下
3、設置系統的環境變量,C:\Users\EDY\AppData\Local\Programs\Python\Python39\Lib\allure-2.17.2\bin,將此目錄放到path中
4、驗證安裝成功與否,下面都成功才算成功安裝
- cmd進入allure -v,有許多allure信息
- pycharm的終端輸入allure ,會有相關信息
5、注意安裝完需要:1、重啟電腦,使環境變量生效;2、使用的時候需要重啟pycharm
問題:
1、生成的報告打開沒有用例:
解決:可能是報告數據路徑參數錯了,或者,沒有清理前次報告的數據
2、將生成報告的命令放到session函數后置中,運行后,生成的報告少了部分用例,(預期有兩個用例)
解決:放到run.py中,待pytest相關的文件執行后再執行allure
二、總覽報告
沒有使用函數的報告
1、環境配置顯示,默認沒有,兩種形式:
通過創建environment.properties或者environment.xml文件,並把文件存放到報告目錄下,就是 --alluredir 后面跟的目錄
environment.properties舉例
Browser=Chrome Browser.Version=81.0.4044.92 Stand=Production ApiUrl=127.0.0.1/login python.Version=3.7.2
environment.xml舉例
<environment> <parameter> <key>Browser</key> <value>Chrome</value> </parameter> <parameter> <key>Browser.Version</key> <value>81.0.4044.92</value> </parameter> <parameter> <key>Stand</key> <value>Production</value> </parameter> <parameter> <key>ApiUrl</key> <value>127.0.0.1/login</value> </parameter> <parameter> <key>python.Version</key> <value>3.7.2</value> </parameter> </environment>
注意都不要寫中文,會亂碼,效果都是一樣的,如下
2、菜單categories中將 categories.json 文件添加到報告目錄即可 ,和上面的環境文件一樣
categories.json 舉例
[ { "name": "Ignored tests", "matchedStatuses": ["skipped"] }, { "name": "Infrastructure problems", "matchedStatuses": ["broken", "failed"], "messageRegex": ".*bye-bye.*" }, { "name": "Outdated tests", "matchedStatuses": ["broken"], "traceRegex": ".*FileNotFoundException.*" }, { "name": "Product defects", "matchedStatuses": ["failed"] }, { "name": "Test defects", "matchedStatuses": ["broken"] } ]
參數的含義
- name:分類名稱
- matchedStatuses:測試用例的運行狀態,默認["failed", "broken", "passed", "skipped", "unknown"]
- messageRegex:測試用例運行的錯誤信息,默認是 .* ,是通過正則去匹配的
- traceRegex:測試用例運行的錯誤堆棧信息,默認是 .* ,也是通過正則去匹配的
三、allure的函數介紹
@allure.step(str):顯示具體步驟
import allure @allure.step("第一步") def passing_step(): pass @allure.step("第二步") def step_with_nested_steps(): nested_step()
效果
圖片
attach(body, name=None, attachment_type=None, extension=None)和aattach.file(source, name=None, attachment_type=None, extension=None):附加文件
參數列表
- body:要顯示的內容(附件)
- name:附件名字
- attachment_type:附件類型,是 allure.attachment_type 里面的其中一種
- extension:附件的擴展名
- source:要導入的文件名
attachment_type的附件類型選擇
具體操作
allure.attach('在fixture前置操作里面添加一個附件txt', 'fixture前置附件', allure.attachment_type.TEXT) def test_multiple_attachments(): allure.attach('<head></head><body> 一個HTML頁面 </body>', 'Attach with HTML type', allure.attachment_type.HTML) allure.attach.file('./reports.html', attachment_type=allure.attachment_type.HTML)
圖片
3、@allure.description(str)用例描述,以下三種方式都是一樣的效果
@allure.description_html(str):相當於傳一個HTML代碼組成的字符串,類似 allure.attach() 中傳HTML
@allure.description(""" 這是一個@allure.description裝飾器 沒有特別的用處 """) def test_description_from_decorator(): assert 42 == int(6 * 7) # 方式二 def test_unicode_in_docstring_description(): """ 當然,在方法聲明的下一行這樣子寫,也算一種添加description的方式哦 """ assert 42 == int(6 * 7) # 方式三 @allure.description_html(""" <h1>Test with some complicated html description</h1> <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr align="center"> <td>William</td> <td>Smith</td> </table> """) def test_html_description(): assert True
5、@allure.title()
@allure.title("多個參數{name},{phone},{age}") @pytest.mark.parametrize("name,phone,age", [ (1, 2, 3), (4, 5, 6), (7, 8, 9) ]) def test_test_test(name, phone, age): print(name, phone, age)
6、@allure.link() @allure.issue() @allure.testcase(),超鏈接,效果差不多,傳一個url和name就行
源碼比較
def link(url, link_type=LinkType.LINK, name=None): return safely(plugin_manager.hook.decorate_as_link(url=url, link_type=link_type, name=name)) def issue(url, name=None): return link(url, link_type=LinkType.ISSUE, name=name) def testcase(url, name=None): return link(url, link_type=LinkType.TEST_CASE, name=name)
7、BDD標記裝飾器
提供了三個裝飾器
- @allure.epic:敏捷里面的概念,定義史詩,往下是 feature
- @allure.feature:功能點的描述,理解成模塊往下是 story
- @allure.story:故事,往下是 title
命令行增加參數,指定標記部分用例執行,和冒煙測試差不多
- --allure-epics
- --allure-features
- --allure-stories
8、優先級@allure.serverity()
源碼
@staticmethod def severity(severity_level): Dynamic.label(LabelType.SEVERITY, severity_level) class Severity(str, Enum): BLOCKER = 'blocker' CRITICAL = 'critical' NORMAL = 'normal' MINOR = 'minor' TRIVIAL = 'trivial' #使用 @allure.severity(allure.severity_level.TRIVIAL) def test_with_trivial_severity(): pass
可以看到不同 severity 測試用例運行的統計數據,比較直觀
同時顯示同一個優先級的失敗、通過用例數,以及哪條用例是失敗、通過的
命令行參數,指定運行某個優先級別的用例 allure-serverities
pytest test_severity.py -sq --alluredir=./allure --allure-severities=blocker,critical
9、指定報告路徑,並清理前一次的報告
pytest test_2.py --alluredir=./allure --clean-alluredir
10、allure 命令講解
Commands: generate Generate the report Usage: generate [options] The directories with allure results Options: -c, --clean Clean Allure report directory before generating a new one. Default: false --config Allure commandline config path. If specified overrides values from --profile and --configDirectory. --configDirectory Allure commandline configurations directory. By default uses ALLURE_HOME directory. --profile Allure commandline configuration profile. -o, --report-dir, --output The directory to generate Allure report into. Default: allure-report serve Serve the report Usage: serve [options] The directories with allure results Options: --config Allure commandline config path. If specified overrides values from --profile and --configDirectory. --configDirectory Allure commandline configurations directory. By default uses ALLURE_HOME directory. -h, --host This host will be used to start web server for the report. -p, --port This port will be used to start web server for the report. Default: 0 --profile Allure commandline configuration profile. open Open generated report Usage: open [options] The report directory Options: -h, --host This host will be used to start web server for the report. -p, --port This port will be used to start web server for the report. Default: 0 plugin Generate the report Usage: plugin [options] Options: --config Allure commandline config path. If specified overrides values from --profile and --configDirectory. --configDirectory Allure commandline configurations directory. By default uses ALLURE_HOME directory. --profile Allure commandline configuration profile.
generate 主要用-c刪除報告目錄 -o指定目錄生成報告
open 打開生成的報告
serve 啟動服務器打開報告
示例
pytest -sq --alluredir=./allure allure serve ./allure #另一種方式 pytest -sq --alluredir=./allure allure generate -c -o ./allure-report ./allure allure open ./allure-report
11、動態生成
源碼
lass Dynamic(object): @staticmethod def title(test_title): plugin_manager.hook.add_title(test_title=test_title) @staticmethod def description(test_description): plugin_manager.hook.add_description(test_description=test_description) @staticmethod def description_html(test_description_html): plugin_manager.hook.add_description_html(test_description_html=test_description_html) @staticmethod def label(label_type, *labels): plugin_manager.hook.add_label(label_type=label_type, labels=labels) @staticmethod def severity(severity_level): Dynamic.label(LabelType.SEVERITY, severity_level) @staticmethod def feature(*features): Dynamic.label(LabelType.FEATURE, *features) @staticmethod def story(*stories): Dynamic.label(LabelType.STORY, *stories) @staticmethod def tag(*tags): Dynamic.label(LabelType.TAG, *tags) @staticmethod def link(url, link_type=LinkType.LINK, name=None): plugin_manager.hook.add_link(url=url, link_type=link_type, name=name) @staticmethod def issue(url, name=None): Dynamic.link(url, link_type=LinkType.ISSUE, name=name) @staticmethod def testcase(url, name=None): Dynamic.link(url, link_type=LinkType.TEST_CASE, name=name) @staticmethod def suite(suite_name): Dynamic.label(LabelType.SUITE, suite_name) @staticmethod def parent_suite(parent_suite_name): Dynamic.label(LabelType.PARENT_SUITE, parent_suite_name) @staticmethod def sub_suite(sub_suite_name): Dynamic.label(LabelType.SUB_SUITE, sub_suite_name)
使用方式,動態方法生成的效果優先級比同類裝飾器更高
@allure.title("裝飾器標題") def test_1(): print(123) allure.dynamic.title("動態標題")
四、其他操作
1、更改allure報告的logo圖標
待補充