Read time: 3.6 minutes (358 words)

Step 4: Continuous Integration

Many program development projects (most?) rely on Continuous Integration to ensure their code is always ready for deployment on a number of platforms.

For Open Source developers, there are a number of free Continuous Integration services that can be used to test projects on a number of different platforms.

This project will use three services for Continuous Integration testing:

In this section, we will configure all three services for basic project testing.

GitHub Workflows

Hosting your project on GitHub provides an easy way to test your code. Setting up a basic test process is as easy as clicking on Actions then scrolling down until you see the Python application entry. Selecting this will create a new directory in your project named .github. Under a workflows subdirectory you will find a file named python_app.yml. This is a YAML file containing instructions on how to clone your project into a virtual machine, install any dependencies, then run tests on your code.

 1name: Python application
 2
 3on:
 4  push:
 5    branches: [ main ]
 6  pull_request:
 7    branches: [ main ]
 8
 9jobs:
10  build:
11    runs-on: ubuntu-latest
12    steps:
13    - uses: actions/checkout@v2
14    - name: Set up Python 3.9
15      uses: actions/setup-python@v2
16      with:
17        python-version: 3.9
18    - name: Install dependencies
19      run: |
20        python -m pip install --upgrade pip
21        pip install flake8 pytest
22        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
23        sudo apt-get update
24        sudo apt-get install openscad
25        sudo apt-get install antlr4
26    - name: Lint with flake8
27      run: |
28        python -m flake8 tests --count --select=E9,F63,F7,F82 --show-source --statistics
29        python -m flake8 tests --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
30    - name: Test with pytest
31      uses: GabrielBB/xvfb-action@v1
32      with:
33          run: python -m pytest

Travis-CI

Setting up testing on Travis-CI is similar. We need to create another control file, this one named .travis.yml. This needs to be placed in the root directory of the project. For this initial example, we will again test on a Linux host.

 1dist: focal
 2
 3services:
 4  - xvfb
 5
 6language: python
 7
 8python:
 9  - "3.9"
10
11install:
12  - pip install --upgrade pip
13  - pip install -r requirements.txt
14  - sudo apt-get update
15  - sudo apt-get install openscad
16  - sudo apt-get install antlr4
17
18script:
19    python -m pytest
20
21after_success:
22  - coveralls

To activate testing, you need to set up an account on Travis-CI, which can be done using your username and password from GitHub. Once that has been done, log in and you click on Settings. On this page you will see a Sync account button that will make sure you see all repositories you have on GitHub (I have over 100!). Scroll down until you see the project you are working on and click on the Setting button on the right side of the repository name. In the next panel, make sure both the :menuselection:`Build pushed branches and Build pushed pull requests are selected.

Now, navigate to your GitHub account and click on your account image. Select Settings ‣ Applications. You need to grant Travis-CI access to your account so it can receive notifications every time you push new changes to GitHub. When that happens, Travis-CI will create a new virtual machine, clone your updated code, and run the tests specified in the control file.

As with GitHub, we can add a badge to the README.rst file showing the results.