pytest:找不到固定变量
我是Pytest的新手,并且已经写了一个带有固定装置的测试功能,但是我收到的错误是找不到固定变量。这是代码:
@pytest.fixture(scope="module")
def func1(arg1,arg2):
return arg1 + arg2
def test_func1(func1):
assert func1== 4
当我运行此文件时,我会得到错误e fixture arg1
。如果我定义arg1
和arg2
在全球范围内不通过功能传递,这将正常工作,但是如何将参数传递给固定功能。
I am new to pytest and I have written a test function with fixture but I am receiving an error that fixture variable is not found. Here is the code:
@pytest.fixture(scope="module")
def func1(arg1,arg2):
return arg1 + arg2
def test_func1(func1):
assert func1== 4
When I run this file, I get the error E fixture arg1 not found
. If I define arg1
and arg2
globally and not pass them through function, this will work fine, but how can I pass an argument to fixture function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将arg1和arg2定义为固定装置本身,然后它应该起作用。如下:
You need to define arg1 and arg2 as fixtures themselves, then it should work. Do as follows:
目前尚不清楚如何将论点传递给灯具。我只能猜测它们应该是您的测试功能的参数它们被传递给
func1
固定装置:It's not completely clear how you want the arguments to be passed to the fixture. I can only guess that they are supposed to be parameters of your test function in which case you need to define them in
@pytest.mark.parametrize
and set them asindirect
so that they are passed tofunc1
fixture: