Python主要讲究简洁简单使用,所以它不像junit一样支持参数化测试,需要改装一下也可以传参。直接上代码实例

  1. import unittest
  2. class ParametrizedTestCase(unittest.TestCase):
  3. """ TestCase classes that want to be parametrized should
  4. inherit from this class.
  5. """
  6. def __init__(self, methodName='runTest', param=None):
  7. super(ParametrizedTestCase, self).__init__(methodName)
  8. self.param = param
  9. @staticmethod
  10. def parametrize(testcase_klass, param=None):
  11. """ Create a suite containing all tests taken from the given
  12. subclass, passing them the parameter 'param'.
  13. """
  14. testloader = unittest.TestLoader()
  15. testnames = testloader.getTestCaseNames(testcase_klass)
  16. suite = unittest.TestSuite()
  17. for name in testnames:
  18. suite.addTest(testcase_klass(name, param=param))
  19. return suite
  20. #####################################################
  21. ##用法-testcase
  22. class TestOne(ParametrizedTestCase):
  23. def test_something(self):
  24. print 'param =', self.param
  25. self.assertEqual(1, 1)
  26. def test_something_else(self):
  27. self.assertEqual(2, 2)
  28. ##用法-测试
  29. suite = unittest.TestSuite()
  30. suite.addTest(ParametrizedTestCase.parametrize(TestOne, param=42))
  31. suite.addTest(ParametrizedTestCase.parametrize(TestOne, param=13))
  32. unittest.TextTestRunner(verbosity=2).run(suite)
  33. #结果
  34. test_something (__main__.TestOne) ... param = 42
  35. ok
  36. test_something_else (__main__.TestOne) ... ok
  37. test_something (__main__.TestOne) ... param = 13
  38. ok
  39. test_something_else (__main__.TestOne) ... ok
  40. ----------------------------------------------------------------------
  41. Ran 4 tests in 0.000s
  42. OK

或者可以使用meta类来 解决这个问题

    1. import unittest
    2. l = [["foo", "a", "a",], ["bar", "a", "a"], ["lee", "b", "b"]]
    3. class TestSequenceMeta(type):
    4. def __new__(mcs, name, bases, dict):
    5. def gen_test(a, b):
    6. def test(self):
    7. self.assertEqual(a, b)
    8. return test
    9. for tname, a, b in l:
    10. test_name = "test_%s" % tname
    11. dict[test_name] = gen_test(a,b)
    12. return type.__new__(mcs, name, bases, dict)
    13. class TestSequence(unittest.TestCase):
    14. __metaclass__ = TestSequenceMeta
    15. if __name__ == '__main__':
    16. unittest.main()

转载自:http://blog.csdn.net/hqzxsc2006/article/details/50125735

unittest改写传参方法的更多相关文章

  1. 学习chrome 插件 DHC ,http请求传参方法

    DHC的简介 DHC是一款可以帮助用户使用chrome插件模拟HTTP客户端发送测试数据到服务器的谷歌浏览器插件,在chrome中安装了DHC插件以后,就可在服务器端代码初步完成的时候,使用DHC进行 ...

  2. jquery-uploadify传参方法

    jquery-uploadify传参方法$(document).ready(function () { $("#uploadify").uploadify({ 'uploader' ...

  3. js方法之间的调用之——传参方法

    在最近项目需求中发现,完成一些功能的时候总是要调很多结构类似的方法,写起来很繁琐,所以就想写一个“万能”方法,是的代码更简洁.即:把一个方法作为参数传给这个“万能”方法,让它去执行你给定的方法,就类似 ...

  4. 定时器setTimeout()的传参方法

    更具体的代码:http://www.cnblogs.com/3body/p/5416830.html // 由于setTimeout()的延迟执行特性,所以在执行的函数中直接使用外部函数的变量是无法获 ...

  5. AngularJS中页面传参方法

    1.基于ui-router的页面跳转传参 (1) 用ui-router定义路由,比如有两个页面,一个页面(producers.html)放置了多个producers,点击其中一个目标,页面跳转到对应的 ...

  6. TKinter当Label绑定bind事件时传参方法

    记录下tkinter的 当在label绑定bind事件时,遇到需要传参时的解决方法(因为有event存在 所以不能直接传参) https://www.cnblogs.com/liyuanhong/ar ...

  7. 不用Ajax时的传参方法

    不用Ajax时的怎么传参 创建一个form表单 function test(){ var params = { "参数名": "参数值" }; postExce ...

  8. tp5闭包子查询传参方法

    在channel表中查询status,channel_id,channel_name,account_level这些字段,且这些字段的channel_id不在adv_id为$id的表adv_chann ...

  9. laravel console handle 传参方法

    <?php namespace App\Console\Commands; use Illuminate\Console\Command; use App\Libs\wxpay\CLogFile ...

随机推荐

  1. featureCounts 软件说明

    featuresCounts 软件用于定量,不仅可以支持gene的定量,也支持exon, gene bodies, genomic bins, chromsomal locations的定量: 官网 ...

  2. postgresql某进程占用cpu资源过高,降不下来

    由于是开发阶段,所以并没有配置postgres的参数,都是使用安装时的默认配置,以前运行也不见得有什么不正常,可是前几天我的cpu资源占用突然升高.查看进程,发现有一个postgres的进程占用CPU ...

  3. php处理数据分组问题

    很简单的一个需求,将数据库取出的二维数组进行按照id分组,同组的数据用逗号连接,例如: 处理为 就是按照id分组,name进行逗号拼接. 那么按照数据库的思路来说,采用group_concat即可,如 ...

  4. LeetCode - 768. Max Chunks To Make Sorted II

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

  5. 转载>>C# Invoke和BeginInvoke区别和使用场景

    转载>>C# Invoke和BeginInvoke区别和使用场景 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是dotnet程 ...

  6. 在iPhone手机上写了input type="date" 显示不出来的原因

    在iPhone手机上写了input type="date" 显示不出来的原因 今天在手机页面上使用新的input类型,这样子写,在chrome浏览器上浏览,很好,显示出来.然后用i ...

  7. gitlab服务器IP调整后修改domian或ip

    背景 本地搭建的gitlab 服务器,在 /etc/gitlab/gitlab.rb 中 external_url 通常是局域网ip的形式.如下所示 external_url 'http://192. ...

  8. springboot源码解读01

    package org.springframework.web; @javax.servlet.annotation.HandlesTypes({org.springframework.web.Web ...

  9. layui实现左侧菜单点击右侧内容区显示

    https://segmentfault.com/a/1190000014617129

  10. 【2】static 、construct

    [面向对象] 两个概念: 什么是类 具有一批相同属性的集合 什么是对象 特指的某一个具体的事物 [面向对象的三大特征] 1.封装 public 公共的 protected 受保护的 private 私 ...