Django Unit Tests failing on Travis builds

Our company uses a fairly complex Django app (Python 3.6.4) and I’ve written a unit test which passes locally but when pushing into GitHub it’s a no-go. An in memory SQLite DB, we’ve routed this to a SparkDB, which seem to pass, but to get back to point the UT is created (by default) for the tests. When my Travis CI build runs the same tests the tests pass but the test command fails with the following error:

File "/home/travis/virtualenv/python3.6.5/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 301, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "SCHEMA": syntax error

The command "python manage.py test --settings=myapp.dev_settings" exited with 1.

One strange thing I notice is that when the tests are run on Travis, it’s says it’s reusing an existing DB and never destroys it after the tests are run:

$ python manage.py test --settings=myapp.dev_settings

Using existing test database for alias ‘default’…

I don’t really get that, because it should be an in memory DB and when I run it locally, a new database is created each time:

Creating test database for alias 'default'... 
. . .
Destroying test database for alias 'default'... 

My dev_settings.py file has a sqlite db on the filesystem, but that is only used for running the local development server:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Travis installs all the dependencies and they match my local environment (I’m fairly sure).

All help appreciated.

Hey nice to see you again @SolarUltima,

This is probably because the django-heroku package sets its own test-runner, along with a task-runner that does extra stuff with the database if it detects a CI in the environment, as the example I’ll write out and show you below:

 if 'CI' in os.environ:
     config['TEST_RUNNER'] = 'django_heroku.HerokuDiscoverRunner'

The test-runner does the following which does not work for SQLite:

with connection.cursor() as cursor:
    cursor.execute(
        """
            DROP SCHEMA public CASCADE;
            CREATE SCHEMA public;
            GRANT ALL ON SCHEMA public TO postgres;
            GRANT ALL ON SCHEMA public TO public;
            COMMENT ON SCHEMA public IS 'standard public schema';
        """
    )

Unfortunately this check also detects non Heroku CI environments (you can see this in the env vars) and will revert to a default configuration. You can solve this problem by disabling the Heroku test-runner. So in your settings.py try:

django_heroku.settings(locals(), test_runner=False)

That should help get you going again with your Django project using Travis.

2 Likes

Oh, one more thing probably goes without saying make sure you run travis history and travis logs to see if the results are consistent.

Let me know if this solves your issue!