您的位置 首页 > 腾讯云社区

pytest文档38-allure.setp添加测试用例步骤---上海-悠悠

前言

一般流程性的测试用例,写成自动化用例时,步骤较多写起来会比较长。在测试用例里面添加详细的步骤有助于更好的阅读,也方便报错后快速的定位到问题。 举个常见的测试场景用例:从登陆开始,到浏览商品添加购物车,最后下单支付

用例步骤:

1.登陆, 2.浏览商品 3.添加购物车 4.生成订单 5.支付成功

用例设计

先把上面的每个环节,写成函数放到common_fucntion.py

# common_fucntion.py import allure import pytest ''' 流程性的用例,添加测试步骤,让用例更清晰 用例步骤:1.登陆, 2.浏览商品 3.添加购物车 4.生成订单 5.支付成功 作者:上海-悠悠 QQ交流群:779429633 ''' def login(username, password): '''登陆''' print("前置操作:先登陆") def open_goods(): '''浏览商品''' print("浏览商品") def add_shopping_cart(goods_id="10086"): '''添加购物车''' print("添加购物车") def buy_goods(): '''生成订单''' print("buy") def pay_goods(): '''支付''' print("pay")

接下来测试用例设计,登陆可以单独拿出来,当成前置操作,后面的步骤合起来就是一个用例test_allure_step.py

# test_allure_step.py import allure import pytest from .common_function import * ''' 流程性的用例,添加测试步骤,让用例更清晰 用例步骤:1.登陆, 2.浏览商品 3.添加购物车 4.生成订单 5.支付成功 作者:上海-悠悠 QQ交流群:779429633 ''' @pytest.fixture(scope="session") def login_setup(): login("yoyo", "123456") @allure.feature("功能模块") @allure.story("测试用例小模块-成功案例") @allure.title("测试用例名称:流程性的用例,添加测试步骤") def test_add_goods_and_buy(login_setup): ''' 用例描述:前置:登陆 用例步骤:1.浏览商品 2.添加购物车 3.购买 4.支付成功 ''' with allure.step("step1:浏览商品"): open_goods() with allure.step("step2:添加购物车"): add_shopping_cart() with allure.step("step3:生成订单"): buy_goods() with allure.step("step4:支付"): pay_goods() with allure.step("断言"): assert 1 == 1

执行用例,生成allure报告

> pytest --alluredir ./allure_report test_allure_step.py > allure serve ./allure_report

报告展示效果如下

测试步骤@allure.step()

测试步骤也可以在 common_fucntion.py 里面定义的函数上加上装饰器实现:@allure.step()

import allure import pytest ''' 流程性的用例,添加测试步骤,让用例更清晰 用例步骤:1.登陆, 2.浏览商品 3.添加购物车 4.生成订单 5.支付成功 ''' @allure.step("setup:登陆") def login(username, password): '''登陆''' print("前置操作:先登陆") @allure.step("step:浏览商品") def open_goods(): '''浏览商品''' print("浏览商品") @allure.step("step:添加购物车") def add_shopping_cart(goods_id="10086"): '''添加购物车''' print("添加购物车") @allure.step("step:生成订单") def buy_goods(): '''生成订单''' print("buy") @allure.step("step:支付") def pay_goods(): '''支付''' print("pay")

测试用例设计 test_allure_step_x.py

import allure import pytest from .common_function import * @pytest.fixture(scope="session") def login_setup(): login("yoyo", "123456") @allure.feature("功能模块") @allure.story("测试用例小模块-成功案例") @allure.title("第二种实现方式:流程性的用例,添加测试步骤") def test_add_goods_and_buy_2(login_setup): ''' 用例描述:前置:登陆 用例步骤:1.浏览商品 2.添加购物车 3.购买 4.支付成功 ''' open_goods() add_shopping_cart(goods_id="10086") buy_goods() pay_goods() assert 1 == 1

执行用例,生成allure报告

> pytest --alluredir ./allure_report test_allure_step_x.py > allure serve ./allure_report

报告展示效果如下

两种方式对比

使用 with allure.step("step:步骤") 这种方式代码可读性更好一点,但不会带上函数里面的传参和对应的值。 使用 @allure.step("step:步骤") 这种方式会带上函数的传参和对应的值。 这两种方式结合起来使用,才能更好的展示测试报告!

---来自腾讯云社区的---上海-悠悠

关于作者: 瞎采新闻

这里可以显示个人介绍!这里可以显示个人介绍!

热门文章

留言与评论(共有 0 条评论)
   
验证码: