用python提供测试数据

2024-06-26 13:17:00 发布

您现在位置:Python中文网/ 问答频道 /正文

如何对大量不同的数据运行相同的测试?我想被报告所有的失败。

例如:

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/

如何运行要报告的所有失败的测试?


Tags: 数据selffalsetruenumberdatareturndef
3条回答

你应该使用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)

使用这种方法,您可以添加更多的细节,例如在出现故障时打印调试信息等

一种解决方案是为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知道如何构造测试用例,请在模块中添加一个^{}函数:

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

输出:

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)

相关问题 更多 >