Tornado. Django-like testrunner and test database

I like django unit tests because they run the test database and run it.

What are the ways to create a test database for a tornado?

UPD: I am interested in creating a postgresql test database in a test run.

+5
source share
1 answer

I found the easiest way - just use the SQL dump for the test database. Create a database, fill it with fixtures and write to a file. Just call load_fixturesbefore you run the tests (or when you want to reset the database). This method can certainly be improved, but it is good enough for my needs.

import os
import unittest2

import tornado.database

settings = dict(
    db_host="127.0.0.1:3306",
    db_name="testdb",
    db_user="testdb",
    db_password="secret",
    db_fixtures_file=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fixtures.sql'),
)

def load_fixtures():
    """Fixtures are stored in an SQL dump.
    """
    os.system("psql %s --user=%s --password=%s < %s" % (settings['db_name'], 
        settings['db_user'], settings['db_password'], settings['db_fixtures_file']))

    return tornado.database.Connection(
        host=settings['db_host'], database=settings['db_name'],
        user=settings['db_user'], password=settings['db_password'])
+5
source

All Articles