Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pytest API测试中传递用户令牌_Python_Pytest - Fatal编程技术网

Python 如何在pytest API测试中传递用户令牌

Python 如何在pytest API测试中传递用户令牌,python,pytest,Python,Pytest,我正在尝试设计pytest自动化套件来实现RESTAPI 在conftest.py中,我有: def pytest_addoption(parser): parser.addoption("--env", action="store", help="accepting dev,qa,stage") def pytest_configure(config): global base_path,execution_path,users,user_alpha,user_beta,us

我正在尝试设计pytest自动化套件来实现RESTAPI

conftest.py中,我有:

def pytest_addoption(parser):
    parser.addoption("--env", action="store", help="accepting dev,qa,stage")

def pytest_configure(config):
    global base_path,execution_path,users,user_alpha,user_beta,user_gamma,psw_alpha,psw_beta,psw_gamma

    cfg=ConfigParser()
    cfg.read('config.ini') 

    base_path = cfg.get('base', 'path')

    if config.getoption('env') in ['qa','stage','dev']:
        environ = config.getoption('env')
        execution_path = base_path.format(env=environ)
    else:
        sys.exit(1)

@pytest.fixture(scope='session')
def base():
    global execution_path
    return execution_path

def get_user():
    global execution_path,users

    cfg=ConfigParser()
    cfg.read('config.ini')

    user1 = cfg.get('users','user1')
    psw_user1 = cfg.get('users','psw.user1')

    user2 = cfg.get('users','user.user2')
    psw_user2 = cfg.get('users','psw.user2')

    user3 = cfg.get('users','user.user3')
    psw_user3 = cfg.get('users','psw.user3')

    users = [(user1, psw_user1,user1_token), (user2, psw_user2,user2_token), (user3, psw_user3,user3_token)]

    return users

@pytest.fixture(params=get_user(),scope='session', ids=['user1','user2','user3'])
def return_user(request):
    return request.param
在测试模块py中:

def test_function1(base, return_user):
    pass

def test_function2(base, return_user):
    pass
输出

test_upload.py::test_pass4[user1] PASSED                                [ 16%]
test_upload.py::test_pass5[user1] PASSED                                [ 33%]
test_upload.py::test_pass4[user2] PASSED                                [ 50%]
test_upload.py::test_pass5[user2] PASSED                                [ 66%]
test_upload.py::test_pass4[user3] PASSED                                [ 83%]
test_upload.py::test_pass5[user3] PASSED                                [100%]
可以使用pytest_configure(metafunc)创建令牌。但是,通过pytest_configure,pytest为每个用户运行test_pass4,然后为每个用户开始test_pass5,从而为每个测试生成一个新令牌。我希望在同一模块中为同一用户的所有测试使用相同的令牌

要获取令牌,我需要传递命令行参数,以便可以使用在命令行中传递的env变量格式化执行路径。我可以通过def get_user(config)访问env来实现这一点。但是@pytest.fixture(params=get_user(config))无法识别config。或者我没有其他参数要传入get_user()

有没有办法做到这一点

更新:我能够使用
pytest.config.getoption('env'))访问设备内部的命令行参数
,然后在设备内部执行必要的转换

@pytest.fixture(params=get_user(),scope='session', ids=['user1','user2','user3'])
def return_user(request):
    cfg=ConfigParser()
    cfg.read('config.ini') 

    base_path = cfg.get('base', 'path')
    base_path = base_path.format(env=request.config.getoption('env'))

    user = #payload to login user  
    user.token = #code to get token

    temp = request.param
    temp.append(user.token)
    return temp