For the complete documentation index, see llms.txt. This page is also available as Markdown.

Code Coverage (Third-Party Tools)

Include code coverage in your CI pipelines using third-party tools.

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.

  1. 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.html

    For 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
  2. Add a step to upload your code coverage report to cloud storage.

Java

  1. Set up a Java code coverage tool, such as JaCoCo. By including JaCoCo in pom.xml, the mvn test command automatically writes a code coverage report to an exec file.

  2. Run your tests in a Run step, for example:

  3. Store and publish your code coverage report:

JavaScript

  1. 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.

  2. Add code coverage arguments or commands to the relevant Run step. For example, with Jest, add --collectCoverage=true to your jest command.

  3. Add a step to upload your code coverage report to cloud storage.

PHP

The built-in phpdbg tool can generate code coverage reports.

  1. Add the following command to the Run step where your run your tests:

    For example:

  2. Add a step to upload your code coverage report to cloud storage.

Python

Use these steps to install code coverage tools when you run Python tests in Run steps.

  1. Install a Python code coverage tool, such as Coverage.py. Depending on your build infrastructure, you can install this directly on the host machine or use a Run step to set up the test environment at runtime.

  2. Add code coverage commands to the Run step where your run your tests.

    COVERAGE.PY USAGE

    With Coverage.py, replace the initial python or pytest in your usual test commands with coverage run.

    For more information, refer to the Coverage.py quick start guide.

  3. Add a step to upload your code coverage report to cloud storage.

Ruby

Use these steps to install code coverage tools when you run Ruby tests in Run steps.

  1. Set up a Ruby code coverage tool, such as SimpleCov.

  2. Run your tests in a Run step.

    SimpleCov doesn't require additional commands in the Run step since it is loaded in test/test_helper.rb.

  3. Add a step to upload your code coverage report to cloud storage.

Code coverage services

You can use code coverage services with Harness.

Codacy

You can use the Codacy Drone plugin in a Plugin step to upload Golang coverage reports to Codacy.

For other languages, go to the Codacy documentation on uploading coverage data to Codacy.

CodeCov

You can use a Run step to include CodeCov code coverage in a Harness CI pipeline. The Run step contains a script that runs tests with code coverage, then downloads and runs the CodeCov Uploader tool.

  1. Make sure you have a CodeCov account with code coverage enabled on a code repo and a CodeCov Upload Token. For instructions, go to CodeCov Quick Start.

  2. Create a Harness text secret containing your CodeCov Upload Token. Make note of the secret's ID.

  3. Enter a Name for the step.

  4. Depending on your build infrastructure, you might need to specify a Container Registry and Image containing the binaries that the step needs to run your commands.

    For example, you need to run pytest, and pytest isn't available on the build machine, you must specify a Docker connector and image, such as pytest:latest.

    For information about when these fields are required and how to specify images, go to Use Run steps.

  5. Make sure the Command field includes all commands necessary to prepare the test environment, run tests with code coverage, and download and run the CodeCov Uploader tool, for example:

  6. In Environment Variables, add a CODECOV_TOKEN environment variable and set the value to an expression referencing your CodeCov Upload token secret, such as <+secrets.getValue("my_codecov_upload_token")>.

  7. Add one or more Report Paths for your code coverage reports.

  8. Select Apply Changes to save the step, and then select Save to save the pipeline.

Here are examples of Run steps configured for CodeCov code coverage in the Visual and YAML editors.

When you run your pipeline, you can review CodeCov information in the Run step's logs on the Build details page. If the results were successfully uploaded to CodeCov, the logs include a resultURL that you can follow to view the code coverage output in your CodeCov account, such as:

Alternately, you can run your tests with code coverage in a Run step and then upload the results to CodeCov by running the CodeCov Drone plugin in a Plugin step.

Coveralls

To integrate Coveralls in your Harness CI pipelines, follow the Coveralls documentation to Integrate Coveralls with your codebase. Note the following:

  • For Step 2: Choose an integration, use the Universal Coverage Reporter.

  • For Step 3: Configure your project to send coverage to Coveralls:

    • Create a Harness text secret for your COVERALLS_REPO_TOKEN.

    • Add the COVERALLS_REPO_TOKEN environment variable to steps in your CI pipelines that run tests with code coverage.

    • For the environment variable value, use a Harness expression to reference the encrypted text secret, such as <+secrets.getValue("YOUR_COVERALLS_SECRET_ID")>.

Add an environment variable to a step

In Harness, edit the step that runs your tests with code coverage.Under Environment Variables, select Add.Set the key to COVERALLS_REPO_TOKEN.Set the value to <+secrets.getValue("YOUR_COVERALLS_SECRET_ID")>Add envVariables to the step.spec for the relevant Run or Test step. - step: type: Run name: npm test identifier: npm_test spec: shell: Sh command: |- npm install npm run build --if-present npm test reports: type: JUnit spec: paths: - report.xml envVariables: COVERALLS_REPO_TOKEN: <+secrets.getValue("YOUR_COVERALLS_SECRET_ID")>

