【发布时间】:2017-08-22 19:35:51
【问题描述】:
我正在学习使用 pyest 进行参数化测试。在关注the relevant pytest documentation之后,我想出了这个简单的例子:
import unittest
import pytest
@pytest.fixture(autouse=True, params=['foo', 'bar'])
def foo(request):
print('fixture')
print(request.param)
class Foo(unittest.TestCase):
def setUp(self):
print('unittest setUp()')
def test(self):
print('test')
这会产生以下错误:
Failed: The requested fixture has no parameter defined for the current test.
E
E Requested fixture 'foo' defined in:
E tests/fixture.py:7
第 7 行是def foo(request):。
是什么导致了这个错误,我该如何解决?
【问题讨论】:
-
我通常使用
@pytest.mark.parametrise('request', ['foo', 'bar'])。它不能回答你的问题,但它可以作为一个快速修复(其中request是参数的名称) -
@Artyer 感谢您的建议。我链接的文档没有这个,所以我仍然有兴趣了解我做错了什么。
-
我认为问题就像错误中提到的那样,您没有定义参数,正如@Artyer 在他的示例中所说,他正在命名“请求”然后传递值。在您的代码中,您将请求参数传递给 foo 函数,但由于您没有在夹具内定义,因此没有参数
-
@GenaroMorales 我的示例与docs.pytest.org/en/latest/fixture.html#parametrizing-fixtures 文档中给出的示例有何不同?
-
使用自动使用意味着类中的所有测试方法都将使用这个fixture,如果你删除了你将只在具有请求参数的方法中使用该fixture并且该方法将通过跨度>
标签: python pytest fixtures parameterized-tests