Testing in a CI/CD Pipeline Part 1: PR testing

Testing in a CI/CD Pipeline Part 1: PR testing

Why, how, when, where to perform testing in a CI/CD pipeline.


4 min read

There is no doubt the benefits of TDD ๐Ÿงช or test driven development. Follow this awesome Twitter thread and I bet if you not a fan of TDD you will surely become one.

TDD is a lot more than just the Unit testing. Lets us see how we can design our CI/CD pipeline to adapt to the TDD approach.

Info: ๐Ÿ’ก๐Ÿ’ก๐Ÿ’ก

I am using the Azure DevOps pipeline as CI/CD tool. But I believe you will be able to implement it in other CI/CD tool too. Understanding the mechanism is key.

Let's break down the pipeline into three component and see how to perform a test there.

1. Pull request testing in brief ๐Ÿ’ผ

PR testing is different from the system or unit testing. Let's see in brief (as the original intent of this guide is how to integrate it CI/CD pipeline)

  • In unit testing, tests are performed to measure the correctness of the system's individual smaller or unit component.
  • In contrast, PR testing is a testing stage where the functional test is performed on the complete scope branch which has to be merged.
  • PR testing in CI/CD pipeline works best and integrates easily if your system is build using a Microservice approach.

2. General mechanics ๐Ÿงฐ


  • To ensure the functionality code is working as expected.
  • It helps to restrict the merging of any unintended commits.
  • Ensures nothing breaks over collaboration.


  • The core of running a PR test is the same as running a Unit test.
  • All the tests which is used here are identical to Unit testing with a key difference of:
    1. Unit test are/should be performed locally whereas PR testing is performed over the remote.
    2. Code coverage of PR testing must be always more than the unit test because the intention of the unit test is to just check individual changes locally, whereas PR testing intention is to check for the complete branch which may more than one collaborative effort.
    3. It may be possible that you may not need a 100% passing of the Unit test (though this is not ideal) but PR testing must be 100% passing.


  • PR testing should be performed at the time of a PR completion or merge activity.


  • It is always performed at Remote Repository (eg Github, Gitlab).
  • It should be the first step of any CI/CD pipeline.

3. Implementation it in the Azure DevOps pipeline ๐Ÿš€

  • I am using python and pytest here. I am assuming that you will already have all the unit tests written and follow all conventions for pytest automatic test discovery.
  • All you need to run is a terminal command
- task: CmdLine@2
    script: |
      # If you have setup.py
      python3 -m pip install .
      pytest -vv
    workingDirectory: '$(System.DefaultWorkingDirectory)'
  • But the critical part here is How to make it automated? After all, this is part of CI/CD and the whole point of CI/CD is automation.
  • This is a two-step process in Azure DevOps:
    1. Create a new pipeline dedicated to running the unit tests. The trigger should be the same as its parent build or CI pipeline.
- develop

  vmImage: 'ubuntu-latest'


- task: UsePythonVersion@0
    versionSpec: '3.x'
    addToPath: true
    architecture: 'x64'

- task: CmdLine@2
    script: |
       # If you have setup.py
      python3 -m pip install .
      pytest -vv
    workingDirectory: '$(System.DefaultWorkingDirectory)'

Tip: ๐Ÿค๐Ÿค๐Ÿค

From above you can see I am using trigger as develop branch as this PR testing is intended for develop branch.

  1. Branch Policies:
    • For enabling PR triggers we need to branch policies in Azure DevOps
    • Goto Repo > Branch > Branch Policies > Build Validation

branch policies Img: Branch Policies build validation Img: Build Validation

  • We have to add a build validation policy for the PR trigger. Out of all two settings is key,
    1. Trigger: Must be automatic
    2. Policy requirement: Required

build-validation-policy.png Img: Build Validation Policy

  • See in the Build pipeline section, I am pointing to the PR testing pipeline that I showed earlier.


Img: How the PR testing works (see the marked item)

Did you find this article valuable?

Support Akash Desarda by becoming a sponsor. Any amount is appreciated!