Skip to content

Commit 22de690

Browse files
committed
flask testing
1 parent 5c4b91d commit 22de690

File tree

8 files changed

+76
-36
lines changed

8 files changed

+76
-36
lines changed

manage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@manager.command
2020
def test():
2121
"""Runs the unit tests without coverage."""
22-
tests = unittest.TestLoader().discover('.')
22+
tests = unittest.TestLoader().discover('tests')
2323
unittest.TextTestRunner(verbosity=2).run(tests)
2424

2525

@@ -28,7 +28,7 @@ def cov():
2828
"""Runs the unit tests with coverage."""
2929
cov = coverage.coverage(branch=True, include='project/*')
3030
cov.start()
31-
tests = unittest.TestLoader().discover('.')
31+
tests = unittest.TestLoader().discover('tests')
3232
unittest.TextTestRunner(verbosity=2).run(tests)
3333
cov.stop()
3434
cov.save()

project/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ def get_id(self):
5151
return unicode(self.id)
5252

5353
def __repr__(self):
54-
return '<name {}'.format(self.name)
54+
return '<name - {}>'.format(self.name)

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Please help us keep this free, open source project going. Purchase the [Real Pyt
4848
| 25 | [User Registration (functionality and unit tests)](http://youtu.be/kt4PEa5tsVw) | [part25](https://github.com/realpython/discover-flask/tree/part25) |
4949
| 26 | [Finalize Messaging System](http://youtu.be/WnT188ePHg4) | [part26](https://github.com/realpython/discover-flask/tree/part26) |
5050
| 27 | [Test Coverage with coverage.py](http://youtu.be/7Aqcn0-uAr0) | [part27](https://github.com/realpython/discover-flask/tree/part27) |
51+
| 28 | Flask Testing | Coming Soon! |
5152

5253
**You can view the entire video playlist [here](http://www.youtube.com/watch?v=WfpFUmV1d0w&list=PLLjmbh6XPGK4ISY747FUHXEl9lBxre4mM&feature=share).**
5354

tests/__init__.py

Whitespace-only changes.

tests/base.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from flask.ext.testing import TestCase
2+
3+
from project import app, db
4+
from project.models import User, BlogPost
5+
6+
7+
class BaseTestCase(TestCase):
8+
"""A base test case."""
9+
10+
def create_app(self):
11+
app.config.from_object('config.TestConfig')
12+
return app
13+
14+
def setUp(self):
15+
db.create_all()
16+
db.session.add(User("admin", "ad@min.com", "admin"))
17+
db.session.add(
18+
BlogPost("Test post", "This is a test. Only a test.", "admin"))
19+
db.session.commit()
20+
21+
def tearDown(self):
22+
db.session.remove()
23+
db.drop_all()

tests/test_forms.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# tests/test_forms.py

tests.py renamed to tests/test_functional.py

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1+
# tests/test_functional.py
2+
13
import unittest
24

3-
from flask.ext.testing import TestCase
45
from flask.ext.login import current_user
56

6-
from project import app, db
7-
from project.models import User, BlogPost
8-
9-
10-
class BaseTestCase(TestCase):
11-
"""A base test case."""
12-
13-
def create_app(self):
14-
app.config.from_object('config.TestConfig')
15-
return app
16-
17-
def setUp(self):
18-
db.create_all()
19-
db.session.add(User("admin", "ad@min.com", "admin"))
20-
db.session.add(
21-
BlogPost("Test post", "This is a test. Only a test.", "admin"))
22-
db.session.commit()
23-
24-
def tearDown(self):
25-
db.session.remove()
26-
db.drop_all()
7+
from base import BaseTestCase
278

289

2910
class FlaskTestCase(BaseTestCase):
@@ -98,17 +79,6 @@ def test_logout_route_requires_login(self):
9879
response = self.client.get('/logout', follow_redirects=True)
9980
self.assertIn(b'Please log in to access this page', response.data)
10081

101-
# Ensure user can register
102-
def test_user_registeration(self):
103-
with self.client:
104-
response = self.client.post('register/', data=dict(
105-
username='Michael', email='michael@realpython.com',
106-
password='python', confirm='python'
107-
), follow_redirects=True)
108-
self.assertIn(b'Welcome to Flask!', response.data)
109-
self.assertTrue(current_user.name == "Michael")
110-
self.assertTrue(current_user.is_active())
111-
11282

11383
if __name__ == '__main__':
11484
unittest.main()

tests/test_models.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# tests/test_models.py
2+
3+
4+
import unittest
5+
6+
from flask.ext.login import current_user
7+
8+
from base import BaseTestCase
9+
from project import bcrypt
10+
from project.models import User
11+
12+
13+
class TestUser(BaseTestCase):
14+
15+
# Ensure user can register
16+
def test_user_registeration(self):
17+
with self.client:
18+
response = self.client.post('register/', data=dict(
19+
username='Michael', email='michael@realpython.com',
20+
password='python', confirm='python'
21+
), follow_redirects=True)
22+
self.assertIn(b'Welcome to Flask!', response.data)
23+
self.assertTrue(current_user.name == "Michael")
24+
self.assertTrue(current_user.is_active())
25+
user = User.query.filter_by(email='michael@realpython.com').first()
26+
self.assertTrue(str(user) == '<name - Michael>')
27+
28+
def test_get_by_id(self):
29+
# Ensure id is correct for the current/logged in user
30+
with self.client:
31+
self.client.post('/login', data=dict(
32+
username="admin", password='admin'
33+
), follow_redirects=True)
34+
self.assertTrue(current_user.id == 1)
35+
self.assertFalse(current_user.id == 20)
36+
37+
def test_check_password(self):
38+
# Ensure given password is correct after unhashing
39+
user = User.query.filter_by(email='ad@min.com').first()
40+
self.assertTrue(bcrypt.check_password_hash(user.password, 'admin'))
41+
self.assertFalse(bcrypt.check_password_hash(user.password, 'foobar'))
42+
43+
44+
if __name__ == '__main__':
45+
unittest.main()

0 commit comments

Comments
 (0)