Skip to Content
Flask Framework Cookbook - Second Edition
book

Flask Framework Cookbook - Second Edition

by Shalabh Aggarwal
July 2019
Beginner to intermediate
302 pages
9h 38m
English
Packt Publishing
Content preview from Flask Framework Cookbook - Second Edition

How to do it...

Let's write our first test case:

  1. To start with, the content of the app_tests.py test file will be as follows:
import os 
from my_app import app, db 
import unittest 
import tempfile 

The preceding code describes the imports needed for this test suite. We will use unittest for writing our tests. A tempfile instance is needed to create SQLite databases on the fly.

  1. All the test cases need to subclass from unitest.TestCase:
class CatalogTestCase(unittest.TestCase): 
 
    def setUp(self): 
        self.test_db_file = tempfile.mkstemp()[1] 
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' +            self.test_db_file 
        app.config['TESTING'] = True 
        self.app = app.test_client() 
        db.create_all() 

The preceding method is run before each test is run and creates ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Writing a Web Application with Flask

Writing a Web Application with Flask

Doug Farrell
Mastering Flask

Mastering Flask

Jack Stouffer

Publisher Resources

ISBN: 9781789951295Supplemental Content