ddt is meant to be used by TestCase
subclasses, so it won't work for bare test classes. But note that pytest can run TestCase
subclasses which use ddt
just fine, so if you already have a ddt-based test suite it should run without modifications using the pytest runner.
Also note that pytest has parametrize, which can be used to replace many use cases supported by ddt
.
For example, the following ddt-based tests:
@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)
Become in 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
Or you can even get rid of the class altogether if you prefer:
@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