【pytest-22】allure特性之attach和step

前言

  • 前篇文章主要介绍了allure的特性,这篇文章我们就来讲下allure的标记用法
  • allure.attach() 函数:可以设置需要显示在allure报告的附件,包含了多种类型,可以通过allure.attachment_type查看支持的类型
  • @allure.step() 装饰器:可以设置测试步骤,让测试用例的执行过程更加详细

使用

这里使用步骤1,2,3来示例

#encoding:utf-8
#@Time:2020/12/16 11:22
#@Author:sunny

import pytest
import time
import allure

def test_01(openBroswer,search_data):
    #将测试分为几个步骤
    with allure.step('测试步骤之前置操作'):
        print("前置步骤:用户先操作登录")
        allure.attach('我是附加内容', '我是附加内容的标题')  # 要将一些内容附加到测试报告
    with allure.step('测试步骤2'):
        print("测试步骤2:去查询数据")
    with allure.step('测试步骤3'):
        print("测试步骤3:增加数据")
    time.sleep(1)

def test_02(openBroswer,search_data):
    print("toutiao用例2")
    time.sleep(1)

def test_03(openBroswer,search_data):
    print("toutiao用例3")


if __name__ == '__main__':
    pytest.main(["-s","test_b.py"])

运行结果:

更复杂一点的场景

测试步骤:1.登录,2.查询数据 3.编辑 4.保存,具体实现,(也可以把步骤放在公共类里,测试用例单独放)

#toutiao/test_e.py

#encoding:utf-8
#@Time:2020/12/21 11:32
#@Author:sunny

import allure
from common.com_utils import *
import pytest

'''
1.登录,2.查询数据 3.编辑 4.保存
'''

def login(username,password):
    print("前置操作:先登录")

def search():
    print("搜索")

def edit(id="23"):
    print("编辑增加")

def save():
    print("保存")

@pytest.fixture(scope="session")
def login_setup():
    login("xiaoxiao","123")

def test_add_save(login_setup):
   '''
   测试添加和保存
   :return:
   '''
   with allure.step('步骤2:搜索'):
       search()
   with allure.step('步骤3:编辑增加'):
       edit()
   with allure.step('步骤4:保存'):
       save()

运行结果:

 这里看出还是上一个的结果,因为增量的缓存,需要清理

--clean-alluredir 清空

如果你想每次用例执行之前先清空allure的报告记录,可以加个参数  --clean-alluredir 

带上 clean-alluredir 参数重新执行用例

运行结果:

根据上面可知,我们的allure也可以写在函数的开头用装饰器的方式,好像用处不如在函数内部使用更为直观便捷。

 运行结果:

 

posted @ 2020-12-22 10:12  做一只热爱生活的小透明  阅读(615)  评论(0)    收藏  举报