@ddt能够与py.test一起工作吗?

5
@ddt是否能与py.test一起使用,或者必须使用unittest格式? 我有一些测试,其中安装设置fixture在conftest.py文件中。当我运行测试时,出现错误,因为它没有运行安装设置fixture。例如:
@ddt
class Test_searchProd:
  @data(['clothes': 3],['shoes': 4])
  @unpack
  def test_searchAllProduct(setup,productType):
      .....

基本上,设置夹具是为了打开特定的URL……我是否做错了什么或者@ddt不能与py.test一起使用?

1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
10

ddt旨在由TestCase子类使用,因此它不适用于裸测试类。但请注意,pytest可以运行使用ddtTestCase子类,所以如果您已经有一个基于ddt的测试套件,它应该可以使用pytest runner而无需修改。

还要注意,pytest有parametrize,它可以用来替换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中使用fixture:

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              

太棒了!完全忘记了参数化功能。谢谢! - KaGo
我甚至不知道它的存在,感谢你让我摆脱了 DDT 的束缚。 - Marc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,