LogoLogo
diffblue.comFree trial
  • Discover Diffblue Cover
  • Get Started
    • What is Diffblue Cover?
    • Get started
      • Free trial
      • Get started - Cover Plugin
      • Get started - Cover CLI
      • Get started - Cover Pipeline
      • Get started - Cover Reports
    • Specs & Reqs
    • Reference Deployments
    • Licensing
      • Online license activation
      • Offline license activation
      • Diffblue License Manager
    • Update Cover
    • FAQs
    • Diffblue Learning
      • Get started
        • Just the basics
        • Free trial
        • Cover Plugin (IDE)
        • Cover CLI (Command Line)
      • Developer
        • Unit tests (IDE)
        • Unit tests (CLI)
        • Test coverage
      • DevOps
        • GitHub
        • GitLab
        • Other CI
      • Administrator
        • Admin - IntelliJ
        • Admin - CLI
        • Admin - Reports
      • Test coverage
        • Developer
        • Senior developer
        • Cover Reports Administrator
  • EVALUATION & ONBOARDING
    • Proof of Value
    • Jumpstart
      • Prerequisites for onboarding
      • Phase 1: Up and running
        • Module 1: Create your Cover unit test baseline
        • Module 2: Cover Pipeline CI integration
      • Phase 2: Developer productivity
        • Module 3: Getting started using Cover
        • Module 4: Introduction to Cover Reports
      • Phase 3: Advanced topics
        • Module 5: Speed up your test execution
        • Module 6: Getting more from Cover
  • Features
    • Cover Plugin
      • Writing tests
        • Gutter icons
        • Menu options
        • Run configurations
        • Cover Plugin tool window
        • Test Review
        • Test examples
        • Creating partial tests
        • Creating skeleton tests
        • Covering all enum values
        • Test insertion order
        • Diffblue Sandbox
        • Environment Check Cache
      • Project configuration
        • General dependencies
        • Test framework dependencies
      • Cover Plugin settings
        • Test Naming
        • Test Formatting
        • Spring configuration options
        • Method Annotations
        • Test Directory
        • Reset Cover Plugin settings
      • Cover Plugin admin
        • Core Maintenance
        • Cover Plugin toolbar menu
        • Cover Plugin status bar widget
        • Telemetry
        • Memory management
        • Using SonarQube with Cover Plugin
        • Log files
        • Troubleshooting
    • Cover CLI
      • Writing tests
        • Command summary
        • Test examples
        • Creating partial tests
        • Customizing test inputs
        • Customizing test setup
        • Test naming
        • Test formatting
        • Test insertion order
        • Patch files
        • Diffblue Sandbox
        • Operational behaviors
        • Test validation
      • Project configuration
        • Preflight checks
        • General dependencies
        • Test framework dependencies
        • Compiling your project successfully
          • Building a Maven project
          • Building a Gradle project
        • Configuring Cover to work with your project's build system
          • Quick guide
          • Configuration file in detail
          • Getting Started with Ant Projects
          • Default configuration
        • Mocking using Mockito
        • Spring profiles
        • Runtime environment
      • Commands & Arguments
        • Environment configuration for CI
        • Packages, classes, and methods
        • Quoting command line arguments
        • Argument files
      • Cover CLI admin
        • Core Maintenance
        • Telemetry
        • Memory management
        • Using Cover CLI in Eclipse
        • Using SonarQube with Cover CLI
        • Log files
        • Troubleshooting
      • Environment Configuration
    • Cover Pipeline
      • Cover Pipeline for CI
        • Quick Start - General
        • Quick Start - Jenkins
        • Quick Start - Azure Pipelines
        • Quick Start - AWS Codebuild
        • Integrating Diffblue Cover into CI on pull requests
        • Installation - VMs or CI run
      • Cover Pipeline for GitLab
        • Introduction
        • Installation and initial setup
        • GitLab workflow
        • Configuration
        • Troubleshooting
        • Related topics
      • Cover Pipeline for GitHub
        • Introduction
        • Installation and initial setup
        • GitHub workflow
        • Configuration
        • Troubleshooting
        • Related topics
    • Cover Reports
      • Cover Reports Contributor
        • Java project config (JaCoCo)
        • Generate and upload reports bundles
        • Authenticated uploads
      • Cover Reports User
        • Navigation
        • Dashboards
        • Telemetry data
        • Export API
        • Considerations
      • Cover Reports Administrator
        • Install and update Cover Reports
        • Configuration options
        • Database backup
        • SSO with Cover Reports
        • Uninstall Cover Reports
    • Cover Optimize
      • Get started - Cover Optimize
      • Cover Optimize & Gradle
      • Cover Optimize & Maven
      • Patch files
    • Cover Refactor
      • Get started - Cover Refactor
    • Cover Annotations
      • Mocking Annotations
      • Custom Input Annotations
      • Interesting Value Annotations
    • Output Codes
      • E - Environment Codes
      • L - License Codes
      • R - Reason Codes
      • T - Testability Codes
      • V - Validation Codes
      • Working with output codes
        • Working with code E020
        • Working with codes E057 to E065
        • Working with code E085
        • Working with code R005
        • Working with code R006
        • Working with code R008
        • Working with code R011
        • Working with code R012
        • Working with code R013
        • Working with code R026
        • Working with code R031
        • Working with code V003
    • Tutorials
      • How to measure test quality
      • How to improve code coverage
      • How to test a new feature
      • How to find regressions
      • How to use Diffblue Cover in test driven development (TDD)
      • How to write tests for Kotlin projects
      • Examples of tests created by Diffblue Cover
      • Best practices for testing private methods
  • Updates & Upgrades
    • Update Cover
    • Cover Editions
    • What's new
    • Release archive
      • 2025-05-01
      • 2025-04-02
      • 2025-04-01
      • 2025-03-02
      • 2025-03-01
      • 2025-02-02
      • 2025-02-01
      • 2025-01-02
      • 2025-01-01
      • 2024-12-02
      • 2024-12-01
      • 2024-11-02
      • 2024-11-01
      • 2024-10-02
      • 2024-10-01
      • 2024-09-02
      • 2024-09-01
      • 2024-08-02
      • 2024-08-01
      • 2024-07-04
      • 2024-07-03
      • 2024-07-01
      • 2024-06-02
      • 2024-06-01
      • 2024-05-02
      • 2024-05-01
      • 2024-04-02
      • 2024-04-01
      • 2024-03-02
      • 2024-03-01
      • 2024-02-02
      • 2024-02-01
      • 2024-01-02
      • 2024-01-01
      • 2023-12-02
      • 2023-12-01
      • 2023-11-03
      • 2023-11-02
      • 2023-11-01
      • 2023-10-02
      • 2023-10-01
      • 2023-09-02
      • 2023-09-01
      • 2023-08-02
      • 2023-08-01
      • 2023-07-03
      • 2023-07-02
      • 2023-07-01
      • 2023-06-02
      • 2023-06-01
      • 2023-05-02
      • 2023-05-01
      • 2023-04-02
      • 2023-04-01
      • 2023-03-02
      • 2023-03-01
      • 2023-02-02
      • 2023-02-01
      • 2023-01-02
      • 2023-01-01
      • 2022-12-02
      • 2022-12-01
      • 2022-11-02
      • 2022-11-01
      • 2022-10-03
      • 2022-10-02
      • 2022-10-01
      • 2022-09-02
      • 2022-09-01
      • 2022-08-05
      • 2022-08-03
      • 2022-08-02
      • 2022-08-01
      • 2022-07-02
      • 2022-07-01
      • 2022-06-02
      • 2022-06-01
      • 2022-05-02
      • 2022-05-01
      • 2022-04-02
      • 2022-04-01
      • 2022-03-02
      • 2022-03-01
      • 2022-02-02
      • 2022-02-01
      • 2022-01-02
      • 2022-01-01
      • 2021-06-02
      • 2021-02-01
  • Legal
    • Diffblue Legal
      • Diffblue End User License Agreement (EULA)
      • Cover Third Party Notices and/or Licenses
      • Privacy Notice
