Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 67 lines (53 sloc) 1.994 kb
c33675f @mitsuhiko Added mini blogging application as Flask example.
authored
1 # -*- coding: utf-8 -*-
2 """
3 Flaskr Tests
4 ~~~~~~~~~~~~
5
6 Tests the Flaskr application.
7
8 :copyright: (c) 2010 by Armin Ronacher.
9 :license: BSD, see LICENSE for more details.
10 """
11 import flaskr
12 import unittest
13 import tempfile
14
15
16 class FlaskrTestCase(unittest.TestCase):
17
18 def setUp(self):
19 """Before each test, set up a blank database"""
20 self.db = tempfile.NamedTemporaryFile()
21 self.app = flaskr.app.test_client()
22 flaskr.DATABASE = self.db.name
23 flaskr.init_db()
24
25 def login(self, username, password):
26 return self.app.post('/login', data=dict(
27 username=username,
28 password=password
29 ), follow_redirects=True)
30
31 def logout(self):
32 return self.app.get('/logout', follow_redirects=True)
33
34 # testing functions
35
6dd92ae @mitsuhiko Beefed up the tutorial
authored
36 def test_empty_db(self):
37 """Start with a blank database."""
38 rv = self.app.get('/')
39 assert 'No entries here so far' in rv.data
40
c33675f @mitsuhiko Added mini blogging application as Flask example.
authored
41 def test_login_logout(self):
42 """Make sure login and logout works"""
43 rv = self.login(flaskr.USERNAME, flaskr.PASSWORD)
44 assert 'You were logged in' in rv.data
45 rv = self.logout()
46 assert 'You were logged out' in rv.data
47 rv = self.login(flaskr.USERNAME + 'x', flaskr.PASSWORD)
48 assert 'Invalid username' in rv.data
49 rv = self.login(flaskr.USERNAME, flaskr.PASSWORD + 'x')
50 assert 'Invalid password' in rv.data
51
52 def test_messages(self):
53 """Test that messages work"""
54 self.login(flaskr.USERNAME, flaskr.PASSWORD)
55 rv = self.app.post('/add', data=dict(
56 title='<Hello>',
57 text='<strong>HTML</strong> allowed here'
58 ), follow_redirects=True)
59 assert 'No entries here so far' not in rv.data
60 self.login(flaskr.USERNAME, flaskr.PASSWORD)
61 assert '&lt;Hello&gt' in rv.data
62 assert '<strong>HTML</strong> allowed here' in rv.data
63
64
65 if __name__ == '__main__':
66 unittest.main()
Something went wrong with that request. Please try again.