我正在为我的应用程序进行一些功能测试。根据登录用户的权限,侧边栏会有不同的链接。我正在参数化它们(硬编码)并运行一个运行良好的测试(应用程序是一个 webtest 应用程序):
endpoints = [
'/',
'/endpoint1',
'endpoint2',
...
]
@pytest.mark.parametrize('endpoint', endpoints)
def test_endpoints(endpoint, app):
res = app.get(endpoint).maybe_follow()
assert res.status_code == 200
我想避免为每种类型的用户硬编码链接列表。在一个夹具中,我实际上可以通过编程方式获取它们,所以理想情况下,我想参数化这个夹具的返回值,以便运行上面的测试函数:
@pytest.fixture
def endpoints(app):
res = app.get('/login').follow()
sidebar_links = []
for link in res.html.ul.find_all('a'):
if link.has_attr('href') and not link['href'].startswith('#'):
sidebar_links.append(link['href'])
return sidebar_links
这可能吗?