在python中提供测试数据

时间:2011-06-14 21:22:34

标签: 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(expected, isEven(num))

我找到的解决方案只会在第一次失败时引发错误: http://melp.nl/2011/02/phpunit-style-dataprovider-in-python-unit-test/

如何运行测试以报告所有故障?

6 个答案:

答案 0 :(得分:7)

一种解决方案是为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)

答案 1 :(得分:7)

你应该使用py.test,我认为unittest模块是从junit中盲目复制的,无论如何你可以像这样破解你的方式

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 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", line 15, in _test_func
    self.assertEqual(expected, isEven(num))
AssertionError: False != True

----------------------------------------------------------------------
Ran 4 tests in 0.000s

FAILED (failures=2)

使用这种方法,您可以添加更多细节,例如在失败时打印调试信息等

答案 2 :(得分:4)

如果您使用pytest,可以采用以下方式:

import pytest                                                                                                                   

def is_even(number):
    return True # quite 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_it.py:13: AssertionError
=========== 2 failed, 2 passed in 0.01 seconds ====================

答案 3 :(得分:3)

你在寻找这样的东西:

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()

输出:

F
======================================================================
FAIL: test_is_even (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 29, in test_example
    self.assertEqual(real_res, self.expected_output, ''.join(msg))
AssertionError: 
For 3 Expected True Got False
For 5 Expected True Got False

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

答案 4 :(得分:1)

我也从这个答案中找到了Adrian Panasiuk的方法:

Python's unittest and dynamic creation of test cases

使用此解决方案,可以根据数据设置不同的测试名称。

答案 5 :(得分:0)

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))