Python 将变量从conftest返回到测试类

Python 将变量从conftest返回到测试类,python,pytest,fixture,Python,Pytest,Fixture,我有以下脚本: conftest.py: import pytest @pytest.fixture(scope="session") def setup_env(request): # run some setup return("result") import pytest @pytest.mark.usefixtures("setup_env") class TestDirectoryInit(object): def setup(cls):

我有以下脚本:

conftest.py

import pytest
@pytest.fixture(scope="session")
def setup_env(request):
    # run some setup
    return("result")
import pytest
@pytest.mark.usefixtures("setup_env")
class TestDirectoryInit(object):   
    def setup(cls):
        print("this is setup")
        ret=setup_env()
        print(ret)

    def test1():
        print("test1")

    def teardown(cls):
        print("this teardown")
测试.py

import pytest
@pytest.fixture(scope="session")
def setup_env(request):
    # run some setup
    return("result")
import pytest
@pytest.mark.usefixtures("setup_env")
class TestDirectoryInit(object):   
    def setup(cls):
        print("this is setup")
        ret=setup_env()
        print(ret)

    def test1():
        print("test1")

    def teardown(cls):
        print("this teardown")
我得到一个错误:

    def setup(cls):
        print("this is setup")
>       ret=setup_env()
E       NameError: name 'setup_env' is not defined
setup()
中,我想从
conftest.py
中的
setup\u env()
获取返回值“result”


有专家能指导我怎么做吗?

我相信
@pytest.mark.usefixtures
更适合在执行每个测试之前进行状态更改。从文档中:

“有时测试函数不直接需要访问fixture对象。”

这意味着您的fixture在每个测试开始时都在运行,但您的函数无权访问它

当您的测试需要访问fixture返回的对象时,当将其放置在
conftest.py
中并用
@pytest.fixture
标记时,它应该已经由名称填充。然后,您只需将夹具的名称作为测试函数的参数进行delcare,如下所示:

如果希望在类或模块级别执行此操作,则需要更改
@pytest.fixture
语句的
范围,如下所示:

很抱歉有这么多的文档链接,但我认为它们有很好的例子。希望能把事情弄清楚