This post is part of a series. This list contains all of the posts:
Travis CI is a free and highly popular continuous integration system that hooks into Github repositories. It's very simple to connect, and every time you push to Github, Travis will create a small environment, run unit tests, and indicate if it passed or failed. You can even put a badge on your readme for all to see.
Sign up with your Github account at https://travis-ci.org/.
From the left you can click on the + icon next to My Repositories to add a new Github repo:
Find the repo you want to add and check it:
Click on the repo name, and then switch into your IDE.
I prefer to use the following directory structure, where /blog is project root.
/blog
/blog
__init__.py
.travis.yml
requirements.txt
run_travis.sh
tests.py
First create a .travis.yml
file in project root and add the following basic structure to it:
language: python
python:
- "2.7"
install: pip install -r requirements.txt
script: sh run_travis.sh
Travis will run your application inside of virtualenv
, so the install
portion is required and must point to a file containing the pip packages you need installed.
The script
portion will run the command that kicks off the tests you want to run. I like to use a bash file for this so I can kick up the web server and run my system tests, which rely on the web server being up and running. If you just use pure unit tests with no dependencies on a running server, you can simply use script: nosetests
.
Depending on how advanced your program is, there are lots more you could add. For example, if you use postgresql, you should add
services:
- postgresql
If you are on Heroku or otherwise need to set environment variables:
env:
- BLOG_DATABASE=sqlite:///db.db
Check out all of the .travis.yml
options over at Travis's Python page.
Create a requirements.txt
file in project root containing all of your dependencies. Make sure to include nose
in there. For example:
flask
flask_sqlalchemy
blinker
nose
I like to create a run_travis.sh
file in project root with the following:
#!/usr/bin/env bash
python run.py > /dev/null &
nosetests --with-coverage
The &
will kick off run.py
in the background and then allow the tests to start. Again, I only need this because I use system tests which depend on the web server being live.
run.py
is simply:
from blog import app, db
if __name__ == '__main__':
db.create_all()
app.run(host='0.0.0.0', port=5000, debug=True)
After you have these files, add them with git and push to your repo. Keep your Travis page up during this time, and it will start to refresh and print out messages.
If everything is successful, you'll see this:
At the top of your Travis page, you should see an icon for your badge that says "build passing":
Click on the icon, and change the Image URL drop down to "Markdown":
Copy and paste it at the top of your README.md
file:
Commit your changes and push to GitHub. Load up your repo's page and you can now see your badge:
This post is part of a series. This list contains all of the posts:
Name: saviour
Creation Date: 2017-12-04
Thanks for the tutorial. I have been able to set up my first successful build.