GeezerVison is showing docs.deepsource.com — Code coverage | DeepSource Docs
Code coverage
CI provider guides, Docker container coverage, and multi-report setup for DeepSource test coverage tracking.
This page covers CI-specific configuration for reporting test coverage to DeepSource. For initial setup steps, see the Track code coverage getting-started guide.
Default Kover report location: build/reports/kover/xml/report.xml. Kover automatically runs your test suite — use -x test if you want to run tests separately.
Running tests inside a Docker container is a widely used practice in the software development community since it provides a consistent environment to run the application.
But, at the same time it also adds isolation and therefore, the test coverage reports generated inside the container are not accessible to the outside environment i.e. the CI systems on which the testing pipeline is running.
However, the following two methods can be used to report the test coverage data to DeepSource.
For reporting test coverage to DeepSource from inside the container which runs tests, just pass some environment variables to the container using the --env/-e flag.
docker run -e DEEPSOURCE_DSN -e USER -e TRAVIS_PULL_REQUEST_SHA ...
# Export the latest git commit hash as an environment variableexport GIT_COMMIT_SHA=$(git --no-pager rev-parse HEAD | tr -d '\n')# Pass the exported environment variable to the container in which tests# need to be rundocker run -e DEEPSOURCE_DSN -e GIT_COMMIT_SHA ...
The test coverage report can also be reported by copying it from the Docker container in which tests are run to a shared directory which the host can also access.
# Creating a directory to store test coverage data in the host# This directory will be mounted in the docker container as wellmkdir shared_coverage# Run the Docker container which runs tests# The `-v` flag sets up a bindmount volume that links the ~/coverage directory# from inside the container to the ~/shared_coverage directory on the host machine.docker run --name=test -v "~/shared_coverage:$HOME/coverage" ...# Report the test coverage report stored in the shared directory to DeepSource./bin/deepsource report --analyzer test-coverage --key python --value-file./shared_coverage/coverage.xml
In the Dockerfile of the container which runs test, make sure that the generated test coverage report is moved to the shared directory.
# In the container Dockerfilemv coverage.xml ~/coverage
If you're using a CI to run your tests, the recommended way to use the Test Coverage Analyzer is by automating the coverage reporting from your CI. This should be done in the scripts that are running your tests.
These are the things that you'd need to take care of:
Enable the test-coverage Analyzer in your repository settings.
Make sure that your CI is checking out the same commit and not making a merge commit. Failing to do this would cause the coverage report to be associated with the merge commit, and DeepSource would never pick it up for a run.
Install DeepSource CLI. This would be needed to report the coverage artifact.
DeepSource DSN for the repository. This is needed to identify the repository for which the coverage report is being sent. CLI looks for the DSN
Do not add the DEEPSOURCE_DSN variable as part of any publicly visible configuration file. It contains sensitive information.
The sections below contain boilerplate config for different CI providers. Please refer to the CLI documentation to learn more about CLI's report command used in the examples.
On Travis CI, go to Settings > Environment Variables and add a DEEPSOURCE_DSN environment variable with the DSN copied above as its value.
Add this to .travis.yml:
after_success: # Phase: Tests have finished and a test coverage report is available # Install CLI - curl -fsSL https://cli.deepsource.com/install | BINDIR=./bin sh # From the root directory, run the report coverage command - ./bin/deepsource report --analyzer test-coverage --key python --value-file ./path/to/report
On Circle CI, go to Settings > Environment Variables and add a DEEPSOURCE_DSN environment variable with the DSN copied above as its value.
Add the following step in .circleci/config.yml:
- run: name: Report results to DeepSource command: | # Tests have finished and a test coverage report is available # Install CLI curl -fsSL https://cli.deepsource.com/install | BINDIR=./bin sh # From the root directory, run the report coverage command ./bin/deepsource report --analyzer test-coverage --key python --value-file ./path/to/report
On GitHub, navigate to the main page of the repository. Under your repository name, click "Settings". In the left sidebar, click Secrets.
Type DEEPSOURCE_DSN in the "Name" input box.
Add the value copied above.
When you checkout code, ensure that you use pull request HEAD commit instead of merge commit:
GitHub actions make a merge commit during checkout unless it is asked not to do it. If you're using GitHub actions, please make sure you make the following change in your checkout action.
Add the following step in .github/workflows/main.yml:
- name: Report results to DeepSource run: | # Tests have finished and a test coverage report is available # Install CLI curl -fsSL https://cli.deepsource.com/install | BINDIR=./bin sh # From the root directory, run the report coverage command ./bin/deepsource report --analyzer test-coverage --key python --value-file ./path/to/report env: DEEPSOURCE_DSN: ${{ secrets.DEEPSOURCE_DSN }}
You can also use OIDC to authenticate and send the coverage report to DeepSource, without using the DSN.
Here are the steps to do that:
Make sure your GitHub Actions workflow is not making a merge commit during checkout. You can do this by using the ref input in the actions/checkout step:
Pass --use-oidc flag to the deepsource report command while sending the coverage report:
This will use the workflow's OIDC token to get a temporary DSN for the repository and send the coverage report to DeepSource.
- name: Report results to DeepSource run: | # Tests have finished and a test coverage report is available # Install CLI curl -fsSL https://cli.deepsource.com/install | BINDIR=./bin sh # From the root directory, run the report coverage command ./bin/deepsource report --analyzer test-coverage --key python --value-file ./path/to/report --use-oidc
Navigate to the project page of the repository on GitLab. Under project settings, in the sidebar, click on "CI/CD". Expand the variable section, and add the following:
Type: "Variable"`
Key: DEEPSOURCE_DSN
Value: The DSN value copied above
State: Protected (Yes)
Masked: Yes
Scope: All Environments
Add the following under the test job in .gitlab-ci.yml:
test: script: # Tests have finished and a test coverage report is available # Install CLI - curl -fsSL https://cli.deepsource.com/install | BINDIR=./bin sh # From the root directory, run the report coverage command - ./bin/deepsource report --analyzer test-coverage --key python --value-file ./path/to/report
Navigate to the app's Settings tab in the Heroku Dashboard and then add the Config Variables:
KEY: DEEPSOURCE_DSN
VALUE: The DSN value copied above
Run the following commands:
# Tests have finished and a test coverage report is available# Install CLI- curl -fsSL https://cli.deepsource.com/install | BINDIR=./bin sh# From the root directory, run the report coverage command for DeepSource to analyze it- ./bin/deepsource report --analyzer test-coverage --key python --value-file ./path/to/report
Value: The DSN value for your repository on DeepSource
Add the following script in the pipeline(s) running tests for the repository:
# Add this after the script to run tests, which also generates a coverage report- script: | # Install CLI curl -fsSL https://cli.deepsource.com/install | BINDIR=./bin sh # From the root directory, run the report coverage command for DeepSource to analyze it ./bin/deepsource report --analyzer test-coverage --value-file ./path/to/reportdisplayName: "Report coverage results to DeepSource"env:DEEPSOURCE_DSN: $(DEEPSOURCE_DSN)
In case of pull requests, Azure DevOps can make a merge commit that doesn't belong to the Repo's actual GIT tree. You can let DeepSource know about the correct commit OID of PR it should associate the coverage report to by setting the following environment variable.
DeepSource supports merging coverage reports implicitly. If you have multiple CI pipelines generating partial coverage reports, send them as soon as they are generated under the same key name. DeepSource will combine all of them to prepare a final result. For example, if two CI pipelines test platform-specific parts of a module, you can report both the artifacts, and DeepSource will implicitly combine the results of the reports.
You'll notice a newly updated check every time you submit a new artifact. There's no time limit for sending multiple coverage reports.
The JaCoCo and Clover coverage formats report metrics for individual methods and may not contain data about the individual covered lines in the methods.
For such cases, DeepSource uses a max operation on the reported metrics to calculate the aggregate report. So if parts of certain methods are covered in different coverage reports, the reported line coverage for that method may be lower than the actual line coverage.
For example, if one report covers the top 25% lines for a method, and another report covers the bottom 25% lines for the same method, since there is no way of knowing if both the lines in the reports are different, DeepSource will report the coverage to be 25% even though it might be 50%. This is only a limitation for JaCoCo and Clover coverage formats at the moment.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.