Allure2的运行方式–python
- 使用
--alluredir
参数生成测试报告。
# 在测试执行期间收集结果
pytest [测试用例/模块/包] --alluredir=./result/ (—alluredir这个选项 用于指定存储测试结果的路径)
# 生成在线的测试报告
allure serve ./result
Allure 报告生成
Allure报告生成有两种方式
- 方式一:在线报告,会直接打开默认浏览器展示当前报告。
allure serve ./result/ (注意这里的serve书写)
- 方式二:静态资源文件报告(带 index.html、css、js 等文件),需要将报告布署到 web 服务器上。
# 生成报告
allure generate ./result
# 打开报告
allure open ./report/
Allure的常用参数
- allure generate 可以指定输出路径,也可以清理上次的存储的报告记录。
- -o / –output 输出报告的路径。
- -c / –clean 如果报告路径重复。
- allure open 打开报告。
- -h / –host 主机 IP 地址,此主机将用于启动报表的 web 服务器。
- -p / –port 主机端口,此端口将用于启动报表的 web 服务器,默认值:0
Allure2添加用例标题
- 通过使用装饰器
@allure.title
可以为测试用例自定义一个可阅读性的标题。 - allure.title的三种使用方式:
- 直接使用 @allure.title 为测试用例自定义标题。
- @allure.title`支持通过占位符的方式传递参数,可以实现测试用例标题参数化,动态生成测试用例标题。
3.allure.dynamic.title 动态更新测试用例标题。
Allure2用例添加用例步骤
- Allure 支持两种方法:
- 方法一:使用装饰器定义一个测试步骤,在测试用例中使用。
# 方法一:使用装饰器定义一个测试步骤,在测试用例中使用
import allure
import pytest
@allure.step
def simple_step1(step_param1, step_param2 = None):
'''定义一个测试步骤'''
print(f"步骤1:打开页面,参数1: {step_param1}, 参数2:{step_param2}")
@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
。
Allure 分类
- 应用场景:可以为项目,以及项目下的不同模块对用例进行分类管理。也可以运行某个类别下的用例。
- 报告展示:类别会展示在测试报告的 Behaviors 栏目下。
- Allure 提供了三个装饰器:
-
@allure.epic
:敏捷里面的概念,定义史诗,往下是 feature。
-
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")
-
@allure.feature
:功能点的描述,理解成模块往下是 story。 -
@allure.story
:故事 story 是 feature 的子集。
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")
Allure epic/feature/story 的关系
- epic:敏捷里面的概念,用来定义史诗,相当于定义一个项目。
- feature:相当于一个功能模块,相当于 testsuite,可以管理很多个子分支 story。
- story:相当于对应这个功能或者模块下的不同场景,分支功能。
- epic 与 feature、feature 与 story 类似于父子关系。