Powered by GitBook
On this page
  • Overview
  • 1. Building the project
  • 2. Downloading and activating Diffblue Cover CLI
  • 3. Running Diffblue Cover CLI to create tests
  • 4. Committing the created tests to a branch

Was this helpful?

Export as PDF
  1. Features
  2. Cover Pipeline
  3. Cover Pipeline for CI

Quick Start - Azure Pipelines

PreviousQuick Start - JenkinsNextQuick Start - AWS Codebuild

Last updated 1 year ago

Was this helpful?

Overview

This guide explains how to use Diffblue Cover to write tests in Azure Pipelines CI. This guide assumes that you have:

  • A Maven or Gradle project that:

    • Compiles

    • Does not have non-compiling or failing tests

    • Is stored in a Git repository which you are able to create a new Azure Pipeline YAML for

  • A basic understanding of Azure Pipelines

  • The ability to add secret variables to a pipeline

  • Diffblue Cover stored in the cloud for download along with the license key. See .

To integrate Diffblue Cover into your CI pipeline, we will guide you through creating a pipeline YAML that:

  1. Builds your project

  2. Downloads and activates Diffblue Cover CLI

  3. Runs Diffblue Cover to create tests

  4. Commits the created tests to a branch

The following sections provide more details for each of the above steps.

