简体   繁体   English

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

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

i am testing in pytest. 我正在pytest中测试。 here is my code 这是我的代码

file1.py file1.py

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

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

    return user_id

what it does is take the cli argument in pytest 它所做的是在pytest中使用cli参数

file2.py file2.py

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

when i am performing the test on a module which require the session to be created login_id is giving me data as 当我在需要创建会话的模块上执行测试时,login_id给了我如下数据

< function login_id at 0x00000000092F79D8> <函数login_id位于0x00000000092F79D8>

i am unable to get this data , how to get the value which i am passing in cli argument. 我无法获取此数据,如何获取cli参数中传递的值。

i am using pycharm, python3.5 . 我正在使用pycharm,python3.5。

cli parameter as --uid username cli参数为--uid username

i am not getting the username in login fucntion but instead getting the location of data. 我没有在login功能中获取用户名,而是获取数据的位置。

thanks and regards 谢谢并恭祝安康

If you want to use a fixture's value in another fixture, just pass it as a parameter: 如果要在另一个灯具中使用一个灯具的值,只需将其作为参数传递:

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

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

Suggestion aside: you don't need the pytestconfig fixture since you are already using request . 除了建议:您不需要pytestconfig固定装置,因为您已经在使用request You can get the config via request.config in the login_id fixture: 您可以在login_id固定装置中通过request.config获取配置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM