Flask Snippets

Flask TestCase

By Dan Jacob filed in Utilities

This is a subclass of unittest.TestCase to help manage your unit tests in Flask projects.

It sets up the application and client and exposes the request context so you can do app-specific things outside of your views.

import unittest

class TestCase(unittest.TestCase):

    def create_app(self):
        Create your Flask app here, with any
        configuration you need
        raise NotImplementedError

    def __call__(self, result=None):
       Does the required setup, doing it here
       means you don't have to call super.setUp
       in subclasses.
       super(TestCase, self).__call__(result)

    def _pre_setup(self):
       self.app = self.create_app()
       self.client = self.app.test_client()
       # now you can use flask thread locals

       self._ctx = self.app.test_request_context()

    def _post_tearDown(self):

A further step would be to add convenience methods to this TestCase - for example assertRedirects or assert404:

    def assert404(self, response):
        Checks if a HTTP 404 returned
        resp = self.client.get("/")
        self.assertTrue(response.status_code == 404)

If you need to handle fixtures with SQLAlchemy or another ORM/backend then the Fixture package may be of use:


This snippet by Dan Jacob can be used freely for anything you like. Consider it public domain.