Book Image

Learning Flask Framework

Book Image

Learning Flask Framework

Overview of this book

Table of Contents (17 chapters)
Learning Flask Framework
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Flask and unit testing


You may be thinking: "Unit tests look great for small sections of code, but how do you test it for an entire Flask app?" Well one of the ways, as mentioned previously, is to make sure that all your methods are as discrete as possible—that is, to make sure your methods do the least possible work to complete their function, and to avoid repetition between methods. If your methods are not discrete, now is a good time to get them tidied up.

Another thing that will help is that Flask comes readymade for unit testing. There is a good chance that any existing application can have at least some unit tests applied to it. Especially, any areas of API such as in unable to verify will be extremely easy to test by making use of the methods that represent the HTTP requests already within Flask. Following is a simple example:

import unittest
from flask import request
from main import app

class AppTest(unittest.TestCase):
  def setUp(self):
    self.app = app.test_client()

  def...