pytest(三十三)--allure.step()添加测试用例步骤

前言

一般流程性的测试用例,写成自动化用例时,步骤较多写起来会比较长。在测试用例里面添加详细的步骤有助于更好的阅读,也方便报错后快速的定位到问题。

举个常见的测试场景用例:从登陆开始,到浏览商品添加购物车,最后下单支付。

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

用例设计

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

#c_function.py
import pytest
def login(uname,pwd):
    '''登录'''
    print("前置操作:登录")
def browse_goods():
    '''浏览商品'''
    print("浏览商品")
def add_goods(goods_id="110"):
    '''添加购物车'''
    print("添加购物车")
def buy_goods():
    print("生成订单")
def pay_goods():
    print("付款")  

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

#test_a.py
import allure
import pytest
from .c_function import *

@pytest.fixture(scope="session")
def login_setup():
    login("admin","123456")

@allure.feature("功能模块")
@allure.story("测试小故事")
@allure.title("测试用例名称")
def test_add_buy_goods(login_setup):
    '''用例描述:
    前置:登录
    用例步骤:浏览,添加,生成订单,支付
    '''
    with allure.step("step1:浏览商品"):
        browse_goods()
    with allure.step("step2:添加购物车"):
        add_goods()
    with allure.step("step three:生成订单"):
        buy_goods()
    with allure.step("step four:支付"):
        pay_goods()
    with allure.step("断言"):
        assert 1==1  

执行用例

pytest --alluredir ./report/x  

生成allure报告

allure serve ./report/x

 

报告展示效果如下

 测试步骤@allure.step()

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

#c_function.py
import pytest
import allure
@allure.step("setup:登录")
def login(uname,pwd):
    '''登录'''
    print("前置操作:登录")

@allure.step("step:浏览商品")
def browse_goods():
    '''浏览商品'''
    print("浏览商品")

@allure.step("step:添加购物车")
def add_goods(goods_id="110"):
    '''添加购物车'''
    print("添加购物车")

@allure.step("生成订单")
def buy_goods():
    print("生成订单")

@allure.step("支付")
def pay_goods():
    print("付款")

测试用例设计test_a.py

#test_a.py
import allure
import pytest
from .c_function import *

@pytest.fixture(scope="session")
def login_setup():
    login("admin","123456")

@allure.feature("功能模块")
@allure.story("测试小故事")
@allure.title("测试用例名称")
def test_add_buy_goods(login_setup):
    '''用例描述:
    前置:登录
    用例步骤:浏览,添加,生成订单,支付
    '''
    #with allure.step("step1:浏览商品"):
    browse_goods()
    #with allure.step("step2:添加购物车"):
    add_goods()
    #with allure.step("step three:生成订单"):
    buy_goods()
    #with allure.step("step four:支付"):
    pay_goods()
    #with allure.step("断言"):
    assert 1==1  

执行用例

pytest --alluredir ./report/x  

生成allure报告

allure serve ./report/x

报告展示效果如下

 两种方式对比

使用with allure.step("step:步骤")这种方式代码可读性更好一点,但报告中不会带上函数里面的传参和对应的值。

使用@ allure.step("step:步骤")这种方式会带上函数的传参和对应的值。

这两种方式结合起来使用,才能更好的展示测试报告!

越努力,越幸运!!! good good study,day day up!!!
原文地址:https://www.cnblogs.com/canglongdao/p/13415764.html