pytest:找不到固定变量

发布于 2025-02-14 00:03:55 字数 343 浏览 4 评论 0原文

我是Pytest的新手,并且已经写了一个带有固定装置的测试功能,但是我收到的错误是找不到固定变量。这是代码:

@pytest.fixture(scope="module")
def func1(arg1,arg2):
     return arg1 + arg2

def test_func1(func1):
     assert func1== 4

当我运行此文件时,我会得到错误e fixture arg1。如果我定义arg1arg2在全球范围内不通过功能传递,这将正常工作,但是如何将参数传递给固定功能。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2025-02-21 00:03:55

您需要将arg1和arg2定义为固定装置本身,然后它应该起作用。如下:

@pytest.fixture(scope="module")
def argX():
    # return 'enter definition of argX here'

You need to define arg1 and arg2 as fixtures themselves, then it should work. Do as follows:

@pytest.fixture(scope="module")
def argX():
    # return 'enter definition of argX here'
对风讲故事 2025-02-21 00:03:55

目前尚不清楚如何将论点传递给灯具。我只能猜测它们应该是您的测试功能的参数它们被传递给func1固定装置:

import pytest

@pytest.fixture()
def func1(request):
     return request.param[0] + request.param[1]

@pytest.mark.parametrize('func1', [(1, 3)], indirect=True)
def test_func1(func1):
     assert func1 == 4

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 as indirect so that they are passed to func1 fixture:

import pytest

@pytest.fixture()
def func1(request):
     return request.param[0] + request.param[1]

@pytest.mark.parametrize('func1', [(1, 3)], indirect=True)
def test_func1(func1):
     assert func1 == 4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文