Python的单元测试和动态创建测试用例 [英] Python's unittest and dynamic creation of test cases
问题描述
可能重复:
如何在python中生成动态(参数化)单元测试?
有没有办法动态创建 unittest
测试用例?我试过以下..
class test_filenames(unittest.TestCase):
def setUp自我):
的类别,test.cases在files.items():
为testindex,curtest在枚举(testcases):
def thetest():
parser = FileParser(curtest ['input'])
theep = parser.parse()
self.assertEquals(theep.episodenumber,curtest ['episodenumber'])
setattr(self,'test_ %s_%02d'%(category,testindex),测试)
..它创建所有的方法正确的(它们显示在 dir()
中并且是可调用的),但是unittest的测试检测器也不是 nosetest
执行它们0在...中进行测试)
由于我可能会提出错误的问题 - 我正在努力实现:
我有一个包含测试数据的文件,输入文件名列表和预期的数据ta(简化为 episodenumber
在上述代码中),存储在Python字典中。关键是类别,值是测试用例的列表,例如..
test_cases = {}
pre>
test_cases ['example_1'] = [
{'input':'test.01',
'episodenumber':1},
{'input':'test.02'
'episodenumber':2}
]
test_cases ['example_2'] = [
{'input':'another.123',
'episodenumber':123},
{'input':'test.e42',
'episodenumber':32}
]
目前,我只是循环所有数据,每次测试调用$ code> self.assertEquals 。问题是,如果一个失败,我没有看到其余的故障,因为它们也被分组成一个测试,当断言失败时中断。
我认为,将(动态地)为每个测试用例创建一个函数,也许还有一个更好的方法?
解决方案为此,您应该在鼻子中使用测试生成器。所有你需要做的是产生一个元组,第一个是一个函数,其余的是args。这个例子就是这个例子。
def test_evens():
for i in range(0,5) :
yield check_even,i,i * 3
def check_even(n,nn):
assert n%2 == 0或nn%2 == 0
Possible Duplicate:
How to generate dynamic (parametrized) unit tests in python?Is there a way to dynamically create
unittest
test cases? I have tried the following..class test_filenames(unittest.TestCase): def setUp(self): for category, testcases in files.items(): for testindex, curtest in enumerate(testcases): def thetest(): parser = FileParser(curtest['input']) theep = parser.parse() self.assertEquals(theep.episodenumber, curtest['episodenumber']) setattr(self, 'test_%s_%02d' % (category, testindex), thetest)
..which creates all the methods correctly (they show up in
dir()
and are callable), but unittest's test detector, nornosetest
executes them ("Ran 0 tests in ...")Since I may be asking the wrong question - what I am trying to achieve:
I have a file containing test data, a list of input filenames, and expected data (simplified to
episodenumber
in the above code), stored in a Python dictionary. The key is the category, the value is a list of test cases, for example..test_cases = {} test_cases['example_1'] = [ {'input': 'test.01', 'episodenumber': 1}, {'input': 'test.02', 'episodenumber': 2} ] test_cases['example_2'] = [ {'input': 'another.123', 'episodenumber': 123}, {'input': 'test.e42', 'episodenumber': 32} ]
Currently I just loop over all the data, call
self.assertEquals
on each test. The problem is, if one fails, I don't see the rest of the failures as they are also grouped into one test, which aborts when an assertion fails.The way around this, I thought, would be to (dynamically) create a function for each test case, perhaps there is a better way?
解决方案For this you should use test generators in nose. All you need to do is yield a tuple, with the first being a function and the rest being the args. From the docs here is the example.
def test_evens(): for i in range(0, 5): yield check_even, i, i*3 def check_even(n, nn): assert n % 2 == 0 or nn % 2 == 0
这篇关于Python的单元测试和动态创建测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!