ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Allure2 报告中添加用例步骤 :@allure.step

Allure2 报告中添加用例步骤 :@allure.step 一. Allure2 报告中添加用例步骤 :allure.step1. 简介在编写自动化测试用例时经常会遇到需要编写流程性测试用例的场景这种用例的测试步骤通常比较多为了让测试报告更清晰易懂我们通常需要将复杂的测试流程拆解为详细的步骤。Allure 框架提供了两种主要方式来实现这一点让报告不再是冷冰冰的代码执行结果而是清晰的业务流程记录。场景针对流程性较强的测试用例步骤较多。目的提高测试用例的可阅读性让测试报告更直观方便定位问题。1.2 Allure2 报告中添加用例步骤在 Allure2 报告中添加测试用例的详细步骤可以通过allure.step装饰器和with allure.step代码块实现。方法一使用装饰器allure.step这种方式适合将具体的操作封装成函数。用法: 在定义函数时使用 allure.step(“步骤描述”)。效果: 当测试用例调用该函数时Allure 报告中会自动记录这一步骤。特点: 支持参数化可以动态显示传入的参数值。代码示例importallureimportpytestallure.step()defsimple_step1(step_param1,step_param2None):定义一个测试步骤print(f步骤1打开页面参数1:{step_param1}, 参数2{step_param2})allure.stepdefsimple_step2(step_param):定义一个测试步骤print(f步骤2完成搜索{step_param}功能)pytest.mark.parametrize(param1,[pytest,allure],ids[search pytest,search allure])deftest_parameterize_with_id(param1):simple_step2(param1)pytest.mark.parametrize(param1,[True,False])pytest.mark.parametrize(param2,[value 1,value 2])deftest_parametrize_with_two_parameters(param1,param2):simple_step1(param1,param2)pytest.mark.parametrize(param2,[pytest,unittest])pytest.mark.parametrize(param1,param3,[[1,2]])deftest_parameterize_with_uneven_value_sets(param1,param2,param3):simple_step1(param1,param3)simple_step2(param2)方法二使用代码块 with allure.step这种方式适合在测试函数内部直接定义步骤。用法在测试方法内部使用 with allure.step(“步骤描述”): 包裹具体的代码逻辑。效果报告会将 with 块内的代码归纳为一个步骤展示。特点灵活性高适合写死在某个特定用例中的逻辑。代码示例allure.title(搜索用例)deftest_step_in_method():withallure.step(测试步骤一打开页面):print(操作 a)print(操作 b)withallure.step(测试步骤二搜索):print(搜索操作 )withallure.step(测试步骤三断言):assertTrue
返回列表