【python】【unittest】【parameterized】如何在unittest中使用parameterized传递参数

安装

pip install parameterized

例子

parameterized传递的参数都是列表类型,列表中每一项是一个测试用例,注意如果列表中的一项为map类型,那么直接打印参数为map的key值,并不是它的value值,如下所示

 @parameterized.expand([{"key1": "value1"}, {"key2": "value2"}])
 def test_21(self, a):
     print("\n a is", a)
 # a is key1
 # a is key2

完整代码如下

from parameterized import parameterized, param
import unittest
import xlrd


def read_cases(file_name):
    book = xlrd.open_workbook(file_name)
    sh = book.sheet_by_index(0)
    case = []
    for rx in range(sh.nrows):
        item = {}
        for cx in range(sh.ncols):
            if rx != 0:
                item[sh.cell_value(0, cx)] = sh.cell_value(rx, cx)
        if item:
            case.append(item)
    return case


cases = read_cases("cases.xlsx")
print("cases is", cases)


class TestCreateDashboard(unittest.TestCase):

    # @parameterized.expand([param(1, 2), param(3, 4)])
    @parameterized.expand([(1, 2), (3, 4)])
    def test_1(self, num1, num2):
        print("\n num is ", num1, num2)
    # num is 1 2
    # num is 3 4

    @parameterized.expand([{"key1": "value1"}, {"key2": "value2"}])
    def test_21(self, a):
        print("\n a is", a)
    # a is key1
    # a is key2

    # @parameterized.expand([param({"key1": "value1"}, {"key2": "value2"})])
    @parameterized.expand([({"key1": "value1"}, {"key2": "value2"})])
    def test_2(self, a, b):
        print("\n a is", a, "b is", b)
    # a is {'key1': 'value1'} b is {'key2': 'value2'}

    @parameterized.expand(cases)
    def test_3(self, uid, name, b, url, para, bbb, expect):
        print("\n", uid, name, b, url, para, bbb, expect)


if __name__ == "__main__":
    unittest.main()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值