You can also upload coverage reports to Coveralls by using the Coveralls Drone plugin in a Plugin step.

View code coverage reports on the Artifacts tab

You can use Drone plugins to view code coverage reports on the Artifacts tab on the Build details page.

The Artifact Metadata Publisher plugin pulls content from cloud storage and publishes it to the Artifacts tab.

  1. Add steps to your pipeline that run tests with code coverage and produce code coverage reports.

  2. Add a step to upload the report artifact to cloud storage.

  3. Add a Plugin step that uses the artifact-metadata-publisher plugin. Configure the Plugin step settings as follows:

    • Name: Enter a name.

    • Container Registry: Select a Docker connector.

    • Image: Enter plugins/artifact-metadata-publisher.

    • Settings: Add the following two settings as key-value pairs.

      • file_urls: Provide the URL to the code coverage artifact that was uploaded in the Upload Artifacts step. If you uploaded multiple artifacts, you can provide a list of URLs.

      • artifact_file: Provide any .txt file name, such as artifact.txt or url.txt. This is a required setting that Harness uses to store the artifact URL and display it on the Artifacts tab. This value is not the name of your uploaded artifact, and it has no relationship to the artifact object itself.

  1. Add steps to your pipeline that run tests with code coverage and produce code coverage reports.

  2. Add a step to upload the report artifact to cloud storage.

  3. Add a Plugin step that uses the artifact-metadata-publisher plugin, for example:

The S3 Upload and Publish plugin uploads a specified file or directory to AWS S3 and publishes it to the Artifacts tab.

  1. Add steps to your pipeline that run tests with code coverage and produce code coverage reports.

  2. Add a Plugin step that uses the drone-s3-upload-publish plugin. Configure the Plugin step settings as follows:

    • Name: Enter a name.

    • Container Registry: Select a Docker connector.

    • Image: Enter harnesscommunity/drone-s3-upload-publish.

    • Settings: Add the following seven settings as key-value pairs.

      • aws_access_key_id: An expression referencing a Harness secret or pipeline variable containing your AWS access ID, such as <+pipeline.variables.AWS_ACCESS>

      • aws_secret_access_key: An expression referencing a Harness secret or pipeline variable containing your AWS access key, such as <+pipeline.variables.AWS_SECRET>

      • aws_default_region: Your default AWS region, such as ap-southeast-2

      • aws_bucket: The target S3 bucket.

      • artifact_file: Provide any .txt file name, such as artifact.txt or url.txt. This is a required setting that Harness uses to store the artifact URL and display it on the Artifacts tab. This value is not the name of your uploaded artifact, and it has no relationship to the artifact object itself.

      • source: Provide the path, in the build workspace, to the file or directory that you want to upload. If you want to upload a compressed file, you must use a Run step to compress the artifact before uploading it.

      • target: Optional.

    • Image Pull Policy: Select If Not Present

  1. Add steps to your pipeline that run tests with code coverage and produce code coverage reports.

  2. Add a Plugin step that uses the drone-s3-upload-publish plugin, for example:

For aws_access_key_id and aws_secret_access_key, use expressions to reference Harness secrets or pipeline variables containing your AWS access ID and key. You could also use expressions for target, such as <+pipeline.name>/<+pipeline.sequenceId>, which would automatically organize your artifacts into directories based on the pipeline name and incremental build ID.

If you want to upload a compressed file, you must use a Run step to compress the artifact before uploading it.

Code coverage reports are not the only artifacts you can publish to the Artifacts tab. You can publish any URL to the Artifacts tab.

Code Coverage and Test Intelligence (TI)

Customers utilizing Test Intelligence in steps before Code Coverage reports may find that Test Intelligence will affect the overall code coverage. This is due to the nature of how Test Intelligence works in that it executes fewer tests in order to speed up the overall testing process.

Because Test Intelligence is anticipated to execute fewer tests, customers will experience lower Code Coverage numbers and may conflict with expected test code coverage metrics. The recommended approach is to disable Test Intelligence if you wish to run Code Coverage.

Alternatively, you can execute it in a separate pipeline on a daily or weekly basis. Test Intelligence and Testing Code Coverage are conflicting objectives to achieve so you will have to pick one or the other.

Below is a sample of what can happen: As you can see from the below screenshot, with Test Intelligence off, all tests were executed, and Code Coverage shows that it had a score of 69.9. Code Coverage without Test Intelligence

Once the Test Intelligence was engaged, many of the tests were skipped because of the initial testing. As a result, almost no tests were run and were skipped, so the test step completed quickly. As a result, the coverage for this execution is much lower (9.6), because the actual tests executed were significantly fewer. Code Coverage with Test Intelligence

Last updated

Was this helpful?