gpt4 book ai didi

python - @ddt 是否与 py.test 一起使用?

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:41 26 4
gpt4 key购买 nike

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

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

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

最佳答案

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

还要注意 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:

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

关于python - @ddt 是否与 py.test 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29836946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com