小编Con*_*tle的帖子

将夹具传递给PyTest中的辅助函数?

我的功能需要在测试套件中使用固定装置。这只是一个小的帮助函数,可以帮助生成完整的URL。

def gen_url(endpoint):
    return "{}/{}".format(api_url, endpoint)
Run Code Online (Sandbox Code Playgroud)

我有一个固定装置,conftest.py它返回URL:

@pytest.fixture(params=["http://www.example.com"])
def api_url(request):
    return request.param

@pytest.fixture(params=["MySecretKey"])
def api_key(request):
    return request.param
Run Code Online (Sandbox Code Playgroud)

最后,在我的测试函数中,我需要调用我的gen_url

def test_call_action_url(key_key):
    url = gen_url("player")
    # url should equal: 'http://www.example.com/player'
    # Do rest of test here...
Run Code Online (Sandbox Code Playgroud)

但是,当我执行此操作时,它会引发错误,指出api_urlgen_url调用时未定义。如果添加api_url为第二个参数,则需要将其作为第二个参数传递。那不是我想做的。

是否可以添加api_url第二个参数gen_url而不需要从测试中传递它?为什么不能像api_key我的test_*函数那样使用它?

python pytest

5
推荐指数
2
解决办法
1291
查看次数

标签 统计

pytest ×1

python ×1