cra
mr

Using Travis-CI with Python and Django

You're viewing an archived post which may have broken links or images. If this post was valuable and you'd like me to restore it, let me know!

I’ve been using Travis-CI for a while now. Both my personal projects, and even several of the libraries we maintain at DISQUS rely on it for Continuous Integration. I figured it was about time to confess my undenying love for Travis, and throw up some notes about the defaults we use in our projects.

Getting started with Travis-CI is pretty easy. It involves putting a .travis.yml file in the root of your project, and configuring the hooks between GitHub and Travis. While it’s not always easy to get the hooks configured when you’re using organizations, I’m not going to talk much about that. What I do want to share is how we’ve structured our configuration files for our Django and Python projects.

A basic .travis.yml might look something like this:

language: python
python:
  - "2.6"
  - "2.7"
install:
  - pip install -q -e . --use-mirrors
script:
  - python setup.py test

Most of the projects themselves use Django, which also means they need to test several Django versions. Travis makes this very simple with its matrix builds. In our case, we need to setup a DJANGO matrix, and ensure it gets installed:

env:
  - DJANGO=1.2.7
  - DJANGO=1.3.1
  - DJANGO=1.4
install:
  - pip install -q Django==$DJANGO --use-mirrors
  - pip install -q -e . --use-mirrors

Additionally we generally conform to pep8, and we always want to run pyflakes against our build. We also use a custom version of pyflakes which allows us to filter out warnings, as those are never critical errors. Add this in is pretty simple using the before_script hook, which gets run before the tests are run in script.

install:
  - pip install -q Django==$DJANGO --use-mirrors
  - pip install pep8 --use-mirrors
  - pip install https://github.com/dcramer/pyflakes/tarball/master
  - pip install -q -e . --use-mirrors
before_script:
  - "pep8 --exclude=migrations --ignore=E501,E225 src"
  - pyflakes -x W src

When all is said and done, we end up with something like this:

language: python
python:
  - "2.6"
  - "2.7"
env:
  - DJANGO=1.2.7
  - DJANGO=1.3.1
  - DJANGO=1.4
install:
  - pip install -q Django==$DJANGO --use-mirrors
  - pip install pep8 --use-mirrors
  - pip install https://github.com/dcramer/pyflakes/tarball/master
  - pip install -q -e . --use-mirrors
before_script:
  - "pep8 --exclude=migrations --ignore=E501,E225 src"
  - pyflakes -x W src
script:
  - python setup.py test

Travis will automatically matrix each environment variable with each Python version, so you’ll get a test run for every combination of the two. Pretty easy, right?