一.使用 Allure2 运行方式-Python
# --alluredir 参数生成测试报告。
# 在测试执行期间收集结果
pytest [测试用例/模块/包] --alluredir=./result/ (—alluredir这个选项 用于指定存储测试结果的路径)
# 生成在线的测试报告
allure serve ./result
二.使用 Allure2 运行方式-Java
1.使用 allure:report 参数生成测试报告
# 在测试执行期间收集结果
# mvn命令行使用 maven插件安装
mvn clean test allure:report
# 生成在线的测试报告
# mvn 直接找target/allure-results目录
mvn allure:serve
2.运行mvn命令对应没有在target下面生成allure-results目录。在src/test/resources路径下配置allure配置文件allure.properties,指名allure报告生成路径。
allure.results.directory=target/allure-resultsa
3.运行mvn命令一直卡在下载中,解决方法
①在项目下创建.allure文件夹。
②下载allure解压到.allure文件夹下。
三.生成测试报告
1.生成测试报告需要使用命令行工具allure
2.命令格式allure [option] [command] [command options]
# 步骤一:在测试执行期间收集结果
# —alluredir这个选项 用于指定存储测试结果的路径
pytest [测试文件] -s –q --alluredir=./result/
# 如果要清除已经生成的报告的历史记录,可以添加参数--clean-alluredir
pytest [测试文件] -s –q --alluredir=./result/ --clean-alluredir
# 步骤二:查看测试报告,注意这里的serve书写
allure serve ./result/
3.Allure报告生成的两种方式
方式一:在线报告,会直接打开默认浏览器展示当前报告。
# 方式一:测试完成后查看实际报告,在线查看报告,会直接打开默认浏览器展示当前报告。
allure serve ./result/
方式二:静态资源文件报告(带 index.html、css、js等文件),需要将报告布署到 web服务器上。
a.应用场景:如果希望随时打开报告,可以生成一个静态资源文件报告,将这个报告布署到 web 服务器上,启动 web 服务,即可随时随地打开报告。
b.解决方案:使用allure generate 生成带有 index.html 的结果报告。这种方式需要两个步骤:第一步:生成报告。第二步:打开报告。
# 生成报告
allure generate ./result
# 打开报告
allure open ./report/
4.常用参数
①allure generate 可以指定输出路径,也可以清理上次的报告记录。
②-o / –output 输出报告的路径。
③-c / –clean 如果报告路径重复。
④allure open 打开报告。
⑤-h / –host 主机 IP 地址,此主机将用于启动报表的 web 服务器。
⑥-p / –port 主机端口,此端口将用于启动报表的 web 服务器,默认值:0。
# 生成报告,指定输出路径,清理报告。
allure generate ./result -o ./report --clean
# 打开报告,指定IP地址和端口。
allure open -h 127.0.0.1 -p 8883 ./report/
四.Allure2 报告中添加用例标题
通过使用装饰器@allure.title可以为测试用例自定义一个可阅读性的标题。allure.title的三种使用方式如下:
方式一:直接使用装饰器@allure.title 为测试用例自定义标题。
import allure
import pytest
@allure.title("自定义测试用例标题")
def test_with_title():
assert True
方式二:@allure.title支持通过占位符的方式传递参数,可以实现测试用例标题参数化,动态生成测试用例标题。
import allure
import pytest
@allure.title("参数化用例标题:参数一:{param1} ,参数二: {param2}")
@pytest.mark.parametrize("param1, param2, expected", [
(1, 1, 2),
(0.1, 0.3, 0.4)
])
def test_with_parametrize_title(param1, param2, expected):
assert param1 + param2 == expected
方式三:allure.dynamic.title 动态更新测试用例标题。
@allure.title("原始标题")
def test_with_dynamic_title():
assert True
allure.dynamic.title("更改后的新标题")
五.Allure2报告中添加用例步骤
方法一:使用装饰器定义一个测试步骤,在测试用例中使用。
# 方法一:使用装饰器定义一个测试步骤,在测试用例中使用
import allure
import pytest
# 定义测试步骤:simple_step1
@allure.step
def simple_step1(step_param1, step_param2 = None):
'''定义一个测试步骤'''
print(f"步骤1:打开页面,参数1: {step_param1}, 参数2:{step_param2}")
# 定义测试步骤:simple_step2
@allure.step
def simple_step2(step_param):
'''定义一个测试步骤'''
print(f"步骤2:完成搜索 {step_param} 功能")
@pytest.mark.parametrize('param1', ["pytest", "allure"], ids=['search pytest', 'search allure'])
def test_parameterize_with_id(param1):
simple_step2(param1) # 调用步骤二
@pytest.mark.parametrize('param1', [True, False])
@pytest.mark.parametrize('param2', ['value 1', 'value 2'])
def test_parametrize_with_two_parameters(param1, param2):
simple_step1(param1, param2) # 调用步骤一
@pytest.mark.parametrize('param2', ['pytest', 'unittest'])
@pytest.mark.parametrize('param1,param3', [[1,2]])
def test_parameterize_with_uneven_value_sets(param1, param2, param3):
simple_step1(param1, param3) # 调用步骤一
simple_step2(param2) # 调用步骤二
方法二:使用 with allure.step() 添加测试步骤。
# 方法二:使用 `with allure.step()` 添加测试步骤
@allure.title("搜索用例")
def test_step_in_method():
with allure.step("测试步骤一:打开页面"):
print("操作 a")
print("操作 b")
with allure.step("测试步骤二:搜索"):
print("搜索操作 ")
with allure.step("测试步骤三:断言"):
assert True
六.Allure2报告中添加用例链接
应用场景:将报告与bug管理系统或测试用例管理系统集成,可以添加链接装饰器@allure.link、@allure.issue和@allure.testcase。
1.@allure.link(url, name) 添加一个普通的 link 链接。
2.@allure.testcase(url, name) 添加一个用例管理系统链接。
3.@allure.issue(url, name),添加 bug 管理系统
# 格式1:添加一个普通的link 链接
@allure.link('https://ceshiren.com/t/topic/15860')
def test_with_link():
pass
# 格式1:添加一个普通的link 链接,添加链接名称
@allure.link('https://ceshiren.com/t/topic/15860', name='这是用例链接地址')
def test_with_named_link():
pass
# 格式2:添加用例管理系统链接
TEST_CASE_LINK = 'https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637'
@allure.testcase(TEST_CASE_LINK, '用例管理系统')
def test_with_testcase_link():
pass
# 格式3:添加bug管理系统链接
# 这个装饰器在展示的时候会带 bug 图标的链接。可以在运行时通过参数 `--allure-link-pattern` 指定一个模板链接,以便将其与提供的问题链接类型链接模板一起使用。执行命令需要指定模板链接:
`--allure-link-pattern=issue:https://abc.com/t/topic/{}`
@allure.issue("15860", 'bug管理系统')
def test_with_issue():
pass
七.Allure2报告中添加用例分类
1.Allure分类
(1)应用场景:可以为项目,以及项目下的不同模块对用例进行分类管理。也可以运行某个类别下的用例。
(2)报告展示:类别会展示在测试报告的Behaviors栏目下。
(3)Allure提供了三个装饰器:
·@allure.epic:敏捷里面的概念,定义史诗,往下是 feature。
· @allure.feature:功能点的描述,理解成模块往下是 story。
· @allure.story:故事 story 是 feature 的子集。
2.Allure分类 - epic
场景:希望在测试报告中看到用例所在的项目,需要用到 epic,相当于定义一个项目的需求,由于粒度比较大,在epic下面还要定义略小粒度的用户故事。
装饰器:@allure.epic
import allure
@allure.epic("需求1")
class TestEpic:
def test_case1(self):
print("用例1")
def test_case2(self):
print("用例2")
def test_case3(self):
print("用例3")
3.Allure分类 - feature/story
场景: 希望在报告中看到测试功能,子功能或场景。
装饰器: @allure.Feature、@allure.story
import allure
@allure.epic("需求1")
@allure.feature("功能模块1")
class TestEpic:
@allure.story("子功能1")
@allure.title("用例1")
def test_case1(self):
print("用例1")
@allure.story("子功能2")
@allure.title("用例2")
def test_case2(self):
print("用例2")
@allure.story("子功能2")
@allure.title("用例3")
def test_case3(self):
print("用例3")
@allure.story("子功能1")
@allure.title("用例4")
def test_case4(self):
print("用例4")
4.Allure运行feature/story
# allure相关的命令查看
pytest --help|grep allure
#通过指定命令行参数,运行 epic/feature/story 相关的用例:
pytest 文件名
--allure-epics=EPICS_SET --allure-features=FEATURES_SET --allure-stories=STORIES_SET
# 只运行 epic 名为 "需求1" 的测试用例
pytest --alluredir ./results --clean-alluredir --allure-epics=需求1
# 只运行 feature 名为 "功能模块2" 的测试用例
pytest --alluredir ./results --clean-alluredir --allure-features=功能模块2
# 只运行 story 名为 "子功能1" 的测试用例
pytest --alluredir ./results --clean-alluredir --allure-stories=子功能1
# 运行 story 名为 "子功能1和子功能2" 的测试用例
pytest --alluredir ./results --clean-alluredir --allure-stories=子功能1,子功能2
# 运行 feature + story 的用例(取并集)
pytest --alluredir ./results --clean-alluredir --allure-features=功能模块1 --allure-stories=子功能1,子功能2
Allure epic/feature/story 的关系
5.总结
epic:敏捷里面的概念,用来定义史诗,相当于定义一个项目。
feature:相当于一个功能模块,相当于 testsuite,可以管理很多个子分支 story。
story:相当于对应这个功能或者模块下的不同场景,分支功能。
epic 与 feature、feature 与 story 类似于父子关系。
八.Allure2 报告中添加用例描述
1.应用场景:Allure 支持往测试报告中对测试用例添加非常详细的描述语,用来描述测试用例详情。
2.Allure添加描述的四种方式:
方式一:使用装饰器 @allure.description() 传递一个字符串参数来描述测试用例。
@allure.description("""
多行描述语:<br/>
这是通过传递字符串参数的方式添加的一段描述语,<br/>
使用的是装饰器 @allure.description
""")
def test_description_provide_string():
assert True
方式二:使用装饰器 @allure.description_html 传递一段 HTML 文本来描述测试用例。
@allure.description_html("""html代码块""")
def test_description_privide_html():
assert True
方式三:直接在测试用例方法中通过编写文档注释的方法来添加描述。
def test_description_docstring():
"""
直接在测试用例方法中
通过编写文档注释的方法
来添加描述。
:return:
"""
assert True
方式四:用例代码内部动态添加描述信息。
import allure
@allure.description("""这个描述将被替换""")
def test_dynamic_description():
assert 42 == int(6 * 7)
allure.dynamic.description('这是最终的描述信息')
# allure.dynamic.description_html(''' html 代码块 ''')
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理