希望在报告中看到测试功能,子功能或场景,测试步骤,[email protected],@story,@step,@attach
步骤:
import allure
@allure.attach("具体文本信息"),需要附加的信息,可以是数据,文本,图片,视频,网页
如果只测试部分功能运行的时候可以加限制过滤:
pytest 文件名 --allure-features "需要运行的功能名称"
allure特性—feature/story
feature相当于一个功能,一个大的模块,将case分类到某个feature中,报告中在behaviore中显示,相当于testsuite
story相当于对应这个功能或者模块下的不同场景,分支功能,属于feature之下的结构,报告在features中显示,相当于testcase
feature与story类似于父与子关系
step特性
测试过程中每个步骤,一般放在具体逻辑方法中
可以放在关键步骤中,在报告中显示
在app,web自动化测试中,建议每切换到一个新的页面当做一个step
用法:
@allure.step() 只能以装饰器的形式放在类或方法上面
with allure.step(): 可以放在测试用例方法里面,但测试步骤的代码需要被该语句包含
运行:
在测试执行期间收集结果
pytest [测试文件] -s -q --alluredir=./result --clean-alluredir
--alluredir这个选项,用于指定存储测试结果的路径
--clean-alluredir 这个选项用来清除之前生成的结果
查看测试报告:
方法一:测试完成后查看实际报告,在线看报告,会直接打开默认浏览器展示当前报告
allure serve ./result
方法二:从结果生成报告,这是一个启动tomcat的服务,需要两个步骤
生成报告:
allure generate ./result -o ./report --clean (注意:--clean用来清除之前已生成的报告)
打开报告:
allure open -h 127.0.0.1 -p 8883 ./report
举个例子:
有如下代码文件
#!/usr/bin/python#-*- coding: UTF-8 -*-
"""@author:chenshifeng
@file:test_allure.py
@time:2020/10/10"""
importallureimportpytest
@allure.feature(‘登录模块‘)classTestLogin():
@allure.story(‘登录成功‘)
@allure.title(‘登录成功标题‘)deftest_login_sucess(self):
with allure.step(‘步骤1:打开应用‘):print(‘应用已打开‘)
with allure.step(‘步骤2:进入登录页面‘):print(‘登录页面已打开‘)
with allure.step(‘步骤3:输入用户名和密码‘):print(‘用户名和密码输入成功‘)print(‘登录测试用例:登录成功‘)
@allure.story(‘登录成功‘)deftest_login_sucess2(self):assert ‘1‘ == 1
print(‘登录测试用例:登录成功‘)
@allure.story(‘登录失败‘)deftest_login_failure_a(self):print(‘登录测试用例:登录失败,用户名缺失‘)
@allure.story(‘登录失败‘)deftest_login_failure_b(self):print(‘登录测试用例:登录失败,密码缺失‘)
@allure.story(‘登录失败‘)deftest_login_failure_c(self):
with allure.step(‘输入用户名‘):print(‘已输入用户名‘)
with allure.step(‘输入密码‘):print(‘已输入密码‘)
with allure.step(‘点击登录‘):print(‘已点击登录‘)print(‘登录测试用例:登录失败,密码错误‘)
@allure.feature(‘搜索模块‘)classTestSearch():deftest_search1(self):print(‘搜索用例1‘)
TEST_CASE_LINK= ‘https://mirrors.huaweicloud.com/‘@allure.testcase(TEST_CASE_LINK,‘测试用例连接‘)deftest_search2(self):print(‘搜索用例2‘)
@allure.step(‘搜索步骤‘)deftest_search3(self):print(‘搜索用例3‘)
View Code
依次执行命令:
pytest test_allure.py --alluredir=./result --clean-alluredir
allure serve ./result
chenshifengdeMacBook-Pro:testcode chenshifeng$ pytest test_allure.py --alluredir=./result --clean-alluredir============================================================================= test session starts =============================================================================platform darwin-- Python 3.9.0, pytest-6.1.1, py-1.9.0, pluggy-0.13.1rootdir:/Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: allure-pytest-2.8.18collected8items
test_allure.py .F...... [100%]================================================================================== FAILURES ===================================================================================
________________________________________________________________________ TestLogin.test_login_sucess2 _________________________________________________________________________self= @allure.story(‘登录成功‘)deftest_login_sucess2(self):> assert ‘1‘ == 1E AssertionError:assert ‘1‘ == 1test_allure.py:27: AssertionError=========================================================================== short test summary info ===========================================================================FAILED test_allure.py::TestLogin::test_login_sucess2- AssertionError: assert ‘1‘ == 1
========================================================================= 1 failed, 7 passed in 0.07s =========================================================================chenshifengdeMacBook-Pro:testcode chenshifeng$ allure serve ./result
Generating report to temp directory...
Report successfully generated to/var/folders/p0/3_7fwrvx6n3ftpfd4wjb01300000gn/T/7024790777193223986/allure-report
Starting web server...2020-10-13 21:39:56.174:INFO::main: Logging initialized @6818ms to org.eclipse.jetty.util.log.StdErrLog
Server started at. Press to exit
View Code
生成的报告:
allure特性-testcase
关联测试用例(可以直接给测试用例的地址链接)
例子:
TEST_CASE_LINK = ‘https://mirrors.huaweicloud.com/‘@allure.testcase(TEST_CASE_LINK,‘测试用例连接‘)deftest_search(self):print(‘搜索用例‘)
按重要性级别进行一定范围测试
通常测试有P0、冒烟测试、验证上线测试。按重要性级别来执行的,比如上线要把主流程和重要模块都跑一遍,可通过以下方法解决
通过allure.feature,allure.story
也可以通过allure.severity来附加标记
级别:
trivial:不重要,轻微缺陷(必输项无提示,或者提示不规范)
minor 不太重要,次要缺陷(界面错误与UI需求不符)
normal:正常问题,普通缺陷(数值计算错误)
critical:严重,临界缺陷(功能点缺失)
blocker:阻塞,中断缺陷(客户端程序无响应,无法执行下一步操作)
举例说明:
#!/usr/bin/python#-*- coding: UTF-8 -*-
"""@author:chenshifeng
@file:test_severity.py
@time:2020/10/11"""
importallureimportpytest#不加任何标记
deftest_with_no_severity():pass
#trivial:不重要,轻微缺陷(必输项无提示,或者提示不规范)
@allure.severity(allure.severity_level.TRIVIAL)deftest_with_trivial_severity():pass
#minor 级别 不太重要,次要缺陷(界面错误与UI需求不符)
@allure.severity(allure.severity_level.MINOR)deftest_with_minor_severity():pass
#normal:正常问题,普通缺陷(数值计算错误)
@allure.severity(allure.severity_level.NORMAL)deftest_with_normal_severity():pass
#critical:严重,临界缺陷(功能点缺失)
@allure.severity(allure.severity_level.TRIVIAL)deftest_with_trivial_severity():pass
#blocker:阻塞,中断缺陷(客户端程序无响应,无法执行下一步操作)
@allure.severity(allure.severity_level.BLOCKER)deftest_with_blocker_severity():pass@allure.severity(allure.severity_level.NORMAL)classTestClassWithNormalSeverity(object):deftest_inside_with_normal_severity(self):pass@allure.severity(allure.severity_level.CRITICAL)deftest_inside_with_critical_severity(self):pass
View Code
执行:
chenshifengdeMacBook-Pro:testcode chenshifeng$ pytest test_severity.py --alluredir=./result --clean-alluredir============================================================================= test session starts =============================================================================platform darwin-- Python 3.9.0, pytest-6.1.1, py-1.9.0, pluggy-0.13.1rootdir:/Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: allure-pytest-2.8.18collected7items
test_severity.py ....... [100%]============================================================================== 7 passed in 0.04s ==============================================================================chenshifengdeMacBook-Pro:testcode chenshifeng$ allure serve ./result
Generating report to temp directory...
Report successfully generated to/var/folders/p0/3_7fwrvx6n3ftpfd4wjb01300000gn/T/12787840024126794872/allure-report
Starting web server...2020-10-13 22:03:47.200:INFO::main: Logging initialized @6670ms to org.eclipse.jetty.util.log.StdErrLog
Server started at. Press to exit
View Code