1. Building the project

Add a new pipeline to your project's CI. In the example below the pipeline is triggered on any pull request against the main branch. Make sure to check, and if necessary modify, the command to build your project. Running the project’s tests is not required, and you will save time by skipping them, but they do need to compile and pass.

name: Cover

trigger: none
pr:
  branches:
    include:
    - main

pool:
  vmImage: 'ubuntu-latest'

variables:
  BASH_ENV: "~/.profile"

jobs:
- job: update_diffblue_tests
  displayName: Update Diffblue Tests

  steps:
  - checkout: self
    persistCredentials: true

  - script: |
      git checkout "$(System.PullRequest.SourceBranch)"
    displayName: 'Checkout Branch'

  - script: |
      mvn --batch-mode --no-transfer-progress clean install -DskipTests
    displayName: 'Build Project'

2. Downloading and activating Diffblue Cover CLI

You need to give the CI run access to the Diffblue Cover files and activate the dcover license in order to write tests.

Add a new variable with the name DIFFBLUE_COVER_URL, set the value to the URL of the Diffblue Cover CLI release zip file and tick the "Keep this value secret" box. Then add a second variable with the name DIFFBLUE_COVER_LICENSE_KEY. Set the value to your Diffblue Cover license key and make this a secret variable as well.

Append the code for getting, unzipping and activating dcover to your pipeline YAML.

  - script: |
      mkdir dcover
      cd dcover
      curl --silent --show-error --location --output "diffblue-cover-cli.zip" "$(DIFFBLUE_COVER_URL)" 
      unzip -q "diffblue-cover-cli.zip"
      rm --force "diffblue-cover-cli.zip"
      echo "export PATH=$PATH:$PWD/" >> ~/.profile
      source ~/.profile
    displayName: 'Download Diffblue Cover'
  
  - script: |
      dcover activate "$(DIFFBLUE_COVER_LICENSE_KEY)"
    displayName: 'Activate Diffblue Cover'

This will put the Diffblue Cover files into the dcover directory in the root of the workspace. The Diffblue Cover files contain a script to invoke dcover which has the relative path dcover/dcover. Add this to your path so that you can call Diffblue Cover as dcover. In order to add Cover to your path ensure you include the "BASH_ENV" variable from the first step.

3. Running Diffblue Cover CLI to create tests

Now that Diffblue Cover is running in Azure Pipelines, you can use it to write tests. Append the following to your pipeline YAML. Note that the --batch option makes the output more suitable for CI, as it ensures the CI logs are not cluttered with progress bar output.

  - script: dcover create --batch
    displayName: 'Create tests using Diffblue Cover'

Push the changes so this workflow runs. Once successfully complete, you should expect to see output that looks like this in your "Create tests using Diffblue Cover" step:

INFO  Found 7 callable methods in 2 classes
INFO
INFO  Creating tests:
INFO  ---------------
...
INFO  All 5 created tests were successfully validated.

If you don't see this output, the call may need small modification for your project or dependencies adding until it works. The output gives you warnings along the way to guide you. See CLI Commands for more information.

Depending on the size of your module/project, creating tests could take a while. You may wish to restrict test creation to a single class by specifying its fully qualified name:

dcover create com.somepackage.SomeClass --batch

4. Committing the created tests to a branch

To see these new tests in the project you'll need to commit them and push back to the repository. You'll need to configure Git credentials to commit. We recommend creating a service account for this.

  - script: |
      git config user.name db-ci-bot
      git config user.email db-ci-bot@yourorg.com
    displayName: 'Config git user'

