Search code examples
pythonpostgresqlflasksqlalchemyflask-testing

How to create test database with Flask-Testing


I want to set up test database for Flask-Testing class, so tests do not affect my main database. I need test database being created each time I run my unittests testcase How can I do this?

I use Flask-Testing, Flask-SQLAlchemy, psycopg2

Privilegies to create database properly granted. So now I just have to figure out how perform this.

Please advice how can I do this?


Solution

  • Use different setting for production and testing purpose.

    How to create flask app this way - http://flask.pocoo.org/docs/0.10/patterns/appfactories/#app-factories

    UPD

    Create base_setting.py for base settings, production_setting.py and testing_setting.py with diffrerent db settings and create app from one of them. - http://flask.pocoo.org/docs/0.10/config/#configuring-from-files

    for example:

    app = Flask(__name__)
    app.config.from_pyfile('base')
    app.config.from_envvar('YOURAPPLICATION_SETTINGS')
    

    Don't forget to set YOURAPPLICATION_SETTINGS env