pytest文档38-allure.step()添加测试用例步骤

前言

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

@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
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值