allure报告中如何让测试步骤更清晰

一、allure step测试用例步骤说明

allure step编写测试用例有两种方式

1、with allure.step()用在测试用例中

公共方法代码:

# common_fucntion.py
import allure
import pytest
from common.tools.log_util import Logger
 
runlog=Logger().get_log
'''
流程性的用例,添加测试步骤,让用例更清晰
用例步骤:1.登陆, 2.浏览商品 3.添加购物车  4.生成订单  5.支付成功
'''
 
def login(username, password):
    '''登陆'''
    # print("前置操作:先登陆")
    allure.attach("username:{},password:{}".format(username,password),"前置操作:先登陆")
 
 
def open_goods():
    '''浏览商品'''
    # print("浏览商品")
    allure.attach("这是浏览商品操作")
 
 
def add_shopping_cart(goods_id="10086"):
    '''添加购物车'''
    print("添加购物车")
    # runlog.info("这是runlog.info打印出的信息")
    # runlog.error("这是runlog.error打印出的信息")
 
 
def buy_goods():
    '''生成订单'''
    print("buy")
 
 
def pay_goods():
    '''支付'''
    print("pay")

用例代码:

import allure
import sys
from projects.demo_project.interface import common_function
 
@allure.story(u'with allure.step()')
def test_case003(self):
    """测试用例三
    流程性的用例,添加测试步骤,让报告更清晰
    用例步骤:1.登录 2.浏览商品 3:添加购物车 4.生成订单 5.支付成功
    """
    with allure.step("step1:登录"):
        common_function.login("zhangsan", "12345")
    with allure.step("step2:浏览商品"):
        common_function.open_goods()
    with allure.step("step3:添加购物车"):
        common_function.add_shopping_cart()
    with allure.step("step4:生成订单"):
        common_function.buy_goods()
    with allure.step("step5:支付成功"):
        common_function.pay_goods()

测试报告:
在这里插入图片描述

2、@allure.step()用装饰器的方式修饰在测试步骤的函数上面

公共方法代码:

# common_fucntion.py
import allure
import pytest
 
'''
流程性的用例,添加测试步骤,让用例更清晰
用例步骤:1.登陆, 2.浏览商品 3.添加购物车  4.生成订单  5.支付成功
'''
 
 
@allure.step("step:登录")
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")

测试用例代码:

from projects.demo_project.interface import common_function_allure_step
 
@allure.story(u'@allure.step()')
def test_case004(self):
    """测试用例三
    流程性的用例,添加测试步骤,让报告更清晰
    用例步骤:1.登录 2.浏览商品 3:添加购物车 4.生成订单 5.支付成功
    """
 
    common_function_allure_step.login("zhangsan", "12345")
 
    common_function_allure_step.open_goods()
 
    common_function_allure_step.add_shopping_cart()
 
    common_function_allure_step.buy_goods()
 
    common_function_allure_step.pay_goods()

报告效果图:
在这里插入图片描述
allure.step这两种用法,要结合实际编写测试用例中的场景,看哪种方式更适合,也可以结合着使用。

但allure.step也并不是完美的,当报告中想在步骤中显示参数、预期结果,实际结果,或者截图,文件等内容时,用print或logging来输出。看一下下面截图效果
在这里插入图片描述
print的信息显示在了stdout中,logging.info信息显示在log中,当有logging.error时,又出现了stderr。显然很乱,也没有在对应的测试步骤中显示。为了解决这个问题,研究了allure.attach()这个方法,可以解决。

二、allure.attach():补充说明测试结果

用法1: allure.attach(body, name, attachment_type, extension)
参数说明:

body:要显示的内容或附件
name:附件名字
attachment_type:附件类型
extension:附件扩展名
attachment_type提供的附件类型:
在这里插入图片描述
样例:
在这里插入图片描述
实现


def login(username, password):
    '''登陆'''
    # print("前置操作:先登陆")
    allure.attach("username:{},password:{}".format(username,password),"前置操作:先登陆")
 
 
def open_goods():
    '''浏览商品'''
    # print("浏览商品")
    allure.attach("这是内容:浏览商品操作","这是标题")

用法二:allure.attach.file(source, name, attachment_type, extension)
source:文件路径,相当于传一个文件
其他参数和上面一致

注:allure.attach() 和 allure.attach.file() 的区别:

allure.attach()表示自己写一个文件,allure.attach.file()从外部传一个文件

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值