5

I am using flask-testing to do some unit tests on a Postgres application. According to doc , I have following code.

from flask_testing import TestCase
from src.game.models import db
from src import create_app    
class BaseTest(TestCase):
    SQLALCHEMY_DATABASE_URI = 'postgresql://demo:demo@postgres:5432/test_db'
    TESTING = True

    def create_app(self):
        # pass in test configuration
        return create_app(self)

    def setUp(self):
        db.create_all()

    def tearDown(self):
        db.session.remove()
        db.drop_all()

Of course I got this error

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: database "test_db" does not exist

I do have a database postgresql://demo:demo@postgres:5432/demo which is my production database.

How can I create test_db in this BaseTest class? I am using Python 3.6 and latest flask and flask-sqlalchemy. Thank you very much.

Tiancheng Liu
  • 782
  • 2
  • 9
  • 22

0 Answers0