Testing
Continuous Integration
Learning Objectives
- Understand how continuous integration speeds software development
- Understand the benefits of continuous integration
- Implement a continuous integration server
- Identify a few options for hosting a continuous integration server
To make running the tests as easy as possible, many software development teams implement a strategy called continuous integration. As its name implies, continuous integration integrates the test suite into the development process. Every time a change is made to the repository, the continuous integration system builds and checks that code.
Thought Experiment: Does Your Software Work on Your Colleague’s Computer?
Imagine you developed software on a MacOSX computer. Last week, you helped your office mate build and run it on their Linux computer. You’ve made some changes since then.
- How can you be sure it will still work if they update their repository when they come back from vacation?
- How long will that process take? machine until you try rebuilding it on their machine.
The typical story in a research lab is that, well, you don’t know whether it will work on your colleagues’ machine until you try rebuilding it on their machine. If you have a build system, it might take a few minutes to update the repository, rebuild the code, and run the tests. If you don’t have a build system, it could take all afternoon just to see if your new changes are compatible.
Since the first step the server conducts is to check out the code from a repository, we’ll need to put our code online to make use of this kind of server (unless we are able/willing to set up our own CI server).
Set Up a Mean Git Repository on GitHub
Your mean.py
test_mean.py
files can be the contents of a repository on GitHub.
- Go to GitHub and create a repository called mean.
- Clone that repository (git clone https://github.com:yourusername/mean)
- Copy the
mean.py
andtest_mean.py
files into the repository directory. - Use git to
add
,commit
, andpush
the two files to GitHub.
Giving Instructions
Your work on the mean function has both code and tests. Let’s copy that code into its own repository and add continuous integration to that repository.
Decide How To Set Up the Server
It doesn’t need a build system, because Python does not need to be compiled.
- What does it need?
- Write the names of the software dependencies in a file called requirements.txt and save the file.
- In fact, why don’t you go ahead and version control it?
Travis-CI
Travis is a continous integration server hosting platform. It’s commonly used in Ruby development circles as well as in the scientific python community.
A file called .travis.yml
in your repository will signal to Travis that you want to build and test this repository on Travis-CI. Such a file, for our purposes, is very simple:
language: python
python:
- "2.6"
- "2.7"
- "3.2"
- "3.3"
- "3.4"
- "nightly"
# command to install dependencies
install:
- "pip install -r requirements.txt"
# command to run tests
script: py.test
You can see how the python package manager, pip, will use your requirements.txt file from the previous exercise. That requirements.txt file is a conventional way to list all of the python packages that we need. If we needed pytest, numpy, and pymol, the requirements.txt file would look like this:
numpy
pymol
pytest
Add a .travis.yml File
- Add .travis.yml to your repository
- Commit and push it.
- Check the situation at your server
Continuous Integration Hosting
We gave the example of Travis because it’s very very simple to spin up. While it is able to run many flavors of Linux, it currently doesn’t support other platforms as well. Depending on your needs, you may consider other services such as: