我正在尝试向我的基本应用程序添加测试。访问所有内容都需要登录。
这是我的测试用例类:
class MyAppTestCase(FlaskTestCaseMixin):
def _create_app(self):
raise NotImplementedError
def _create_fixtures(self):
self.user = EmployeeFactory()
def setUp(self):
super(MyAppTestCase, self).setUp()
self.app = self._create_app()
self.client = self.app.test_client()
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
self._create_fixtures()
self._create_csrf_token()
def tearDown(self):
super(MyAppTestCase, self).tearDown()
db.drop_all()
self.app_context.pop()
def _post(self, route, data=None, content_type=None, follow_redirects=True, headers=None):
content_type = content_type or 'application/x-www-form-urlencoded'
return self.client.post(route, data=data, follow_redirects=follow_redirects, content_type=content_type, headers=headers)
def _login(self, email=None, password=None):
email = email or self.user.email
password = password or 'password'
data = {
'email': email,
'password': password,
'remember': 'y'
}
return self._post('/login', data=data)
class MyFrontendTestCase(MyAppTestCase):
def _create_app(self):
return create_app(settings)
def setUp(self):
super(MyFrontendTestCase, self).setUp()
self._login()
我正在运行我的测试,在终端中使用鼻子测试,如下所示:source my_env/bin/activate && nosetests --exe
像这样的基本测试失败:
class CoolTestCase(MyFrontendTestCase):
def test_logged_in(self):
r = self._login()
self.assertIn('MyAppName', r.data)
def test_authenticated_access(self):
r = self.get('/myroute/')
self.assertIn('MyAppName', r.data)
从输出中,我看到这r.data只是登录页面的 HTML,没有错误(例如,错误的用户名或密码)或警报(“请登录以访问此页面”)。
我在此setUp过程中正在登录,因此test_authenticated_access 应该让我访问/myroute/或重定向到登录页面,并显示闪烁的消息“请登录以访问此页面”。但它没有。
我不知道出了什么问题。我的测试基于我在 Flask 文档和这个应用程序样板中找到的那些