我有一个需要在我的测试套件中使用灯具的功能。这只是一个帮助生成完整URL的小帮助函数。在PyTest中将夹具传递给辅助函数?
def gen_url(endpoint):
return "{}/{}".format(api_url, endpoint)
我在conftest.py
夹具返回的网址:
@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
最后,在我的测试功能,我需要调用我的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...
当我这样做但是,它会抛出一个错误,说明调用gen_url
时api_url
未定义。如果我添加api_url
作为第二个参数,我需要将它作为第二个参数传递。这不是我想要做的。
我可以将api_url
作为第二个参数添加到gen_url
而不需要从测试中传递它吗?为什么我不能像api_key
那样在我的test_*
函数中使用它?
为什么你不想把'api_url'传递给'gen_url'? –