Does @ddt work with py.test?

Does @ddt work with py.test or should it use unittest format? I have tests where the install fixture is in the conftest.py file. When I run the test, it crashes because it doesn’t start the installer. For example:

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

      

Basically the installer has to open a specific url ... Am I doing something wrong or @ddt doesn't work with py.test?

+3


source to share


1 answer


ddt is intended to be used by subclasses TestCase

, so it won't work for simple test classes. But note that pytest can run subclasses TestCase

that use ddt

just fine, so if you already have a ddt based test suite it should work unchanged with pytest runner.

Also note that pytest has parametrize which can be used to replace many of the use cases it supports 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 get rid of the class altogether if you like:

@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              

      

+3


source







All Articles