# Setting up Continuous Quality with GitHub and Travis for Python
# Pre-requisites
- A GitHub account.
- Travis CI integration with GitHub.
- DeepSource integration with GitHub.
TIP
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 / Repository root
TIP
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 clickActivate 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
TIP
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 theDSN
from theReporting
section.
DeepSource / Repository Settings
- On Travis CI, go to
Settings
>Environment Variables
and addDEEPSOURCE_DSN
environment variable with value as theDSN
copied above.
Travis CI / Dashboard
Travis CI / Repository Settings
WARNING
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 clickBranches
tab. UnderBranch protection rules
section, add a new branch by clickingAdd rule
(or) updating existing branch protection configuration by clickingEdit
against the listed branch.
GitHub / Repository Settings
- Enable
DeepSource:Python
andcontinuous-integration/travis-ci
GitHub / Repository Settings/ Branch protection
WARNING
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