python 单元测试 自动生成用例_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, nor nosetest 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值