You don't create a new Flask
object in your test cases. You import your existing app
instead.
In many project setups you already added all your extensions to that app
. In many of mine I have a factory method that'll take a configuration object and returns the fully initialized app
object for me; I use this to create a base test case:
import unittest
import project
class Config(object):
DEBUG = False
TESTING = True
CACHE_NO_NULL_WARNING = True # silence Flask-Cache warning
SECRET_KEY = 'SECRET_KEY'
class ProjectCoreViewCase(unittest.TestCase):
"""Base test case for the Project core app"""
def setUp(self, **kwargs):
config = Config()
config.__dict__.update(kwargs)
app = project.create_app(config)
self.app = app.test_client()
and any tests can then use self.app
as the test client in all tests.
This is a base test case, you'd inherit from it; the setUp()
method allows for additional configuration to be set, by passing in keyword arguments to a super()
call:
class ConcreteTestCase(ProjectCoreViewCase):
def setUp(self):
super(ConcreteTestCase, self).setUp(
SQLALCHEMY_DATABASE_URI='your_test_specific_connection_uri',
)