UnitTest 参数化---Parameterized安装

本文介绍了如何使用Parameterized在Python中进行pip安装,包括使用豆瓣镜像源,并展示了如何在unittest测试中利用`@parameterized.expand`实现参数化,减少重复操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Parameterized安装命令

常见安装:pip install parameterized
或使用国内豆瓣镜像源安装:pip install parameterized -i https://pypi.douban.com/simple

shell页面(安装页面cmd内)

C:\Users\S3214>pip install parameterized -i https://pypi.douban.com/simple
Looking in indexes: https://pypi.douban.com/simple
Requirement already satisfied: parameterized in c:\programs\python\python38\lib\site-packages (0.9.0)
WARNING: You are using pip version 19.2.3, however version 23.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

二、核心代码

@parameterized.expand()

Code

#参数化
# 下载pip install parameterized -i https://pypi.douban.com/simple
#  @parameterized.expand()
# 效果,减少重复操作
import unittest
# 导包错误不是import parameterized 而是 from parameterized import parameterized
from parameterized import parameterized
from mathfunc import add, minus, my_random

# 写测试用例(继承mathfunc中写好的函数)
class add_minus_test(unittest.TestCase):
    # 参数化---》减少重复操作()
    @parameterized.expand([(5,6,11),(2,-3,-1),(-3,-2,-5),(2,0,2)])
    def test_add(self,a,b,c):
        num = add(a, b)
        self.assertEqual(c, num)

效果图

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值