Setting up Continuous Quality with GitHub and Travis for Python

Python, GitHub, Travis and DeepSource

Pre-requisites

To integrate DeepSource with GitHub, please follow the quickstart guide. Source code for this guide resides is available on GitHub.

The following steps walk you through how to add python and test-coverage analyzers to DeepSource, how to setup test coverage with Travis CI and optionally how to enable branch protection rules with GitHub.

Configure python and test-coverage analyzers in .deepsource.toml

To enable analysis on the repository, an analysis configuration should be defined in the .deepsource.toml file in the repository's root. Let's configure for Python with test coverage.

# Version of the DeepSource TOML. Should be 1.
version = 1

# Glob pattern of the test files
test_patterns = [
  "tests/**"
]

# List of analyzers

# Python analyzer
[[analyzers]]
name = "python"
enabled = true
runtime_version = "3.x.x"

  [analyzers.meta]
  max_line_length = 90

# Test coverage analyzer
[[analyzers]]
name = "test-coverage"
enabled = true

Now that the configuration is written, create a file .deepsource.toml with the above contents in the repository's root.

GitHub file browser GitHub / Repository root

Refer to specific analyzer's configuration to see other available options.

Activate DeepSource analysis on the repository

  • To activate continuous analysis, go to DeepSource dashboard and click Activate new repo.

  • Since we've already added the configuration to the repository, select the repository and click I've already added the config, skip this step on right bottom and click Activate analysis on the repository overview page.

Configure Travis CI to run tests and report test coverage

Let's configure Travis CI to run the tests:

# Set build language to Python
language: python

# Set python version to 3.6
python: 3.6

# Install dependencies
install:
  - pip install -r requirements.txt

# Run tests
script:
  - coverage run test_hello.py

# Report results to DeepSource
after_success:
  # Generate coverage report in xml format
  - coverage xml

  # Install deepsource CLI
  - curl https://deepsource.io/cli | sh

  # Report coverage artifact to 'test-coverage' analyzer
  - ./bin/deepsource report --analyzer test-coverage --key python --value-file ./coverage.xml

Refer to Travis CI documentation to customize the build.

  • Create a file .travis.yml with the above contents on the repository root.

Activate Travis builds

  • Go to Settings page of the repository dashboard in DeepSource and copy the DSN from the Reporting section.

Repository settings page of DeepSource DeepSource / Repository Settings

  • On Travis CI, go to Settings > Environment Variables and add DEEPSOURCE_DSN environment variable with value as the DSN copied above.

Travis CI dashboard Travis CI / Dashboard

Repository settings page of Travis CI Travis CI / Repository Settings

Do not add the DEEPSOURCE_DSN variable as part of .travis.yml as the value contains sensitive information.

  • Trigger the build on Travis.

Setup GitHub status checks (optional)

Required status checks ensure that all required checks are passing before collaborators can make changes to a branch.

  • Go to repository Settings page on GitHub and click Branches tab. Under Branch protection rules section, add a new branch by clicking Add rule (or) updating existing branch protection configuration by clicking Edit against the listed branch.

Repository settings page of GitHub GitHub / Repository Settings

  • Enable DeepSource:Python and continuous-integration/travis-ci

Branch protection section of repository settings page of GitHub GitHub / Repository Settings/ Branch protection

If you enable required status checks, Travis and DeepSource checks will be strictly enforced -- pull requests can be merged only after all the tests pass and all issues of major or critical severity are fixed.

🎉 Travis and DeepSource now runs tests and quality analysis continuously on every code change.

DeepSource repository dashboard DeepSource / Repository Dashboard