Code coverage
You can add code coverage to a Harness CI pipeline by configuring code coverage tools in your codebase and adding code coverage commands to steps that run tests.
This documentation is not exhaustive. For information about languages or code coverage tools not described here, refer to the documentation for that tool or language.
For more information about running tests in Harness CI, go to Run tests in CI pipelines.
Code coverage by language
The following examples show how to include code coverage in a Harness CI pipeline for different languages.
Go
Go has built-in code coverage functionality.
-
Add the following commands to the Run step where you run your tests:
go test -cover -coverprofile=c.out
go tool cover -html=c.out -o coverage.htmlFor example:
- step:
type: Run
identifier: test
name: Test
spec:
shell: Sh
command: |-
go test -cover -coverprofile=c.out
go tool cover -html=c.out -o coverage.html -
Add a step to upload your code coverage report to cloud storage.
-
Add a step to view your code coverage report on the Artifacts tab.
Java
-
Set up a Java code coverage tool, such as JaCoCo. By including JaCoCo in
pom.xml
, themvn test
command automatically writes a code coverage report to anexec
file. -
Run your tests in a Run step, for example:
- step:
type: Run
name: run test
identifier: run_test
spec:
shell: Sh
command: |-
mvn test
reports:
type: JUnit
spec:
paths:
- target/surefire-reports/*.xml -
Store and publish your code coverage report:
- If you're using JaCoCo, use the JaCoCo Drone plugin in a Plugin step. This plugin uploads your JaCoCo code coverage report to S3 and publishes it to the Artifacts tab on the Build details page.
- With other Java code coverage tools:
- Add an Upload Artifacts to GCS step or Upload Artifacts to S3 step.
- Use the Artifact Metadata Publisher plugin to view your code coverage report on the Artifacts tab.
JavaScript
-
If necessary, set up a JavaScript code coverage tool, such as Istanbul. Your test tool may already include code coverage; for example, Istanbul is included with Jest.
-
Add code coverage arguments or commands to the relevant Run step. For example, with Jest, add
--collectCoverage=true
to yourjest
command.- step:
type: Run
name: Run Jest Tests
identifier: run_jest_tests
spec:
shell: Sh
command: |-
yarn add --dev jest-junit
jest --ci --runInBand --reporters=default --reporters=jest-junit --collectCoverage=true
envVariables:
JEST_JUNIT_OUTPUT_DIR: "/harness/reports"
reports:
type: JUnit
spec:
paths:
- "/harness/reports/*.xml" -
Add a step to upload your code coverage report to cloud storage.
-
Add a step to view your code coverage report on the Artifacts tab.