Continuous integration is an important part of software development and has several advantages, such as a faster development cycle, higher software quality, and happier users of your software. Travis CI is a service that provides continuous integration and will test your software project for every change. Their service provides testing environments for many programming languages including C, C++, C#, Java, PHP, Python, Ruby, and several others.
At SmartFile, we use Travis CI to test various components of our software stack before releasing it to customers. This allows us to make improvements without breaking existing functionality.
In this tutorial, I’ll walk through the three steps for setting up Travis CI to test a Python project hosted on GitHub.
Step 1: Link Travis CI and GitHub
First, Travis CI needs to know about your GitHub project. Login to Travis CI and press the “+” button. Next, flick the repository switch on for your repo:
If you cannot see your code repo, press the “Sync” button to refresh the list of public repositories. If you have a private GitHub project, you need to upgrade to Travis CI Pro since it is only free for public repositories.
Step 2: Build Configuration
Next, create a file called “.travis.yml” in the root of your project repo:
language: python python: - "2.7" cache: pip install: - pip install -r requirements.txt script: - python tests.py
- The ‘language’ section is used to specify the language of the software.
- The ‘python’ section is used to specify the version or versions of Python to use for testing.
- The ‘install’ section is used to specify commands to run before testing, such as the installation of dependencies or the compilation of required packages.
- The ‘script’ section is used to specify the command to test your software. Typically, this command will find all tests in your project and run them. The specified command must exit with a status code of 0 if the test is successful; otherwise the test will be considered a failure.
Alternatively, I can specify multiple versions of Python and Travis CI will test my project with each version:
language: python python: - "2.7" - "3.3" - "3.4" cache: pip install: - pip install -r requirements.txt script: - py.test --cov-report term-missing --cov app -v
For advanced build configuration, see the Travis CI documentation on Configuring your build.
Step 3: Trigger builds
To trigger builds, push the commit that adds “.travis.yml” to the project.
Travis CI makes it easy to visualize build status for every commit.
Advanced Travis CI topics
There are many other options for setting up continuous integration with Travis CI:
I hope you enjoyed these three steps for testing python with Travis CI.