user3704512
user3704512

Reputation: 151

How to set up Django to run tests on PostgreSQL on Travis CI?

I cannot seem to figure out how to integrate my current Django project to run tests on Travis CI. Right now I have PostgreSQL set up to run on my local machine when unit tests are run.

language: python
python:
  - 3.4.1
addons:
  postgresql: "9.3"
before_script:
- psql -U postgres -c "create extension postgis"
  - psql -c 'create database travis_ci_test;' -U postgres
install:
  - pip install -r requirements.txt
  - pip install coveralls
script:
  coverage run --source=calculator manage.py test
after_success:
  coveralls

Travis tells me:

$ coverage run --source=calculator manage.py test
Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database
Type 'yes' if you would like to try deleting the test database 'test_dk5va592r6j0v', or 'no' to cancel: 

And right now I have a hacky set-up to deal with switching between a local db and my heroku db:

import dj_database_url

if DEBUG:
    DATABASE_URL = 'postgres://localhost/storage'
else:
    DATABASE_URL = 'postgres://somerealurl'
DATABASES = {'default': dj_database_url.config(default=DATABASE_URL)}

Does anyone have a good way to fix my issue? It seems I need to be able to create a PostgreSQL on Travis, and then run my tests so I can get coverage. Debug will also have to be set to False, whenever the code is checked in as well.

If you could post a working Travis, DJango, and PSQL setup that would awesome!

Upvotes: 2

Views: 1497

Answers (1)

kevgathuku
kevgathuku

Reputation: 348

What I have done and has been successful for me is setting the DATABASE_URL as an environment variable and just using
DATABASES = {'default': dj_database_url.config(default=DATABASE_URL)} in the code which will switch gracefully from local to production.

Here is a working travis config using Postgres, Django and deployed on Heroku.

https://github.com/kevgathuku/sermonbuddy/blob/master/.travis.yml

Upvotes: 2

Related Questions