Allure报告 02-pytest+Allure报告

1. 用例等级

用例的严重等级大致分为以下几种:

  • BLOCKER = 'blocker'
  • CRITICAL = 'critical'
  • NORMAL = 'normal'
  • MINOR = 'minor'
  • TRIVIAL = 'trivial'
# test_allure.py

import allure
@allure.severity('normal')
@allure.feature('登录')
@allure.title('输入正确的用户名和密码')
def test_x1():
    """输入正确的用户名和密码"""
    print('x11111')

@allure.severity('critical')
@allure.feature('注册')
@allure.title('输入错误的用户名和密码')
def test_x2():
    """输入错误的用户名和密码"""
    print('x222222')

@allure.severity('trivial')
@allure.feature('登录')
@allure.title('输入正确的用户名和密码')
def test_x3():
    """输入正确的用户名和密码"""
    print('x11111')

@allure.severity('blocker')
@allure.feature('注册')
@allure.title('输入错误的用户名和密码')
def test_x4():
    """输入错误的用户名和密码"""
    print('x222222')

运行:pytest .\test_allure.py --alluredir ./report/allure_raw,再运行:allure serve .\report\allure_raw\,查看报告,即可区分用例已经按照指定进行等级的划分。

1.1 动态添加Allure属性

pytest结合allure描述用例的时候我们一般都是使用@allure.titile等方式进行描述用例的情况。在用例里面也可以动态更新标题和详情,使用allure.dynamic方式实现。allure方式都支持如下:

  • allure.dynamic.feature
  • allure.dynamic.story
  • allure.dynamic.title
  • allure.dynamic.description
    需要注意的是测试步骤,是用with allure.step()的方式实现。

注意的坑:这动态方法必须放在用例内部才生效,在开发关键字开发时,需要放在用例test函数内部才生效

feature模块:allure.dynamic.feature(name)
功能点:allure.dynamic.story(name)
用例标题:allure.dynamic.title(name)

# test_allure_2.py

import allure
import requests
import json

class TestDemo:

    def test_x1(self):
        """输入正确的用户名和密码"""
        with allure.step('步骤1:登录前'):
            print('步骤1:登录前')
        allure.dynamic.feature('动态添加功能点1')
        print('x11111')
        allure.dynamic.title('动态添加title11')
        with allure.step('步骤2:登录后'):
            print('登录结束')


    def test_x2(self):
        """输入错误的用户名和密码"""
        with allure.step('步骤1:登录前'):
            print('步骤1:登录前')
        allure.dynamic.feature('动态添加功能点2')
        print('x222222')
        allure.dynamic.title('动态添加title22')
        with allure.step('步骤2:登录后'):
            print('登录结束')

    def test_x3(self):
        """输入错误的用户名和密码"""
        allure.dynamic.feature('动态添加功能点1')
        print('x3333333')
        allure.dynamic.title('动态添加title33')

    def test_x4(self):
        url = 'http://10.xxx.212.xxx/api/v1/security/user/login'
        body = {
            "user_name": "zhitao",
            "password": "Eaton@2023"
        }
        allure.dynamic.feature('动态添加功能点2')
        allure.dynamic.title('用户登录')
        allure.attach(body=f'POST {url}\n {json.dumps(body)}', name='request', attachment_type=allure.attachment_type.TEXT)  # body参数传的是str型,所以需要进行序列化
        res = requests.post(url=url, json=body)
        print(f'响应结果:{res.text}')
        allure.attach(body=F'{res.status_code}  {res.reason}\n {res.text}', name='response', attachment_type=allure.attachment_type.TEXT)

1.3 全局conftest.py钩子实现

我们可以将公共的allure的标题或者模块放在钩子函数中动态传入,就省了很多重复的写allure.feature的工作。查看官方文档,给出了:pytest_runtest_call钩子函数。

# conftest.py
from pytets import Item
import allure

# Item: 表示每个用例对象,或用例模块对象
def pytest_runtest_call(item: Item):
    # 动态添加测试类的allure.feature()
    if item.parent._obj.__doc__:  # noqa: 表示不检查这行的语法错误
        allure.dynamic.feature(item.parent._obj.__doc__)  # 将test类的注释作为模块的名称
    # 动态添加测试用例title标题:allure.title()
    if item.function.__doc__:
        allure.dynamic.title(item.function.__doc__)  # 将testcase的注释作为用例的名称
posted @ 2024-01-19 20:06  dack_deng  阅读(25)  评论(0编辑  收藏  举报