如何解决用python提供测试数据
| 如何对许多不同的数据运行相同的测试?我想被举报所有失败。 例如:def isEven(number):
return True # quite buggy implementation
data = [
(2,True),(3,False),(4,(5,]
class MyTest:
def evenTest(self,num,expected):
self.assertEquals(expected,isEven(num))
我找到了仅在首次失败时引发错误的解决方案:
http://melp.nl/2011/02/PHPunit-style-data-provider-in-python-unit-test/
如何运行测试以报告所有失败?
解决方法
您应该使用
py.test
,我认为unittest模块是从junit盲目复制的,无论如何您都可以这样破解
import unittest
data = [
(2,True),(3,False),(4,(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 enought 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.F
======================================================================
FAIL: test_data_1 (__main__.TestIsEven)
----------------------------------------------------------------------
Traceback (most recent call last):
File \"untitled-1.py\",line 15,in _test_func
self.assertEqual(expected,isEven(num))
AssertionError: False != True
======================================================================
FAIL: test_data_3 (__main__.TestIsEven)
----------------------------------------------------------------------
Traceback (most recent call last):
File \"untitled-1.py\",isEven(num))
AssertionError: False != True
----------------------------------------------------------------------
Ran 4 tests in 0.000s
FAILED (failures=2)
使用这种方法,您可以添加更多细节,例如在失败时打印调试信息等。
, 一种解决方案是为data
中的每个条目创建不同的测试用例实例:
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))
为了使unittest
知道如何构造测试用例,请向模块添加load_tests()
函数:
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,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()
输出:
F
======================================================================
FAIL: test_is_even (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File \"test.py\",line 29,in test_example
self.assertEqual(real_res,\'\'.join(msg))
AssertionError:
For 3 Expected True Got False
For 5 Expected True Got False
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
, 如果您使用的是pytest,则可以这样操作:
import pytest
def is_even(number):
return True # quite buggy implementation
@pytest.mark.parametrize(\"number,expected\",[
(2,False)
])
def test_is_even(number,expected):
assert is_even(number) == expected
您会得到类似(缩短)的信息:
/tmp/test_it.py:13: AssertionError
=========== 2 failed,2 passed in 0.01 seconds ====================
, 我还从这个答案中找到了阿德里安·帕纳西乌克的方法:
Python的单元测试和测试用例的动态创建
使用此解决方案,可以根据数据设置不同的测试名称。
, import unittest
data = [
(2,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))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。