我的功能需要在测试套件中使用固定装置。这只是一个小的帮助函数,可以帮助生成完整的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_url在gen_url调用时未定义。如果添加api_url为第二个参数,则需要将其作为第二个参数传递。那不是我想做的。
是否可以添加api_url第二个参数gen_url而不需要从测试中传递它?为什么不能像api_key我的test_*函数那样使用它?