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图标
待补充