如何在Travis CI上配置Django以使用PostgreSQL运行测试?
我现在搞不清楚怎么把我的Django项目和Travis CI结合起来,以便进行测试。目前我在本地机器上已经设置好了PostgreSQL,当我运行单元测试时可以正常使用。
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告诉我:
$ 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:
现在我有一个比较麻烦的设置,用来在本地数据库和我的Heroku数据库之间切换:
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)}
有没有人能给我一个好的解决办法?看起来我需要在Travis上创建一个PostgreSQL数据库,然后运行我的测试,这样才能获取覆盖率。而且,每当代码被提交时,调试模式也必须设置为False。
如果你能提供一个可以工作的Travis、Django和PostgreSQL的设置,那就太好了!
1 个回答
2
我做的事情,并且对我来说很成功,就是把DATABASE_URL设置为一个环境变量,然后在代码中使用
DATABASES = {'default': dj_database_url.config(default=DATABASE_URL)}
,这样就可以很顺利地在本地和生产环境之间切换。
这里有一个使用Postgres、Django并部署在Heroku上的travis配置示例。
https://github.com/kevgathuku/sermonbuddy/blob/master/.travis.yml