使用pytest,我可以这样定义fixture:
@pytest.fixture
def foo():
return "blah"
在这样的测试中使用它:
^{pr2}$一切都很好。但是我想做的是定义一个fixture函数,它“扩展”为一个测试函数提供多个参数。像这样:
@pytest.multifixture("foo,bar")
def foobar():
return "blah", "whatever"
def test_stuff(foo, bar):
assert foo == "blah" and bar == "whatever"
我想将这两个对象foo
和{
@pytest.fixture
def foo():
return "blah"
@pytest.multifixture("foo,bar")
def foobar():
f = foo()
return f, some_info_related_to(f)
这个例子可能看起来很傻,但在某些情况下foo
类似于请求对象,bar
对象需要链接到同一个请求对象。(也就是说,我不能将foo
和{
本质上,我要做的是将fixture函数的名称与测试函数参数的名称分离,这样我就可以定义一个fixture,它由测试函数签名中参数名的特定集触发,而不仅仅是一个名称与fixture函数名称相同的参数。在
当然,我总是可以返回一个tuple作为fixture的结果,然后自己在test函数中解压它。但是考虑到pytest提供了各种神奇的技巧来自动匹配名称和参数,似乎它也可以神奇地处理这个问题。pytest有这种可能吗?在
注意:如果您的设备依赖于另一个带有参数的设备,则此解决方案不起作用
不知道pytest包中是否有默认的解决方案,但您可以定制一个:
所以我们定义了pytest_generate_tests元函数。这个函数
如果标记为on-它将采用变量名“foo,bar”和functionfoobar,这些变量将在生成时执行
在@pytest.mark.multifixture(“foo,bar”,foobar)
现在可以使用pytest-cases执行此操作:
更多细节请参见documentation(顺便说一下,我是作者)
相关问题 更多 >
编程相关推荐