unittest改写传参方法
Python主要讲究简洁简单使用,所以它不像junit一样支持参数化测试,需要改装一下也可以传参。直接上代码实例
- import unittest
- class ParametrizedTestCase(unittest.TestCase):
- """ TestCase classes that want to be parametrized should
- inherit from this class.
- """
- def __init__(self, methodName='runTest', param=None):
- super(ParametrizedTestCase, self).__init__(methodName)
- self.param = param
- @staticmethod
- def parametrize(testcase_klass, param=None):
- """ Create a suite containing all tests taken from the given
- subclass, passing them the parameter 'param'.
- """
- testloader = unittest.TestLoader()
- testnames = testloader.getTestCaseNames(testcase_klass)
- suite = unittest.TestSuite()
- for name in testnames:
- suite.addTest(testcase_klass(name, param=param))
- return suite
- #####################################################
- ##用法-testcase
- class TestOne(ParametrizedTestCase):
- def test_something(self):
- print 'param =', self.param
- self.assertEqual(1, 1)
- def test_something_else(self):
- self.assertEqual(2, 2)
- ##用法-测试
- suite = unittest.TestSuite()
- suite.addTest(ParametrizedTestCase.parametrize(TestOne, param=42))
- suite.addTest(ParametrizedTestCase.parametrize(TestOne, param=13))
- unittest.TextTestRunner(verbosity=2).run(suite)
- #结果
- test_something (__main__.TestOne) ... param = 42
- ok
- test_something_else (__main__.TestOne) ... ok
- test_something (__main__.TestOne) ... param = 13
- ok
- test_something_else (__main__.TestOne) ... ok
- ----------------------------------------------------------------------
- Ran 4 tests in 0.000s
- OK
或者可以使用meta类来 解决这个问题
- import unittest
- l = [["foo", "a", "a",], ["bar", "a", "a"], ["lee", "b", "b"]]
- class TestSequenceMeta(type):
- def __new__(mcs, name, bases, dict):
- def gen_test(a, b):
- def test(self):
- self.assertEqual(a, b)
- return test
- for tname, a, b in l:
- test_name = "test_%s" % tname
- dict[test_name] = gen_test(a,b)
- return type.__new__(mcs, name, bases, dict)
- class TestSequence(unittest.TestCase):
- __metaclass__ = TestSequenceMeta
- if __name__ == '__main__':
- unittest.main()
转载自:http://blog.csdn.net/hqzxsc2006/article/details/50125735
unittest改写传参方法的更多相关文章
- 学习chrome 插件 DHC ,http请求传参方法
DHC的简介 DHC是一款可以帮助用户使用chrome插件模拟HTTP客户端发送测试数据到服务器的谷歌浏览器插件,在chrome中安装了DHC插件以后,就可在服务器端代码初步完成的时候,使用DHC进行 ...
- jquery-uploadify传参方法
jquery-uploadify传参方法$(document).ready(function () { $("#uploadify").uploadify({ 'uploader' ...
- js方法之间的调用之——传参方法
在最近项目需求中发现,完成一些功能的时候总是要调很多结构类似的方法,写起来很繁琐,所以就想写一个“万能”方法,是的代码更简洁.即:把一个方法作为参数传给这个“万能”方法,让它去执行你给定的方法,就类似 ...
- 定时器setTimeout()的传参方法
更具体的代码:http://www.cnblogs.com/3body/p/5416830.html // 由于setTimeout()的延迟执行特性,所以在执行的函数中直接使用外部函数的变量是无法获 ...
- AngularJS中页面传参方法
1.基于ui-router的页面跳转传参 (1) 用ui-router定义路由,比如有两个页面,一个页面(producers.html)放置了多个producers,点击其中一个目标,页面跳转到对应的 ...
- TKinter当Label绑定bind事件时传参方法
记录下tkinter的 当在label绑定bind事件时,遇到需要传参时的解决方法(因为有event存在 所以不能直接传参) https://www.cnblogs.com/liyuanhong/ar ...
- 不用Ajax时的传参方法
不用Ajax时的怎么传参 创建一个form表单 function test(){ var params = { "参数名": "参数值" }; postExce ...
- tp5闭包子查询传参方法
在channel表中查询status,channel_id,channel_name,account_level这些字段,且这些字段的channel_id不在adv_id为$id的表adv_chann ...
- laravel console handle 传参方法
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use App\Libs\wxpay\CLogFile ...
随机推荐
- featureCounts 软件说明
featuresCounts 软件用于定量,不仅可以支持gene的定量,也支持exon, gene bodies, genomic bins, chromsomal locations的定量: 官网 ...
- postgresql某进程占用cpu资源过高,降不下来
由于是开发阶段,所以并没有配置postgres的参数,都是使用安装时的默认配置,以前运行也不见得有什么不正常,可是前几天我的cpu资源占用突然升高.查看进程,发现有一个postgres的进程占用CPU ...
- php处理数据分组问题
很简单的一个需求,将数据库取出的二维数组进行按照id分组,同组的数据用逗号连接,例如: 处理为 就是按照id分组,name进行逗号拼接. 那么按照数据库的思路来说,采用group_concat即可,如 ...
- 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 ...
- 转载>>C# Invoke和BeginInvoke区别和使用场景
转载>>C# Invoke和BeginInvoke区别和使用场景 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是dotnet程 ...
- 在iPhone手机上写了input type="date" 显示不出来的原因
在iPhone手机上写了input type="date" 显示不出来的原因 今天在手机页面上使用新的input类型,这样子写,在chrome浏览器上浏览,很好,显示出来.然后用i ...
- gitlab服务器IP调整后修改domian或ip
背景 本地搭建的gitlab 服务器,在 /etc/gitlab/gitlab.rb 中 external_url 通常是局域网ip的形式.如下所示 external_url 'http://192. ...
- springboot源码解读01
package org.springframework.web; @javax.servlet.annotation.HandlesTypes({org.springframework.web.Web ...
- layui实现左侧菜单点击右侧内容区显示
https://segmentfault.com/a/1190000014617129
- 【2】static 、construct
[面向对象] 两个概念: 什么是类 具有一批相同属性的集合 什么是对象 特指的某一个具体的事物 [面向对象的三大特征] 1.封装 public 公共的 protected 受保护的 private 私 ...