flask-tutorial icon indicating copy to clipboard operation
flask-tutorial copied to clipboard

第9章测试章节代码没有按预期执行

Open lsasln opened this issue 2 years ago • 0 comments

章节:第9章 位置:test_watchlist.py:测试认证相关功能

class WatchlistTestCase(unittest.TestCase):
    # ...
    # 测试登录保护
    def test_login_protect(self):
        response = self.client.get('/')
        data = response.get_data(as_text=True)
        self.assertNotIn('Logout', data)
......

这个测试用例不会通过,原因猜测可能是存在登录的缓存,我看了返回的html是登录过后的。于是我在前面加了个logout的方法,并且在这段代码前使用,即可通过测试:

    def logout(self):
        self.client.get('/logout')
......
    def test_login_protect(self):
        self.logout()
        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)

lsasln avatar Aug 03 '23 08:08 lsasln