无法从 pytest.fixture 获取数据,而是获取数据位置

unable to get the data from pytest.fixture instead getting the data location

我正在测试pytest。 这是我的代码

file1.py

def pytest_addoption(parser):
     parser.addoption('--uid')

@pytest.fixture
def login_id(request,pytestconfig):
    user_id = pytestconfig.getoption('--uid')

    return user_id

它所做的是在 pytest

中获取 cli 参数

file2.py

@pytest.fixture 
def func(): 
      login(login_id)
      return session()

当我在一个需要创建会话的模块上执行测试时 login_id 给我的数据是

< function login_id at 0x00000000092F79D8>

我无法获取此数据,如何获取我在 cli 参数中传递的值。

我正在使用 pycharm、python3.5 .

cli 参数为 --uid username

我没有在 login 函数中获取用户名,而是获取数据的位置。

感谢和问候

如果您想在另一个灯具中使用灯具的值,只需将其作为参数传递即可:

@pytest.fixture
def login_id(request):
    ...
    return ...

@pytest.fixture 
def func(login_id): 
      login(login_id)
      return session()

搁置建议:您不需要 pytestconfig 夹具,因为您已经在使用 request。您可以通过 login_id 夹具中的 request.config 获取配置:

@pytest.fixture
def login_id(request):
    user_id = request.config.getoption('--uid')
    return user_id