【pytest】paramtrize 参数化、数据驱动

paramtrize 参数化:

pytest 中可以使用 @pytest.mak.paramtrize(argsnames, argsvalues, ids=None) 来实现参数化;

主要参数说明:

  • argsnames :参数名,是个字符串,如中间用逗号分隔则表示为多个参数名;
  • argsvalues :参数值,参数组成的列表,列表中有几个元素,就会生成几条测试用例;

使用方法:

  1. 使用 @pytest.mak.paramtrize() 装饰测试方法;
  2. parametrize('data', param) 中的 ‘data’ 是自定义的参数名,param 是引入的参数列表;
  3. 将自定义的参数名 data 作为参数传给测试用例 test_func;
  4. 然后就可以在测试用例内部使用 data 的参数了;

整个过程中,pytest 将参数列表 [1, 2, 3] 中的 3 个元素取出来,用每个元素生成 1 条测试用例,并且将元素赋值给 data 作为测试用例的参数,由测试用例自由使用;

# test_moduleName.py

import pytest

class TestClass_1:
    """测试类1"""

    param = [1, 2, 3]
    
    # 'data' :自定义的参数名
    # param  :参数列表
    @pytest.mark.parametrize('data', param)
    def test_func(self, data):  # 将参数名作为测试方法的参数
        """测试方法"""

        print('\n参数:', data)  # 实际得到的参数
# 执行结果:
============================= test session starts =============================
test_moduleName.py::TestClass_1::test_func[1] 
参数:data =  1
PASSED
test_moduleName.py::TestClass_1::test_func[2] 
参数:data =  2
PASSED
test_moduleName.py::TestClass_1::test_func[3] 
参数:data =  3
PASSED

============================== 3 passed in 0.03s ==============================

Process finished with exit code 0

多个参数名的用法:

前面讲了参数名可以就用逗号分隔,表示为多个参数名,当使用多个参数名时要注意,参数列表中的元素必须是可拆包的数组,并且数组内的元素数必须和参数名的数量相同;

# test_moduleName.py

import pytest


class TestClass_1:
    """测试类1"""

    param = [('admin1', 'pwd001'), ('admin2', 'pwd002'), ('admin3', 'pwd003')]
    # 因为参数名是 2 个,所以列表的每个元素都必须是长度为 2 的数组;
    @pytest.mark.parametrize('uname, pwd', param)
    def test_func(self, uname, pwd):
        """测试方法"""

        print('\n参数:uname = ', uname)
        print('参数:pwd = ', pwd)
# 执行结果
============================= test session starts =============================
test_moduleName.py::TestClass_1::test_func[admin1-pwd001] 
参数:uname =  admin1
参数:pwd =  pwd001
PASSED
test_moduleName.py::TestClass_1::test_func[admin2-pwd002] 
参数:uname =  admin2
参数:pwd =  pwd002
PASSED
test_moduleName.py::TestClass_1::test_func[admin3-pwd003] 
参数:uname =  admin3
参数:pwd =  pwd003
PASSED

============================== 3 passed in 0.02s ==============================

多次使用 paramtrize :

同一个测试用例还可以同时适用多个 @pytest.mak.paramtrize(),多个 paramtrize 的所有元素互相组合(类似笛卡儿乘积)生成大量测试用例;

# test_moduleName.py

import pytest


class TestClass_1:
    """测试类1"""

    @pytest.mark.parametrize('date3', ['value3-1', 'value3-2', 'value3-3'])
    @pytest.mark.parametrize('date2', ['value2-1', 'value2-2', 'value2-3'])
    @pytest.mark.parametrize('date1', ['value1-1', 'value1-2', 'value1-3'])
    def test_func(self, date1, date2, date3):
        """测试方法"""

        print('\n参数:date1 = ', date1)
        print('参数:date2 = ', date2)
        print('参数:date3 = ', date3)

如下所示,3 个 paramtrize() ,共有 3 * 3 * 3 = 27 种组合,pytest 便会生成 27 条测试用例!

test_moduleName.py::TestClass_1::test_func[value1.1-value2.1-value3.1] 
test_moduleName.py::TestClass_1::test_func[value1.1-value2.1-value3.2] 
test_moduleName.py::TestClass_1::test_func[value1.1-value2.1-value3.3] 
test_moduleName.py::TestClass_1::test_func[value1.1-value2.2-value3.1] 
test_moduleName.py::TestClass_1::test_func[value1.1-value2.2-value3.2] 
test_moduleName.py::TestClass_1::test_func[value1.1-value2.2-value3.3] 
test_moduleName.py::TestClass_1::test_func[value1.1-value2.3-value3.1] 
test_moduleName.py::TestClass_1::test_func[value1.1-value2.3-value3.2] 
test_moduleName.py::TestClass_1::test_func[value1.1-value2.3-value3.3] 
test_moduleName.py::TestClass_1::test_func[value1.2-value2.1-value3.1] 
test_moduleName.py::TestClass_1::test_func[value1.2-value2.1-value3.2] 
test_moduleName.py::TestClass_1::test_func[value1.2-value2.1-value3.3] 
test_moduleName.py::TestClass_1::test_func[value1.2-value2.2-value3.1] 
test_moduleName.py::TestClass_1::test_func[value1.2-value2.2-value3.2] 
test_moduleName.py::TestClass_1::test_func[value1.2-value2.2-value3.3] 
test_moduleName.py::TestClass_1::test_func[value1.2-value2.3-value3.1] 
test_moduleName.py::TestClass_1::test_func[value1.2-value2.3-value3.2] 
test_moduleName.py::TestClass_1::test_func[value1.2-value2.3-value3.3] 
test_moduleName.py::TestClass_1::test_func[value1.3-value2.1-value3.1] 
test_moduleName.py::TestClass_1::test_func[value1.3-value2.1-value3.2] 
test_moduleName.py::TestClass_1::test_func[value1.3-value2.1-value3.3] 
test_moduleName.py::TestClass_1::test_func[value1.3-value2.2-value3.1] 
test_moduleName.py::TestClass_1::test_func[value1.3-value2.2-value3.2] 
test_moduleName.py::TestClass_1::test_func[value1.3-value2.2-value3.3] 
test_moduleName.py::TestClass_1::test_func[value1.3-value2.3-value3.1] 
test_moduleName.py::TestClass_1::test_func[value1.3-value2.3-value3.2] 
test_moduleName.py::TestClass_1::test_func[value1.3-value2.3-value3.3] 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值