To commit the tests append the following to your pipeline YAML. This will check for any changes to Diffblue tests, add them to a commit and push to your branch.

  - script: |
      if [ -n "$(git status --short **/*DiffblueTest.java)" ]; then 
        git add **/*DiffblueTest.java
        git commit --message "Update Unit Tests for $(git rev-parse --short HEAD)"
        git push --set-upstream origin
      else
        echo "Nothing to commit"
      fi
    displayName: Stage, commit and push unit tests

Please note - be careful not to create an infinite CI loop here. This is because pushing a commit with updated tests will re-trigger the pipeline. To avoid this becoming a loop we recommend checking the author of each commit to ensure you are not creating tests for a commit authored by your Diffblue service account. This can be done by appending the following to the start of your pipeline YAML.

  - script: |
      LAST_NON_BOT_COMMIT="$(git rev-list -1 --author='^(?!db-ci-bot).*$' --perl-regexp HEAD --no-merges)"
      echo "Last non bot commit is $LAST_NON_BOT_COMMIT"
      LAST_COMMIT="$(git rev-list HEAD -1 --no-merges)"
      echo "Last commit is $LAST_COMMIT"
      if [[ "$LAST_NON_BOT_COMMIT" == "$LAST_COMMIT" ]]
      then
        echo "##vso[task.setvariable variable=last_bot_commit]0"
      else
        echo "##vso[task.setvariable variable=last_bot_commit]1"
      fi
    displayName: 'Check if last commit was bot commit'

Then append this condition to all of the subsequent jobs:

  condition: eq(variables.last_bot_commit, 0)

Your final pipeline YAML should look similar to:

name: Cover

trigger: none
pr:
  branches:
    include:
    - main

pool:
  vmImage: 'ubuntu-latest'

variables:
  BASH_ENV: "~/.profile"

jobs:
- job: update_diffblue_tests
  displayName: Update Diffblue Tests

  steps:
  - checkout: self
    persistCredentials: true

  - script: |
      git checkout "$(System.PullRequest.SourceBranch)"
    displayName: 'Checkout Branch'

  - script: |
      LAST_NON_BOT_COMMIT="$(git rev-list -1 --author='^(?!db-ci-bot).*$' --perl-regexp HEAD --no-merges)"
      echo "Last non bot commit is $LAST_NON_BOT_COMMIT"
      LAST_COMMIT="$(git rev-list HEAD -1 --no-merges)"
      echo "Last commit is $LAST_COMMIT"
      if [[ "$LAST_NON_BOT_COMMIT" == "$LAST_COMMIT" ]]
      then
        echo "##vso[task.setvariable variable=last_bot_commit]0"
      else
        echo "##vso[task.setvariable variable=last_bot_commit]1"
      fi
    displayName: 'Check if last commit was bot commit'

  - script: |
      mvn --batch-mode --no-transfer-progress clean install -DskipTests
    displayName: 'Build Project'
    condition: eq(variables.last_bot_commit, 0)

  - script: |
      mkdir dcover
      cd dcover
      curl --silent --show-error --location --output "diffblue-cover-cli.zip" "$(DIFFBLUE_COVER_URL)" 
      unzip -q "diffblue-cover-cli.zip"
      rm -f "diffblue-cover-cli.zip"
      echo "export PATH=$PATH:$PWD/" >> ~/.profile
      source ~/.profile
    displayName: 'Download Diffblue Cover'
    condition: eq(variables.last_bot_commit, 0)
  
  - script: |
      dcover activate "$(DIFFBLUE_COVER_LICENSE_KEY)"
    displayName: 'Activate Diffblue Cover'
    condition: eq(variables.last_bot_commit, 0)

  - script: |
      dcover create --batch
    displayName: 'Creating unit tests using Diffblue Cover'
    condition: eq(variables.last_bot_commit, 0)

  - script: |
      git config user.name db-ci-bot
      git config user.email db-ci-bot@yourorg.com
    displayName: 'Config git user'
    condition: eq(variables.last_bot_commit, 0)

  - script: |
      if [ -n "$(git status --short **/*DiffblueTest.java)" ]; then 
        git add **/*DiffblueTest.java
        git commit --message "Update Unit Tests for $(git rev-parse --short HEAD)"
        git push --set-upstream origin
      else
        echo "Nothing to commit"
      fi
    displayName: Stage, commit and push unit tests
    condition: eq(variables.last_bot_commit, 0)

This guide assumes that you have a URL with the Diffblue Cover CLI release zip and the license key for online activation during the CI run. See . If your license allows it you may wish to install Diffblue Cover with offline activation. See Licensing.

Push the changes so this workflow runs - ensure that you can see the successful activation of dcover in your "Activate Diffblue Cover" step before moving on. You will see a line starting with "Successfully activated key" if this was successful. If your Diffblue Cover did not successfully activate, please see Licensing or contact .

Installation
Installation
Diffblue Support