Continuous integration/Tutorials/Test your Python

This tutorial is aimed at getting Jenkins jobs to be triggered for your python project. We will cover steps that needs to be done in your code repository and how to have jobs added and triggered by Wikimedia continuous integration platform.

Design
The jobs are meant to be run using, a thin wrapper around. As a prerequisite you will need a working  to let tox install your python project in an isolated environment. Each environment defined in the tox file can then be defined as a Jenkins job. Whenever a change is proposed in Gerrit, all the defined jobs will be triggered and report back in Gerrit. The jobs are run on labs instance under the integration labs project.

Prerequisites
You should have a working setup.py installed and a few tests. What is the point of writing a python module if it can not be installed and lacks even the most basic tests. One can use the python module pbr to easily bootstrap a setup.py.

Matching conventions and adding tox support
Your repository should match a bunch of conventions and support tox. Do not be afraid, that is surprisingly very easy to achieve. We will get environments to run the flake8 python linter and nose tests.

Create a configuration file  to finely tune the behavior of the utilities:

List your requirements! Most probably your project will require additional modules. The convention in python world is to list the modules in two files:


 * requirements.txt : for modules needed to execute your software
 * test-requirements.txt : additional dependencies when executing tests

They will be added to tox configuration which invokes 'pip install --pre'.

Then create a tox configuration boilerplate. This is done in a file   at the root of your repository. We will define two environments: py26 and py27 for tests with python 2.6 and 2.7, then flake8 to run the linting utility. If your code is for Python 3, you could use py34 and/or py35.

Then you can install tox:

pip install tox

At first list environment triggered by default:

Execute them using. If all went well you will seen green success.

You can execute a single environment by passing -e to tox:

Editing Zuul configuration
The configuration is held in  repository as.

Under the  section add an entry for the Gerrit repository name (ex:  ). We will want to trigger on patch proposals and when someone vote +2:



This is a special job that will run the command  on a Debian Jessie virtual machine. You just have to create an entry for your repo and add a template:

Save, commit and propose the patch for review.

Refer to Zuul upstream documentation for configuration syntax for more details.