Last active
December 4, 2015 05:08
-
-
Save ketaro/9698875 to your computer and use it in GitHub Desktop.
Writing Tests with Flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == '__main__': | |
import os, sys | |
basedir = os.path.abspath(os.path.dirname(__file__) + '/../') | |
sys.path.insert(0, basedir) | |
from app import app, db | |
from hakaru_test import MyAppTest | |
from pyquery import PyQuery as pq | |
from app.model import * | |
# | |
# Functional tests for Flask routes | |
class MainSiteTest(MyAppTest): | |
def test_hakaru_index(self): | |
rv = self.app.get('/') | |
# Make sure we got an OK response | |
self.assertEqual(rv.status_code, 200) | |
# Test the Singup Page | |
def test_signup(self): | |
rv = self.app.get('/signup') | |
# Make sure we got an OK response | |
self.assertEqual(rv.status_code, 200) | |
data = {} | |
# Posting empty data to signup for should generate alerts | |
rv = self.app.post('/signup', data=data) | |
self.assertEqual(rv.status_code, 200) | |
q = pq(rv.data) | |
alerttext = q('.alert').text() | |
assert 'First Name' in alerttext | |
assert 'Last Name' in alerttext | |
assert 'Organization Name' in alerttext | |
assert 'Invitation Code' in alerttext | |
assert 'E-Mail Address' in alerttext | |
assert 'Password' in alerttext | |
assert 'Accept TOS' in alerttext | |
# Now submitting with "real" data should not produce errors | |
# and should result in new database entries | |
data = { | |
'invite_code': 'AskMeAboutGofers', | |
'email': 'testuser@test.com', | |
'orgname': 'My Test Organization', | |
'givenname': 'Test', | |
'surname': 'User', | |
'password': 'asdf1234', | |
'confirm': 'asdf1234', | |
'accept_tos': 'y' | |
} | |
rv = self.app.post('/signup', data=data) | |
self.assertEqual(rv.status_code, 302) # On success we redirect | |
# Check that database entries were created | |
u = User.query.filter_by(email=data['email']).all() | |
self.assertEqual(len(u), 1, 'Incorrect number of users created') | |
self.assertEqual(u.givenname, data['givenname']) | |
o = Organization.query.filter_by(name='My Test Organization').all() | |
self.assertEqual(len(o), 1, 'Incorrect number of organizations created') | |
ou = OrganizationUsers.query.filter_by(organization_id=o[0].id, user_id=u[0].id, role='owner').all() | |
self.assertEqual(len(ou), 1, 'Incorrect number of organization user records created') | |
if __name__ == '__main__': | |
try: | |
unittest.main() | |
except: | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
basedir = os.path.abspath(os.path.dirname(__file__)) | |
import unittest | |
from app import app, db | |
# My custom base class. All tests that inherit from this class will have | |
# a database setup for them before the tests begin | |
class MyAppTest(unittest.TestCase): | |
@classmethod | |
def setUpClass(self): | |
app.config['TESTING'] = True | |
app.config['WTF_CSRF_ENABLED'] = False | |
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('TEST_DATABASE_URI', 'sqlite:///' + os.path.join(basedir, 'hakaru_test.db')) | |
db.drop_all() | |
db.create_all() | |
@classmethod | |
def tearDownClass(self): | |
db.session.remove() | |
# Normally we'd drop all the tables when we're done our tests, but I like to leave them there | |
# so I can see what the tests did | |
# db.drop_all() | |
def setUp(self): | |
self.app = app.test_client() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == '__main__': | |
import os, sys | |
basedir = os.path.abspath(os.path.dirname(__file__) + '/../') | |
sys.path.insert(0, basedir) | |
import unittest | |
import datetime | |
from app import app, db | |
from app.helpers import * | |
# This class contains actual unit tests | |
class MyAppUnitTest(unittest.TestCase): | |
# Example test | |
def test_example(self): | |
self.failUnless( 1+1 == 2, 'one plus one fails!') | |
def test_datetime_to_epoch(self): | |
dt = datetime.datetime(2013, 8, 8, 8, 0, 0) | |
self.assertEqual( datetime_to_epoch(dt, 'America/New_York'), 1375963200.0) | |
def test_epoch_to_datetime(self): | |
dt = epoch_to_datetime(1375963200, 'America/New_York') | |
self.assertEqual(dt.year, 2013) | |
self.assertEqual(dt.month, 8) | |
self.assertEqual(dt.day, 8) | |
self.assertEqual(dt.hour, 8) | |
self.assertEqual(dt.minute, 0) | |
def test_select_choices_dict(self): | |
menu = [ | |
('pending', 'Pending'), | |
('open', 'Open'), | |
('closed', 'Closed'), | |
('completed', 'Completed') | |
] | |
d = select_choices_dict(menu) | |
self.assertEqual(d['pending'], 'Pending') | |
self.assertEqual(d['open'], 'Open') | |
self.assertEqual(d['closed'], 'Closed') | |
self.assertEqual(d['completed'], 'Completed') | |
if __name__ == '__main__': | |
try: | |
unittest.main() | |
except: | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
# Import your test classes | |
from tests.mainsite_tests import MainSiteTest | |
from tests.site_tests import ManagerSiteTest | |
from tests.myapp_unittests import MyAppUnitTest | |
# For the Code Coverage Report | |
from coverage import coverage | |
cov = coverage(branch = True, omit = ['env/*', 'run_tests.py', 'tests/*']) | |
cov.start() | |
def suite(): | |
suite = unittest.TestSuite() | |
suite.addTest(unittest.makeSuite(MyAppUnitTest)) | |
suite.addTest(unittest.makeSuite(MainSiteTest)) | |
suite.addTest(unittest.makeSuite(ManagerSiteTest)) | |
return suite | |
if __name__ == '__main__': | |
runner = unittest.TextTestRunner() | |
test_suite = suite() | |
try: | |
runner.run(test_suite) | |
except: | |
pass | |
cov.stop() | |
print "\n\nCoverage Report:\n" | |
cov.report() | |
cov.erase() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment