pytest【四、fixture高阶用法】

1、输入多组测试用例

自定义fixture可以添加多组测试用例数据,在测试用例函数当做实参传入
示例代码如下:

ps: 其中注释掉的测试函数用例test_get_min1和test_get_min2作用相同,若定义fixture时指定name参数,即@pytest.fixture(params=[...], name='function_get_min_data')该用法时,定义的fixture作为实参时候就必须使用name的值,若无指定,则用定义的fixture函数名,二者不可混用

import pytest


def get_min(data: tuple) -> int:
    min_ = data[0]
    for i in data:
        if min_ > i:
            min_ = i
    return min_


class TestMyDemo:
    @pytest.fixture(params=[((1, 3, 5), 1),
                            ((2, 7, 4), 2),
                            ((7, 56, 5, 3), 4)])
    def demo_data(self, request):
        return request.param

    # def test_get_min1(self, function_get_min_data):
    #     test_data = function_get_min_data[0]
    #     test_result = function_get_min_data[1]
    #     assert get_min(test_data) == test_result

    def test_get_min2(self, demo_data):
        test_data = demo_data[0]
        test_result = demo_data[1]
        assert get_min(test_data) == test_result

运行结果如下:
在这里插入图片描述

2、对session、mudule、class、function的公共资源配置

可以使用@pytest.fixture(scope="function", autouse=True, params=[...], name="xxx")进行测试用例的一些公共资源配置,

  1. scope指定作用域,为session时在整个测试会话之前执行,module在该测试模块(文件)之
    前执行,class在执行测试用例类之前执行,function在执行测试用例函数之前执行
  2. autouse是否自动执行,默认False
  3. params要传入的参数列表
  4. name定义的fixture名字

有如下测试用例:

import pytest


def get_min(data: tuple) -> int:
    min_ = data[0]
    for i in data:
        if min_ > i:
            min_ = i
    return min_


def get_max(data: tuple) -> int:
    max_ = data[0]
    for i in data:
        if max_ < i:
            max_ = i
    return max_


class TestMyDemo:
    @pytest.fixture(params=[((1, 3, 5), 1, 5),
                            ((2, 7, 4), 2, 7),
                            ((7, 56, 5, 3), 3, 56)], name="testcase_data")
    def demo_data(self, request):
        return request.param

    @pytest.fixture(scope="session", autouse=True, name="config_session_fixture")
    def config_session(self):
        print("\nInit Session Resource")

    @pytest.fixture(scope="module", autouse=True, name="config_module_fixture")
    def config_module(self):
        print("\tInit Module Resource")

    @pytest.fixture(scope="class", autouse=True, name="config_class_fixture")
    def config_class(self):
        print("\t\tInit Class Resource")

    @pytest.fixture(scope="function", autouse=True, name="config_function_fixture")
    def config_function(self, request):
        print("\n\t\t\tInit Function[%s] Resource" % request.function.__name__)

    def test_get_min(self, testcase_data):
        test_data = testcase_data[0]
        test_result = testcase_data[1]
        assert get_min(test_data) == test_result

    def test_get_max(self, testcase_data):
        test_data = testcase_data[0]
        test_result = testcase_data[2]
        assert get_max(test_data) == test_result

pytest执行测试用例,结果如下:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

八戒无戒i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值