ddt http://ddt.readthedocs.org/en/latest/旨在被使用TestCase
子类,因此它不适用于裸测试类。但请注意 pytest 可以运行TestCase
使用的子类ddt
很好,所以如果您已经有一个基于 ddt 的测试套件,它应该使用 pytest 运行器无需修改即可运行。
另请注意 pytest 有参数化 http://pytest.org/latest/parametrize.html,它可以用来替换许多由ddt
.
例如,以下基于 ddt 的测试:
@ddt
class FooTestCase(unittest.TestCase):
@data(1, -3, 2, 0)
def test_not_larger_than_two(self, value):
self.assertFalse(larger_than_two(value))
@data(annotated(2, 1), annotated(10, 5))
def test_greater(self, value):
a, b = value
self.assertGreater(a, b)
在 pytest 中成为:
class FooTest:
@pytest.mark.parametrize('value', (1, -3, 2, 0))
def test_not_larger_than_two(self, value):
assert not larger_than_two(value)
@pytest.mark.parametrize('a, b', [(2, 1), (10, 5)])
def test_greater(self, a, b):
assert a > b
或者,如果您愿意,您甚至可以完全取消该课程:
@pytest.mark.parametrize('value', (1, -3, 2, 0))
def test_not_larger_than_two(value):
assert not larger_than_two(value)
@pytest.mark.parametrize('a, b', [(2, 1), (10, 5)])
def test_greater(a, b):
assert a > b