按照Flask入门教程第9章:测试,运行单元测试后,首先出现问题1如下:
`RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.`
问题1可以按照error message里的app.app_context()
来解决。
但随后出现问题2:15个测试中有1个无法通过
AssertionError: 'Logout' unexpectedly found in ...
我们知道Logout
只在模板base.html中被定义,而且显示的条件是current_user.is_authenticated
。报错的是这一段测试:
def test_login_protect(self):
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertNotIn('Logout', data)
self.assertNotIn('Settings', data)
self.assertNotIn('<form method="post">', data)
self.assertNotIn('Delete', data)
self.assertNotIn('Edit', data)
我按照自己理解的逻辑排查过,之后也直接下载了这个commit:66dc487的相关代码进行验证,但依旧得到同样的错误。