用Python提供测试数据
如何针对大量不同的数据运行相同的测试 我想报告所有的故障 例如:用Python提供测试数据,python,unit-testing,Python,Unit Testing,如何针对大量不同的数据运行相同的测试 我想报告所有的故障 例如: def isEven(number): return True # Quite buggy implementation data = [ (2, True), (3, False), (4, True), (5, False), ] class MyTest: def evenTest(self, num, expected): self.assertEquals(
def isEven(number):
return True # Quite buggy implementation
data = [
(2, True),
(3, False),
(4, True),
(5, False),
]
class MyTest:
def evenTest(self, num, expected):
self.assertEquals(expected, isEven(num))
我已找到仅在第一次失败时产生错误的解决方案:
如何运行测试以报告所有故障?一种解决方案是为
数据中的每个条目创建不同的测试用例实例:
import unittest
data = [
(2, True),
(3, False),
(4, True),
(5, False)]
# This should be imported from a separate module.
def isEven(number):
return True # Quite buggy implementation
class TestIsEven(unittest.TestCase):
def test_is_even(self):
for num, expected in data:
self.assertEqual(expected, isEven(num))
class MyTest(unittest.TestCase):
def __init__(self, num, expected):
unittest.TestCase.__init__(self, "evenTest")
self.num = num
self.expected = expected
def evenTest(self):
self.assertEqual(self.expected, isEven(self.num))
要了解如何构造测试用例,请在模块中添加函数:
def load_tests(loader, tests, pattern):
return unittest.TestSuite(MyTest(num, expected)
for num, expected in data)
您可能正在寻找以下内容:
import unittest
def is_even(number):
return True # Quite buggy implementation
class TestCase(unittest.TestCase):
def setUp(self):
self.expected_output = [
(2, True),
(3, False),
(4, True),
(5, False)
]
def test_is_even(self):
real_res = []
for arg, _ in self.expected_output:
real_res.append((arg, is_even(arg)))
msg_error = '\nFor %s Expected %s Got %s'
msg = []
for res1, res2 in zip(real_res, self.expected_output):
if res1[1] != res2[1]:
msg.append(msg_error % (res1[0], res1[1], res2[1]))
self.assertEqual(real_res, self.expected_output, "".join(msg))
if __name__ == '__main__':
unittest.main()
import unittest
data = [
(2, True),
(3, False),
(4, True),
(5, False)]
# This should be imported from a separate module.
def isEven(number):
return True # Quite buggy implementation
def create_test_func(num, expected):
def _test_func(self):
self.assertEqual(expected, isEven(num))
return _test_func
class TestIsEven(unittest.TestCase):
pass
# pyunit isn't Pythonic enough. Use py.test instead
# till then we rely on such hackery
import new
for i, (num, expected) in enumerate(data):
setattr(TestIsEven, 'test_data_%d'%i, create_test_func(num, expected))
if __name__ == "__main__":
unittest.main()
输出:
F
======================================================================
失败:测试为偶数(主测试用例)
----------------------------------------------------------------------
回溯(最近一次呼叫最后一次):
文件“test.py”,第29行,在test_示例中
self.assertEqual(real_res,self.expected_输出,'.join(msg))
断言者错误:
对于3,预期的“真”变为“假”
对于5,预期的“真”变为“假”
----------------------------------------------------------------------
在0.000秒内运行了1次测试
失败(失败=1)
您应该使用py.test
。我认为unittest模块是从JUnit盲目复制的。不管怎样,你可以这样做:
import unittest
def is_even(number):
return True # Quite buggy implementation
class TestCase(unittest.TestCase):
def setUp(self):
self.expected_output = [
(2, True),
(3, False),
(4, True),
(5, False)
]
def test_is_even(self):
real_res = []
for arg, _ in self.expected_output:
real_res.append((arg, is_even(arg)))
msg_error = '\nFor %s Expected %s Got %s'
msg = []
for res1, res2 in zip(real_res, self.expected_output):
if res1[1] != res2[1]:
msg.append(msg_error % (res1[0], res1[1], res2[1]))
self.assertEqual(real_res, self.expected_output, "".join(msg))
if __name__ == '__main__':
unittest.main()
import unittest
data = [
(2, True),
(3, False),
(4, True),
(5, False)]
# This should be imported from a separate module.
def isEven(number):
return True # Quite buggy implementation
def create_test_func(num, expected):
def _test_func(self):
self.assertEqual(expected, isEven(num))
return _test_func
class TestIsEven(unittest.TestCase):
pass
# pyunit isn't Pythonic enough. Use py.test instead
# till then we rely on such hackery
import new
for i, (num, expected) in enumerate(data):
setattr(TestIsEven, 'test_data_%d'%i, create_test_func(num, expected))
if __name__ == "__main__":
unittest.main()
输出为:
.F
======================================================================
失败:测试数据1(主测试)
----------------------------------------------------------------------
回溯(最近一次呼叫最后一次):
文件“untitled-1.py”,第15行,在_test_func中
self.assertEqual(应为,isEven(num))
断言者错误:错误!=真的
======================================================================
失败:测试数据3(主测试)
----------------------------------------------------------------------
回溯(最近一次呼叫最后一次):
文件“untitled-1.py”,第15行,在_test_func中
self.assertEqual(应为,isEven(num))
断言者错误:错误!=真的
----------------------------------------------------------------------
在0.000秒内运行了4次测试
失败(失败=2)
使用这种方法,您可以添加更多细节,如打印故障调试信息等。如果您正在使用,您可以这样做:
import pytest
def is_even(number):
return True # Wuite buggy implementation
@pytest.mark.parametrize("number, expected", [
(2, True),
(3, False),
(4, True),
(5, False)
])
def test_is_even(number, expected):
assert is_even(number) == expected
您将获得以下内容(缩短):
/tmp/test\u it.py:13:AssertionError
==========2失败,2在0.01秒内通过====================
这引发了第一个错误——请再次阅读问题的结尾。如果我有50次测试,其中10次失败,则很难找到发生了什么。对于一小部分简单的测试数据,它将非常方便和有用。谢谢:)@jinowolski:没错,我们很高兴可以自定义错误消息,我们想自定义多少就自定义多少我编辑了我的答案,我不知道这对你是否有用。@Anurag:这就是我所希望的,一个(简单的)功能一个测试,即使我们想拆分测试,至少它们都应该在一个测试用例中解决,如果这只是一个大软件包的一部分,我无法想象将其拆分为多个测试用例,但这只是我的拙见:)+1,在上,我认为unittest模块是盲目地从junit复制的,并且它不是唯一一个从java复制的模块,比如:)当其他答案被提供时,mark Parameterize可能不存在,但这里感觉这是正确的答案。可能的重复:。这本身就是一个复制品。为什么,为什么不?