Python 库“unittest":以编程方式生成多个测试 [英] Python library 'unittest': Generate multiple tests programmatically
问题描述
可能的重复:
如何在 Python 中生成动态(参数化)单元测试?
我有一个要测试的函数,under_test
,以及一组预期的输入/输出对:
我希望这些输入/输出对中的每一个都在其自己的 test_*
方法中进行测试.这可能吗?
这正是我想要的,但强制每个输入/输出对都进行单个测试:
class TestPreReqs(unittest.TestCase):定义设置(自我):self.expected_pairs = [(23, 55), (4, 32)]def test_expected(self):对于 self.expected_pairs 中的 exp:self.assertEqual(under_test(exp[0]), exp[1])如果 __name__ == '__main__':单元测试.main()
(另外,我真的想把 self.expected_pairs
的定义放在 setUp
中吗?)
更新: 尝试 doublep 的建议:
class TestPreReqs(unittest.TestCase):定义设置(自我):预期对 = [(2, 3),(42, 11),(3, 无),(31, 99),]对于 k,在 expected_pairs 中配对:setattr(TestPreReqs, 'test_expected_%d' % k, create_test(pair))def create_test(对):def do_test_expected(self):self.assertEqual(get_pre_reqs(pair[0]),pair[1])返回 do_test_expected如果 __name__ == '__main__':单元测试.main()
这不起作用.运行了 0 个测试.我是否错误地调整了示例?
未测试:
class TestPreReqs(unittest.TestCase):...def create_test(对):def do_test_expected(self):self.assertEqual(under_test(pair[0]),pair[1])返回 do_test_expected对于 k,在 enumerate ([(23, 55), (4, 32)]) 中配对:test_method = create_test(对)test_method.__name__ = 'test_expected_%d' % ksetattr (TestPreReqs, test_method.__name__, test_method)
如果你经常使用它,你可以通过使用实用函数和/或装饰器来美化它,我猜.请注意,在此示例中,pairs 不是 TestPreReqs
对象的属性(因此 setUp
消失了).相反,它们在某种意义上是硬连线"到 TestPreReqs
类.
Possible Duplicate:
How do you generate dynamic (parameterized) unit tests in Python?
I have a function to test, under_test
, and a set of expected input/output pairs:
[
(2, 332),
(234, 99213),
(9, 3),
# ...
]
I would like each one of these input/output pairs to be tested in its own test_*
method. Is that possible?
This is sort of what I want, but forcing every single input/output pair into a single test:
class TestPreReqs(unittest.TestCase):
def setUp(self):
self.expected_pairs = [(23, 55), (4, 32)]
def test_expected(self):
for exp in self.expected_pairs:
self.assertEqual(under_test(exp[0]), exp[1])
if __name__ == '__main__':
unittest.main()
(Also, do I really want to be putting that definition of self.expected_pairs
in setUp
?)
UPDATE: Trying doublep's advice:
class TestPreReqs(unittest.TestCase):
def setUp(self):
expected_pairs = [
(2, 3),
(42, 11),
(3, None),
(31, 99),
]
for k, pair in expected_pairs:
setattr(TestPreReqs, 'test_expected_%d' % k, create_test(pair))
def create_test (pair):
def do_test_expected(self):
self.assertEqual(get_pre_reqs(pair[0]), pair[1])
return do_test_expected
if __name__ == '__main__':
unittest.main()
This does not work. 0 tests are run. Did I adapt the example incorrectly?
Not tested:
class TestPreReqs(unittest.TestCase):
...
def create_test (pair):
def do_test_expected(self):
self.assertEqual(under_test(pair[0]), pair[1])
return do_test_expected
for k, pair in enumerate ([(23, 55), (4, 32)]):
test_method = create_test (pair)
test_method.__name__ = 'test_expected_%d' % k
setattr (TestPreReqs, test_method.__name__, test_method)
If you use this often, you could prettify this by using utility functions and/or decorators, I guess. Note that pairs are not an attribute of TestPreReqs
object in this example (and so setUp
is gone). Rather, they are "hardwired" in a sense to the TestPreReqs
class.
这篇关于Python 库“unittest":以编程方式生成多个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!