25-pytest-参数化生成用例标题

Posted 爱学习de测试小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了25-pytest-参数化生成用例标题相关的知识,希望对你有一定的参考价值。

参数化生成用例标题


前言

  • 本篇来学习两种方式参数化生成用例标题

ids参数化用例标题

  • 未添加ids参数
# -*- coding: utf-8 -*-
# @Time    : 2022/3/18
# @Author  : 大海

import os
import allure
import pytest


def login(username, password):
    """登录"""
    print("输入账号:%s" % username)
    print("输入密码:%s" % password)
    # 返回
    return "code": 0, "msg": "success!"


# 输入数据和期望值
test_data = [
    ("username": "user1", "password": "123456", "success!"),
    ("username": "user2", "password": "1234567", "failed!"),
    ("username": "user3", "password": "12345678", "success!"),
]


@allure.story("验证登录功能")
@pytest.mark.parametrize("test_input,expected", test_data)
def test_login(test_input, expected):
    """验证登录功能"""
    # 获取函数返回结果
    result = login(test_input["username"], test_input["password"])
    # 断言
    assert result["msg"] == expected


if __name__ == '__main__':
    os.system('pytest -s test_45.py --alluredir=./allure_report --clean-alluredir')
    os.system('allure serve ./allure_report')
  • 查看报告
  • 添加ids参数
# -*- coding: utf-8 -*-
# @Time    : 2022/3/18
# @Author  : 大海
import os
import allure
import pytest


def login(username, password):
    """登录"""
    print("输入账号:%s" % username)
    print("输入密码:%s" % password)
    # 返回
    return "code": 0, "msg": "success!"


# 输入数据和期望值
test_data = [
    ("username": "user1", "password": "123456", "success!"),
    ("username": "user2", "password": "1234567", "failed!"),
    ("username": "user3", "password": "12345678", "success!"),
]


@allure.story("验证登录功能")
@pytest.mark.parametrize("test_input,expected", test_data, ids=[
    "输入正确账号,密码,登录成功",
    "输入错误账号,密码,登录失败",
    "输入正确账号,密码,登录成功",
])
def test_login(test_input, expected):
    """验证登录功能"""
    # 获取函数返回结果
    result = login(test_input["username"], test_input["password"])
    # 断言
    assert result["msg"] == expected


if __name__ == '__main__':
    os.system('pytest -s test_45.py --alluredir=./allure_report --clean-alluredir')
    os.system('allure serve ./allure_report')
  • 查看报告

allure.title参数化用例标题

  • parametrize和allure.title结合使用
# -*- coding: utf-8 -*-
# @Time    : 2022/3/18
# @Author  : 大海

import os
import allure
import pytest


def login(username, password):
    """登录"""
    print("输入账号:%s" % username)
    print("输入密码:%s" % password)
    # 返回
    return "code": 0, "msg": "success!"


# 输入数据和期望值
test_data = [
    ("username": "user1", "password": "12345", "success!", "输入正确账号,密码,登录成功"),
    ("username": "user2", "password": "123456", "failed!", "输入错误账号,密码,登录失败"),
    ("username": "user3", "password": "1234567", "success!", "输入正确账号,密码,登录成功"),
]


@allure.story("验证登录功能")
@allure.title("title")
@pytest.mark.parametrize("test_input,expected,title", test_data)
def test_login(test_input, expected, title):
    """验证登录功能"""
    # 获取函数返回结果
    result = login(test_input["username"], test_input["password"])
    # 断言
    assert result["msg"] == expected


if __name__ == '__main__':
    os.system('pytest -s test_46.py --alluredir=./allure_report --clean-alluredir')
    os.system('allure serve ./allure_report')
  • 查看报告

allure.dynamic生成用例标题

  • allure.dynamic.title(‘标题’)
# -*- coding: utf-8 -*-
# @Time    : 2022/3/18
# @Author  : 大海

# -*- coding: utf-8 -*-
# @Time    : 2022/3/18
# @Author  : 大海

import os
import allure
import pytest


def login(username, password):
    """登录"""
    print("输入账号:%s" % username)
    print("输入密码:%s" % password)
    # 返回
    return "code": 0, "msg": "success!"


# 输入数据和期望值
test_data = [
    ("username": "user1", "password": "12345", "success!", "输入正确账号,密码,登录成功"),
    ("username": "user2", "password": "123456", "failed!", "输入错误账号,密码,登录失败"),
    ("username": "user3", "password": "1234567", "success!", "输入正确账号,密码,登录成功"),
]


@allure.story("验证登录功能")
@pytest.mark.parametrize("test_input,expected,title", test_data)
def test_login(test_input, expected, title):
    """验证登录功能"""
    # 获取函数返回结果
    result = login(test_input["username"], test_input["password"])
    # 断言
    assert result["msg"] == expected
    allure.dynamic.title(title)


if __name__ == '__main__':
    os.system('pytest -s test_47.py --alluredir=./allure_report --clean-alluredir')
    os.system('allure serve ./allure_report')

  • 查看报告
  • 属性

allure.dynamic.feature :模块名称
allure.dynamic.link:测试报告中的链接
allure.dynamic.issue:缺陷
allure.dynamic.testcase:测试用例的链接地址
allure.dynamic.story:用户故事
allure.dynamic.title:用例的标题
allure.dynamic.description:用例描述
allure.severity :用例等级

以上是关于25-pytest-参数化生成用例标题的主要内容,如果未能解决你的问题,请参考以下文章

pytest文档69-Hook函数之参数化生成测试用例pytest_generate_tests

unittest介绍

Python使用闭包自动生成函数

第三次冲刺

HttpRunner3源码阅读:4. loader项目路径加载,用例文件转换方法字典生成

片段着色器会覆盖光栅化生成的片段颜色吗?