5

Does @ddt work with py.test or it must use unittest format? I have a tests where the setup fixture is in the conftest.py file. When I run the test, it error out because it didn't run the setup fixture. E.g:

@ddt
class Test_searchProd:
  @data(['clothes': 3],['shoes': 4])
  @unpack
  def test_searchAllProduct(setup,productType):
      .....

Basically the setup fixture is to open a specific URL... Am I doing something incorrect or @ddt does not work with py.test?

KaGo
  • 123
  • 1
  • 8

1 Answers1

10

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              
Bruno Oliveira
  • 13,694
  • 5
  • 43
  • 41