In Azure DevOps you can set up a project so that a PR will fail if the test coverage is below a certain threshold. This ensures people aren’t writing code without testing it to the required level.
To do this is pretty simple, though the official documents, at the time of writing this are not that good.
There are 2 stages to this process:
- Define your code coverage settings
- Set your branch up to use these settings
1. Define your code coverage settings
Simple add a file with name “azurepipelines-coverage.yml” to the root of your project.
The contents of the file will need to be as below:
coverage: status: comments: on #Off by default. When on, details about coverage for each file changed will be posted as a pull request comment. diff: #diff coverage is code coverage only for the lines changed in a pull request. target: 70% #set this to a desired %. Default is 70%.
You can see other examples of the settings and their explanations in this GitHub repo.
2. Set your branch up to use these settings
- In Azure, go to your branch that you will be PR’ing into, e.g. develop or master.
- Click on the menu > Branch Policies
- Click on “Add status policy” in the “Require approval from additional services” section.
- There should be a “Code Coverage” option in the drop down.
- Click save
Now, when you create a PR you will see the code coverage checks in the side bar:
If the code coverage is below the level you specified in the “azurepipelines-coverage.yml” then the PR will fail. If you have comments turned on in the “azurepipelines-coverage.yml” file then you will see a comment on the PR like below: