@ddt 可以与 py.test 一起使用吗?

2023-12-19

@ddt 是否与 py.test 一起使用还是必须使用 unittest 格式? 我有一个测试,其中设置夹具位于 conftest.py 文件中。当我运行测试时,它出错了,因为它没有运行设置夹具。例如:

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

基本上,设置装置是打开一个特定的 URL... 我做错了什么或者 @ddt 不适用于 py.test 吗?


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              
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

@ddt 可以与 py.test 一起使用吗? 的相关文章

随机推荐