Permalink
Browse files

increase test coverage

  • Loading branch information...
mjhea0 committed Dec 28, 2014
1 parent 81d40ed commit 384f2bd79376a61d7b6dcaacdc2ece055cd60c42
Showing with 196 additions and 150 deletions.
  1. +5 −1 manage.py
  2. +10 −9 project/home/views.py
  3. +12 −10 project/users/views.py
  4. +2 −0 readme.md
  5. +36 −0 tests/test_basic.py
  6. +29 −0 tests/test_blog.py
  7. +0 −1 tests/test_forms.py
  8. +0 −84 tests/test_functional.py
  9. +0 −45 tests/test_models.py
  10. +102 −0 tests/test_users.py
View
@@ -26,7 +26,11 @@ def test():
@manager.command
def cov():
"""Runs the unit tests with coverage."""
cov = coverage.coverage(branch=True, include='project/*')
cov = coverage.coverage(
branch=True,
include='project/*',
omit=["*/__init__.py", '*/models.py']
)
cov.start()
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
View
@@ -2,12 +2,13 @@
#### imports ####
#################
from flask import render_template, Blueprint, request, flash, redirect, url_for
from flask.ext.login import login_required, current_user
from flask import render_template, Blueprint, \
request, flash, redirect, url_for # pragma: no cover
from flask.ext.login import login_required, current_user # pragma: no cover
from .forms import MessageForm
from project import db
from project.models import BlogPost
from .forms import MessageForm # pragma: no cover
from project import db # pragma: no cover
from project.models import BlogPost # pragma: no cover
################
#### config ####
@@ -16,16 +17,16 @@
home_blueprint = Blueprint(
'home', __name__,
template_folder='templates'
)
) # pragma: no cover
################
#### routes ####
################
# use decorators to link the function to a url
@home_blueprint.route('/', methods=['GET', 'POST'])
@login_required
@home_blueprint.route('/', methods=['GET', 'POST']) # pragma: no cover
@login_required # pragma: no cover
def home():
error = None
form = MessageForm(request.form)
@@ -45,6 +46,6 @@ def home():
'index.html', posts=posts, form=form, error=error)
@home_blueprint.route('/welcome')
@home_blueprint.route('/welcome') # pragma: no cover
def welcome():
return render_template('welcome.html') # render a template
View
@@ -3,12 +3,13 @@
#################
from flask import flash, redirect, render_template, request, \
url_for, Blueprint
from flask.ext.login import login_user, login_required, logout_user
url_for, Blueprint # pragma: no cover
from flask.ext.login import login_user, \
login_required, logout_user # pragma: no cover
from .forms import LoginForm, RegisterForm
from project import db
from project.models import User, bcrypt
from .forms import LoginForm, RegisterForm # pragma: no cover
from project import db # pragma: no cover
from project.models import User, bcrypt # pragma: no cover
################
#### config ####
@@ -17,14 +18,14 @@
users_blueprint = Blueprint(
'users', __name__,
template_folder='templates'
)
) # pragma: no cover
################
#### routes ####
################
@users_blueprint.route('/login', methods=['GET', 'POST'])
@users_blueprint.route('/login', methods=['GET', 'POST']) # pragma: no cover
def login():
error = None
form = LoginForm(request.form)
@@ -43,15 +44,16 @@ def login():
return render_template('login.html', form=form, error=error)
@users_blueprint.route('/logout')
@login_required
@users_blueprint.route('/logout') # pragma: no cover
@login_required # pragma: no cover
def logout():
logout_user()
flash('You were logged out.')
return redirect(url_for('home.welcome'))
@users_blueprint.route('/register/', methods=['GET', 'POST'])
@users_blueprint.route(
'/register/', methods=['GET', 'POST']) # pragma: no cover
def register():
form = RegisterForm()
if form.validate_on_submit():
View
@@ -49,6 +49,8 @@ Please help us keep this free, open source project going. Purchase the [Real Pyt
| 26 | [Finalize Messaging System](http://youtu.be/WnT188ePHg4) | [part26](https://github.com/realpython/discover-flask/tree/part26) |
| 27 | [Test Coverage with coverage.py](http://youtu.be/7Aqcn0-uAr0) | [part27](https://github.com/realpython/discover-flask/tree/part27) |
| 28 | [Flask Testing!](http://youtu.be/YO2k80aDJj8) | [part28](https://github.com/realpython/discover-flask/tree/part28) |
| 29 | Flask Testing (increase test coverage) | Coming Soon! |
**You can view the entire video playlist [here](http://www.youtube.com/watch?v=WfpFUmV1d0w&list=PLLjmbh6XPGK4ISY747FUHXEl9lBxre4mM&feature=share).**
View
@@ -0,0 +1,36 @@
# tests/test_basic.py
import unittest
from base import BaseTestCase
class FlaskTestCase(BaseTestCase):
# Ensure that Flask was set up correctly
def test_index(self):
response = self.client.get('/login', content_type='html/text')
self.assertEqual(response.status_code, 200)
# Ensure that main page requires user login
def test_main_route_requires_login(self):
response = self.client.get('/', follow_redirects=True)
self.assertIn(b'Please log in to access this page', response.data)
# Ensure that welcome page loads
def test_welcome_route_works_as_expected(self):
response = self.client.get('/welcome', follow_redirects=True)
self.assertIn(b'Welcome to Flask!', response.data)
# Ensure that posts show up on the main page
def test_posts_show_up_on_main_page(self):
response = self.client.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
self.assertIn(b'This is a test. Only a test.', response.data)
if __name__ == '__main__':
unittest.main()
View
@@ -0,0 +1,29 @@
# tests/test_blog.py
import unittest
from base import BaseTestCase
class BlogPostTests(BaseTestCase):
# Ensure a logged in user can add a new post
def test_user_can_post(self):
with self.client:
self.client.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
response = self.client.post(
'/',
data=dict(title="test", description="test"),
follow_redirects=True
)
self.assertEqual(response.status_code, 200)
self.assertIn(b'New entry was successfully posted. Thanks.',
response.data)
if __name__ == '__main__':
unittest.main()
View
@@ -1 +0,0 @@
# tests/test_forms.py
View
@@ -1,84 +0,0 @@
# tests/test_functional.py
import unittest
from flask.ext.login import current_user
from base import BaseTestCase
class FlaskTestCase(BaseTestCase):
# Ensure that Flask was set up correctly
def test_index(self):
response = self.client.get('/login', content_type='html/text')
self.assertEqual(response.status_code, 200)
# Ensure that main page requires user login
def test_main_route_requires_login(self):
response = self.client.get('/', follow_redirects=True)
self.assertIn(b'Please log in to access this page', response.data)
# Ensure that welcome page loads
def test_welcome_route_works_as_expected(self):
response = self.client.get('/welcome', follow_redirects=True)
self.assertIn(b'Welcome to Flask!', response.data)
# Ensure that posts show up on the main page
def test_posts_show_up_on_main_page(self):
response = self.client.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
self.assertIn(b'This is a test. Only a test.', response.data)
class UserViewsTests(BaseTestCase):
# Ensure that the login page loads correctly
def test_login_page_loads(self):
response = self.client.get('/login')
self.assertIn(b'Please login', response.data)
# Ensure login behaves correctly with correct credentials
def test_correct_login(self):
with self.client:
response = self.client.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
self.assertIn(b'You were logged in', response.data)
self.assertTrue(current_user.name == "admin")
self.assertTrue(current_user.is_active())
# Ensure login behaves correctly with incorrect credentials
def test_incorrect_login(self):
response = self.client.post(
'/login',
data=dict(username="wrong", password="wrong"),
follow_redirects=True
)
self.assertIn(b'Invalid username or password.', response.data)
# Ensure logout behaves correctly
def test_logout(self):
with self.client:
self.client.post(
'/login',
data=dict(username="admin", password="admin"),
follow_redirects=True
)
response = self.client.get('/logout', follow_redirects=True)
self.assertIn(b'You were logged out', response.data)
self.assertFalse(current_user.is_active())
# Ensure that logout page requires user login
def test_logout_route_requires_login(self):
response = self.client.get('/logout', follow_redirects=True)
self.assertIn(b'Please log in to access this page', response.data)
if __name__ == '__main__':
unittest.main()
View
@@ -1,45 +0,0 @@
# tests/test_models.py
import unittest
from flask.ext.login import current_user
from base import BaseTestCase
from project import bcrypt
from project.models import User
class TestUser(BaseTestCase):
# Ensure user can register
def test_user_registeration(self):
with self.client:
response = self.client.post('register/', data=dict(
username='Michael', email='michael@realpython.com',
password='python', confirm='python'
), follow_redirects=True)
self.assertIn(b'Welcome to Flask!', response.data)
self.assertTrue(current_user.name == "Michael")
self.assertTrue(current_user.is_active())
user = User.query.filter_by(email='michael@realpython.com').first()
self.assertTrue(str(user) == '<name - Michael>')
def test_get_by_id(self):
# Ensure id is correct for the current/logged in user
with self.client:
self.client.post('/login', data=dict(
username="admin", password='admin'
), follow_redirects=True)
self.assertTrue(current_user.id == 1)
self.assertFalse(current_user.id == 20)
def test_check_password(self):
# Ensure given password is correct after unhashing
user = User.query.filter_by(email='ad@min.com').first()
self.assertTrue(bcrypt.check_password_hash(user.password, 'admin'))
self.assertFalse(bcrypt.check_password_hash(user.password, 'foobar'))
if __name__ == '__main__':
unittest.main()
Oops, something went wrong.

0 comments on commit 384f2bd

Please sign in to comment.