我正在测试我的Flask应用程序 .
In summary, here is my question(s):
1)我在下面的代码块中的最终url断言失败 . 根据斜纹,它失败了,因为实际的网址是'/ auth / login' . 换句话说,用户未被重定向到受保护的管理页面 . 为什么?
2)如何在我的url断言中包含“/?next ='admin'”请求参数?换句话说,有没有办法 - 通过斜纹或任何Pythonic方法 - 来测试正确解析的'下一个'参数?
3)在功能测试中,除了断言301状态代码之外,还有其他方法可以显示重定向吗?
Here's the Twill related part of my script...
t.browser.go(t.url("/auth/login/?next=%2Fadmin%2F"))
url("/auth/login/?next=%2Fadmin%2F")
这是结果回溯:
TwillAssertionError: current url is 'http://127.0.0.1:5000/auth/login/?next=%2Fadmin%2F';
does not match '/auth/login/?next=%2Fadmin%2F'
注意:奇怪的是,在'go'命令之后断言200状态代码不会返回任何类型的错误 . 我想表明,在登录上述URL后,最终的URL确实是管理页面 . 像这样......
The test I'd like to run without any fails, but can't...
def test_redirect_via_url(self):
with Twill(self.app, port=8080) as t:
#: go to admin page before logging in...
t.browser.go(t.url("/admin"))
#: assert admin url redirected to login url
url('/auth/login')
#: The above assertion works, but...
#: why can't I assert this URL..a 'next' argument...?
#: "/auth/login/?next=%2Fadmin%2F"
#: that's what actually appears in the browser's address bar
#: In any regard, at the '/auth/login' url, let's login
fv("1", "username", "test")
fv("1", "password", "welcome1")
submit()
#: Now I want to show my app actually redirects to the protected admin page...
#: which should have a URL of localhost/admin
url('/admin')
#: The above url assertion fails. The actual url after submit is still '/auth/login'
1 回答
原因是
twill.commands.url
断言它传递的URL与浏览器的URL匹配,方法是将其转换为正则表达式并与之匹配 . 这导致/auth/login/?next=%2Fadmin%2F
被翻译为:这意味着它将匹配
/auth/login/next=%2Fadmin%2F
或/auth/loginnext=%2Fadmin%2F
但不匹配/auth/login/?next=%2Fadmin%2F
. 解决方法是逃避问号(url(r"/auth/login/\?next=%2Fadmin%2F")
应该工作) .