Overview of Cover Pipeline for GitLab
Cover Pipeline for GitLab integrates the power of Diffblue Cover directly into your GitLab pipeline to autonomously write Java unit tests for your projects on merge requests.
Automatically write a baseline unit test suite for your projects.
Automatically write new unit tests for new code.
Automatically update existing unit tests in your code.
Automatically remove existing unit tests in your code when they’re no longer required.
Introduction/Overview of Cover Pipeline for GitLab
Poor test coverage, or the complete lack of unit tests, acts as a blocker to CI implementation. Diffblue's reinforcement learning AI platform removes this blocker, autonomously writing and updating Java unit tests for your projects directly in your GitLab pipeline, helping you to save developer time, increase test coverage, and catch regressions.
On first use, Diffblue Cover will create your full test suite for your entire project. On subsequent code-change merge requests, these tests will be automatically updated to reflect the new behavior, allowing you and your development team to catch regressions and unplanned behavior changes.
Check out our introductory video for an overview of Cover Pipeline for GitLab and a brief demonstration of how to install, activate, and use Diffblue Cover directly within your GitLab pipeline. [4min]
Install, activate, configure Cover Pipeline for GitLab
Implementing Diffblue Cover into your pipeline is a straightforward process. In brief, we’ll:
Find and configure the Diffblue Cover integration.
Set up a pipeline for the sample project using the GitLab pipeline editor and the Diffblue Cover pipeline template. Note that the Quick Start - General describes how to configure Diffblue Cover in CI without using the integration or pipeline template.
Create the full baseline unit test suite for a sample project.
Short on time? If you'd like a fast-track experience check out our getting started video. [5 min]
Clone the sample project from the https://github.com/diffblue/demo-spring-petclinic
Diffblue repo to your GitLab account.
From the project in GitLab (either the sample Spring PetClinic project, or your own project) go to Settings > Integrations, find Diffblue Cover, and click Configure.
In the configuration screen, update the following (some items may be pre-selected):
Check/tick the Active box.
Enter your Diffblue Cover License key provided in your welcome email or by your organization. If needed, click the Try Diffblue Cover link to sign up for a free trial.
Enter details of your GitLab access token (Name and Secret) to allow Diffblue Cover to access your project. In general, use a GitLab Project Access Token with a Developer
role, plus api
and write_repository
scopes/permissions. If necessary you can use Group Access or Personal Access Tokens, again with api
and write
permissions. See the GitLab docs links below for full details on how to create an access token.
When you're done, click Save changes to apply your updates. Your Diffblue Cover integration is now Active and ready for use in your project.
Here we'll create a merge request pipeline for the project that will download the latest version of Diffblue Cover, build the project, write Java unit tests for the project, and commit the changes to the branch.
From the project in GitLab go to Build > Pipeline editor and click Configure pipeline to create/edit the .gitlab-ci.yml
file.
Copy the sample template below and paste this into the editor over-writing the default template created by GitLab.
When we commit these changes we'll use the commit to create a new branch and create a new merge request. So before you click Commit, update the following:
Update the Commit message with an appropriate description such as "Update .gitlab-ci.yml file
".
Update the Branch field with an appropriate name such as "add-diffblue-cover-pipeline
".
Check/tick Start a new merge request.
When you're ready, click Commit changes and go to #3.-create-the-baseline-unit-test-suite below.
For reference, an example Diffblue Cover pipeline template is shown below.
On the New merge request screen, update Title with an appropriate merge request title such as Add Cover pipeline and create baseline unit test suite
.
When you're ready, click Create merge request.
The merge request pipeline will now run Diffblue Cover to create the baseline unit test suite for the project. Once complete, go to the src/test
folders in the project repo to see the unit tests created by Diffblue Cover (suffixed with *DiffblueTest.java
).
When performing subsequent code changes to a project, the merge request pipeline will run Diffblue Cover but will only update the associated tests. The resulting diff can then be analyzed to check the new behavior, catch regressions, and spot any unplanned behavioral changes to the code. This workflow is illustrated further in GitLab workflow.
This topic demonstrates some of the key features of Cover Pipeline for GitLab and how to use the integration within a pipeline. The wider and deeper functionality, provided through dcover
commands in the pipeline template, can be implemented to extend your unit test capabilities even further.
When using the Diffblue Cover pipeline template with your own project and existing pipeline file, add the Diffblue template content to your file and modify as needed - see Configuration for details. Note that only specific dcover
commands and arguments are currently supported for use with Cover Pipeline for GitLab, as detailed in Configuration.
Once you're familiar with the topics covered under Cover Pipeline for GitLab, check out the Related topics to expand your knowledge further.
Illustrative development workflow, with and without Cover Pipeline for GitLab
Integrating Diffblue Cover into your CI/CD pipeline supports the software development workflow to provide AI automation of unit tests. The following provides an illustrative development workflow, with and without Cover Pipeline for GitLab, covering key steps to illustrate differences in workflow.
A Java project with manually written tests.
A Java project with an AI created Java Unit Test Suite.
A software developer makes a code change and manually updates the associated unit tests.
A software developer makes a code change - no manual unit test update is needed.
After a merge request, the code change and unit test change are manually reviewed.
After a merge request, the Diffblue Test Suite is automatically updated, and the code change and unit test change are reviewed. The test diff assists with catching regressions and bugs.
After passing code review, the updated code and associated unit tests are merged.
After passing code review, the updated code and associated unit tests are merged.
Cover Pipeline for GitLab configuration options
There are four key components for configuring Cover Pipeline for GitLab:
Labels: Pre-defined Diffblue Cover project labels to determine test requirements on merge requests (full baseline, update, or skip).
Integration Config: Direct license key and access token config using the Diffblue Cover integration page on GitLab.
Pipeline Config: Configure the Diffblue Cover section of your pipeline including Cover version, build config, and Cover commands to execute on merge requests.
Masked Variables: Define the Diffblue Cover license key and GitLab access token using GitLab masked variables. This avoids the need to configure the integration directly from the GitLab Integrations page.
When writing tests, Diffblue Cover will respond to specific project labels:
Diffblue Cover: Baseline Used to mark a merge request as requiring a full suite of tests to be written. This overrides the default behavior where Cover will only write tests related to the code changes already in the merge request. The baseline label is applied automatically when running Diffblue Cover for the first time on a project (to create the initial baseline test suite). On subsequent runs, you can select this label within a merge request, if required - useful when you want to refresh your entire test suite, such as when new product enhancements are released (to update the entire test suite with the latest enhancements).
Diffblue Cover: Skip Used to mark a merge request as requiring no tests to be written. The skip label is useful when performing merge requests that have no impact on unit tests, such as updates to comments only.
These labels are created automatically in GitLab when running Diffblue Cover for the first time on a project.
You can configure the Diffblue license key and associated GitLab access token directly, using the Diffblue Cover integration page on GitLab. Note that, if preferred, you can use GitLab masked variables to define these properties - see Masked variables.
From your project in GitLab go to Settings > Integrations, find Diffblue Cover, and click Configure.
In the configuration screen, update the following (some items may be pre-selected):
Check/tick the Active box.
Enter your Diffblue Cover License key provided in your welcome email or by your organization. If needed, click the Try Diffblue Cover link to sign up for a free trial.
Enter details of your GitLab access token (Name and Secret) to allow Diffblue Cover to access your project. In general, use a GitLab Project Access Token with a Developer
role, plus api
and write_repository
scopes/permissions. If necessary you can use Group Access or Personal Access Tokens, again with api
and write
permissions. See the GitLab docs links below for full details on how to create an access token.
When you're done, click Save changes to apply your updates. Your Diffblue Cover integration is now now Active and ready for use in your projects.
Update or create the .gitlab-ci.yml
file for your project to configure the Diffblue Cover section of your CI/CD pipeline.
From your project in GitLab go to Build > Pipeline editor and click Configure pipeline to create/edit the .gitlab-ci.yml
file.
Copy the Diffblue Cover pipeline template below Into your project .gitlab-ci.yml
pipeline file, and update if needed. Configurable properties are listed below - for everything else, please leave these set to the values defined in the Diffblue template.
image
Select the Cover CLI docker image to use with your CI tool.
Tag variations are provided for each supported JDK version - see https://hub.docker.com/r/diffblue/cover-cli for details. To use the latest version of Diffblue Cover, use one of the latest-jdk
tags. To use a specific release version, use one of the yyyy.mm.dd-jdk
tags.
script
(build command)
Diffblue Cover requires the project to be built before creating any tests. Either specify the build command here (Maven and Gradle examples provided in the template) or provide pre-built artifacts via a job dependency.
script
(dcover
commands)
Diffblue Cover commands and options to run. Core example provided in the template:
dcover
– the core Diffblue Cover command.
ci
– enable the GitLab CI/CD integration.
activate
- activate the license key.
validate
- remove non-compiling and failing tests.
create
- create new tests for the project.
--maven
– use the Maven build tool.
Configurable components:
Additional optional arguments can be used with the validate
and create
commands - see the Commands & Arguments topic for details.
The --maven
options can be changed to --gradle
to specify the Gradle build tool, if appropriate.
Cover Reports commands and arguments are not currently supported for use with Cover Pipeline for GitLab.
For reference, the Diffblue Cover pipeline template is shown below.
You can configure the Diffblue license key and associated GitLab access token using GitLab masked variables. Note that, if preferred, you can configure these properties directly, using the Diffblue Cover integration page on GitLab - see Integration config.
To add or update variables in the project or group settings (also see the Variables GitLab docs topic if needed):
In your group or project go to Settings > CI/CD and expand the Variables section.
Select Add variable and create each variable:
Key: Name of the variable. The three Diffblue variables to create are DIFFBLUE_LICENSE_KEY
, DIFFBLUE_ACCESS_TOKEN_NAME
, and DIFFBLUE_ACCESS_TOKEN
. Note that offline licenses can also optionally be configured via environment variables, see below.
Value: Value for the variable:
DIFFBLUE_LICENSE_KEY
- enter your Diffblue Cover License Key provided in your welcome email or by your organization.
DIFFBLUE_ACCESS_TOKEN_NAME
, and DIFFBLUE_ACCESS_TOKEN
- enter details of your GitLab access token (Name and Secret) to allow Diffblue Cover to access your project. In general, use a GitLab Project Access Token with a Developer
role, plus api
and write_repository
permissions. If necessary you can use Group Access or Personal Access Tokens, again with api
and write
permissions. See the GitLab docs links below for full details on how to create an access token.
Optional: Note that if you have an offline license and wish to configure the offline license activation file this can be done by using the environment variable DIFFBLUE_OFFLINE_LICENSE_ACTIVATION_FILE_CONTENTS
containing the contents of your offline license activation file ls_activation.lic
Type: Set to Variable
.
Environment scope: Optional. All
, or specific environments.
Protect variable Optional. If selected, the variable is only available in pipelines that run on protected branches or protected tags.
Mask variable Select this option. This will mask the variable’s Value in job logs.
After you create a variable, you can use it in your .gitlab-ci.yml
file or in job scripts.
Diffblue Cover log files are saved to a .diffblue/
directory in the pipeline artifacts (as defined in the .gitlab-ci.yml
file), and are available for download once the pipeline completes. These logs include user logs (also output during the run and visible in the job output) and support logs (if you ever require Diffblue support).
Troubleshooting Cover Pipeline for GitLab
Here are some common issues that you may encounter when configuring and using Cover Pipeline for GitLab.
Diffblue Cover performs a number of checks before, during, and after creating unit tests. Cover can generate a range of output codes during these checks to provide general information and highlight any issues. These codes and associated messages are output during the run, visible in the job output, and saved to the log files. See Output Codes for details.
For general Diffblue Cover issues, see the Cover CLI Troubleshooting topic.
License issues are reported using L - License Codes. Common issues include:
L005 - The license has expired
- Update your license key (see Configuration) and/or contact Diffblue Support for assistance.
L009 - License key not found
- Make sure you've entered the correct license key and in the format provided (see Configuration).
Access token issues are reported using the E120 - Git command failed
output code (see E - Environment Codes). For example:
Access token with incorrect permissions. In this case, check that the access token has correct permissions for the project (Developer
role, plus api
and write_repository
scopes/permissions) - see Configuration.
A mismatch of the access token name can lead to a Git error. GitLab requires the access token name used in the Cover Pipeline for GitLab integration to match the name of the access token in GitLab. Check and update your access token name as needed - see Configuration.
The Diffblue Cover docker image to use as part of your project pipeline is specified in your .gitlab-ci.yml
file (see Configuration). Tag variations are provided for each supported JDK version (see https://hub.docker.com/r/diffblue/cover-cli for details). An error in the naming of the docker image will cause a failure to download the docker file:
To resolve this issue, simply update the docker image name in your .gitlab-ci.yml
file.
If no tests are created, updated, or removed as part of your merge request, you may have selected the Diffblue Cover: Skip label in the merge request. This label is used to mark a merge request as requiring no tests to be written. See Labels.
Learning path recommendations after getting started with Cover Pipeline for GitLab
Once you're familiar with the topics covered under Cover Pipeline for GitLab, check out the following to help extend your unit test capabilities even further:
Specs & Reqs Specifications, prerequisites, and general project requirements are all covered in Specs & Reqs.
Cover Reports Integration
Additional coverage-reports
and upload
commands can be added to your pipeline in order to integrate Diffblue Cover: Baseline labelled CI runs into Cover Reports- see Generate and upload reports bundlesfor details.
dcover
Commands in a pipeline
Additional optional arguments can be used with the create
and validate
commands listed in your .gitlab-ci.yml
file for your project - see Commands & Arguments for details.
Output Codes Diffblue Cover performs a number of checks before, during, and after creating unit tests. Cover can generate a range of output codes during these checks to provide general information and highlight any issues. These codes and associated messages are output during the run, visible in the job output, and saved to the log files. See Output Codes for details.
Test Coverage See the How to improve code coverage topic for general information on ways to improve your overall coverage across your Java projects.
Developer Tools Diffblue Cover is also available as two developer tools - Cover Plugin for IntelliJ (write tests directly within the IntelliJ IDE) and Cover CLI (full command line tool) - both of which are bundled with Cover Pipeline for GitLab. These tools can be used by your development staff to automatically write tests during the development stages of a project, thereby allowing them to check for regressions and unplanned code behavior changes even before the code is committed back to GitLab. See the Free trial or Get started topics to check out the tools for yourself.