zoukankan      html  css  js  c++  java
  • unittest参数化

    我们在写case的时候,如果用例的操作是一样的,就是参数不同,比如说要测一个登陆的接口,要测正常登陆的、黑名单用户登陆的、账号密码错误的等等,在unittest里面就要写多个case来测试。

    这样的情况只是调用接口的时候参数不一样而已,再写多个case的话就有点多余了,那怎么办呢,就得把这些参数都写到一个list里面, 然后循环去执行这个case。这样就可以省去写多个case了。

    当然有个第三方模块就直接有这样的功能,不需要咱们再自己写循环了。那就是nose-parameterized,直接pip安装即可。

    pip install nose-parameterized

    下面是代码

    import unittest
    from nose_parameterized import parameterized
    #导入这个模块
    class My(unittest.TestCase):
     
        @parameterized.expand(
            [
                (1,2,3),
                (1,2,3),
                (1,2,3),
                (1,2,4)
             ]
        )#使用它提供的装饰器装饰这个函数,把咱们写的这4个数据放到这个list里面
        def test1(self,a,b,c):
            self.assertEqual(a+b,c)
     
    if __name__=='__main__':
        unittest.main()

    下面是运行结果,咱们上面只运行了一次,可以发现,它自动帮咱们运行了4次。

  • 相关阅读:
    -webkit-line-clamp 多行文字溢出...
    整理一些知识碎片...
    localstorage sessionstorage和cookie的区别
    数据结构 --- Set
    Iterator(遍历器)
    ES6数组方法 -- reduce()
    ES6 -- 展开运算符
    Centos7 + Oracel 18c
    Mysql 查询返回大量数据导致内存溢出
    github的安装和使用
  • 原文地址:https://www.cnblogs.com/feng0815/p/8045859.html
Copyright © 2011-2022 走看看