# Test Coverage

Test coverage analyzer accepts the coverage report file and creates metrics and issues around code coverage.

This section covers configuration specific to test-coverage analyzer. Please make sure to read the general configuration guide first.

# Configuration - .deepsource.toml

# name

name = "test-coverage"

# enabled

enabled = true

# Configuration - Artifacts

Refer 'report' command of CLI documentation for command usage.

Key Value format
python XML(Cobertura)
go cover.out (Generated)
javascript XML(Cobertura)
ruby JSON(SimpleCov)

TIP

The maximum allowed size for the coverage report file is 5 MB.

# Examples

# JavaScript

# JavaScript: Jest

Currently, only the cobertura-XML format is supported by DeepSource.

Here are the steps to report the coverage data.

# Make sure `jest` is installed. If not, install it using `yarn`
yarn add --dev jest
# OR `npm`
npm install --save-dev jest

# Add the following options to the jest config to get a cobertura report
"collectCoverage": true,
"coverageReporters": ["cobertura"]

# Or, add these options as flags to test script in package.json
{
  "scripts": {
    "test": "jest --coverage=true --coverageReporters=cobertura"
  }
}

# Run the tests
yarn test
# OR
npm test

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

# Set DEEPSOURCE_DSN env variable from repository settings page
export DEEPSOURCE_DSN=https://sampledsn@deepsource.io

# From the root directory, run the report coverage command
./bin/deepsource report --analyzer test-coverage --key javascript --value-file ./coverage/cobertura-coverage.xml

TIP

If the coverage file is generated at a path other than the root directory, pass that path to the value-file flag of the last command.

Example :

./bin/deepsource report --analyzer test-coverage --key javascript --value-file ./src/app/coverage/cobertura-coverage.xml

# Go

# Run your tests and generate coverage report
go test -coverprofile=cover.out

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

# Set DEEPSOURCE_DSN env variable from repository settings page
export DEEPSOURCE_DSN=https://sampledsn@deepsource.io

# From the root directory, run the report coverage command
./bin/deepsource report --analyzer test-coverage --key go --value-file ./cover.out

# Note: Tests for different go packages

If you are running tests and generating coverage report for different packages separately, you need to combine all generated coverage reports before reporting it to the test-coverage analyzer.

This is how you can combine the coverage artifacts before reporting it:

# Run tests for a package
go test -coverprofile=cover.out ./somePackage

# Append coverage data in a seperate file
cat cover.out >> coverage.out

# Run tests for another package
go test -coverprofile=cover.out ./someOtherPackage

# Combine the coverage data in the same way
cat cover.out >> coverage.out

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

# Set DEEPSOURCE_DSN env variable from repository settings page
export DEEPSOURCE_DSN=https://sampledsn@deepsource.io


# From the root directory, run the report coverage command
./bin/deepsource report --analyzer test-coverage --key go --value-file ./coverage.out

# Python

# Python: unittests

# Install coverage.py pacakage from pip
pip install coverage

# Run coverage
coverage run tests.py

# Generate coverage report in xml format
coverage xml

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

# Set DEEPSOURCE_DSN env variable from repository settings page
export DEEPSOURCE_DSN=https://sampledsn@deepsource.io

# From the root directory, run the report coverage command
./bin/deepsource report --analyzer test-coverage --key python --value-file ./coverage.xml

# Python: pytest

# Install pytest and pytest-cov pacakages from pip
pip install pytest pytest-cov

# Run pytest with --cov and --cov-report flags
pytest --cov=./ --cov-report xml

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

# Set DEEPSOURCE_DSN env variable from repository settings page
export DEEPSOURCE_DSN=https://sampledsn@deepsource.io

# From the root directory, run the report coverage command
./bin/deepsource report --analyzer test-coverage --key python --value-file ./coverage.xml

Reference: https://pypi.org/project/pytest-cov/ (opens new window)

# Python: nose2

# Install nose2 package from pip
pip install nose2[coverage_plugin]>=0.6.5

# Run nose with --with-coverage and --coverage-report flags
nose2 --with-coverage --coverage-report xml

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

# Set DEEPSOURCE_DSN env variable from repository settings page
export DEEPSOURCE_DSN=https://sampledsn@deepsource.io

# From the root directory, run the report coverage command
./bin/deepsource report --analyzer test-coverage --key python --value-file ./coverage.xml

Reference: https://docs.nose2.io/en/latest/plugins/coverage.html (opens new window)

# NOTE: Usage with tox

If you are using tox.readthedocs.io (opens new window) then make sure you omit .tox/* and env/* in your .coveragerc file because when running tests with tox the coverage report will contain test report files other than your source e.g site-packages etc.

example .coveragerc

[run]
branch = True
source = src
omit =
    .tox/*
    env/*

and with multiple python versions, combine the coverage data files before reporting.

example tox.ini

[tox]
envlist = cov-init,py27,py36,py37,cov-report
skipsdist=True
skip_missing_interpreters=True


[testenv]
setenv =
    COVERAGE_FILE = .coverage.{envname}
deps =
    pytest
    pytest-cov
    coverage
commands =
    pytest --cov


[testenv:cov-init]
skipsdist = True
setenv =
    COVERAGE_FILE = .coverage
deps = coverage
commands =
    coverage erase


[testenv:cov-report]
skipsdist = True
setenv =
    COVERAGE_FILE = .coverage
deps = coverage
commands =
    coverage combine
    coverage report
    coverage xml

# Ruby

# SimpleCov

First, install simplecov if not already installed.

gem install simplecov

Then, follow the steps below to generate test coverage.

  1. Add the following to spec_helper.rb file inside the tests folder:
# frozen_string_literal: true

require 'simplecov'

SimpleCov.start
  1. Add --require spec_helper.rb to .rspec file.
  2. Run rspec using bundle exec rake rspec to generate coverage.
  3. The coverage results will be available inside the coverage folder created.

Now, you can do the following to report the coverage to DeepSource:

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

# Set DEEPSOURCE_DSN env variable from repository settings page
export DEEPSOURCE_DSN=https://sampledsn@deepsource.io

# From the root directory, run the report coverage command
./bin/deepsource report --analyzer test-coverage --key ruby --value-file ./coverage/.resultset.json

TIP

SimpleCov generates the results in JSON format to .resultset.json. Please report the same to DeepSource.

# Tracking Coverage with CI

Last Updated: 10/17/2020, 1:48:35 PM