Implement a Quality Gate strategy in your DevOps to fail the build when required

What is a quality gate?

A quality gate is a code quality goal. A quality goal must be enforced before releasing and eventually, before committing to source control.

A dozen of default Quality Gates are proposed by NDepend related to measures like technical debt amount, code coverage or amount of issues with particular severity.

Default NDepend Quality Gates

A quality gate is not a rule.

  • A rule executed against some code produces some issues.
  • A quality gate executed against some code produces a state in the range [ PASS, WARN , FAIL].

Fail the build

Quality Gates can be used to fail the build when certain criteria are not-verified.
When at least one quality gate fails, NDepend.Console.exe returns a non-zero value that can be used to fail the build.

Quality Gates Build Failure

In the Azure DevOps / TFS extension there is the setting Stop the build when at least one Quality Gate fails.

Azure DevOps / TFS Quality Gates Build Failure

Create your own quality gates

What makes NDepend unique is that a quality gate is a textual C# LINQ Query that can be easily created, edited and customized. For example if you wish to enforce a certain amount of code coverage through a quality gate, you can just write:

// <QualityGate Name="Percentage Code Coverage" Unit="%" />
failif value < 70%
warnif value < 80%

Notice also the special clauses failif (mandatory clause) and warnif (optional clause) that are specific to NDepend CQLinq. codebase.PercentageCoverage is actually a C# statement that NDepend evaluates.

Both the quality gate fail/warn thresholds and its logic are embedded into a short textual query that can be easily edited. The quality gate logic can query anything:

  • the code model
  • the code coverage data
  • the code delta since the baseline
  • the issues obtained from executing the rules
  • the issues' technical-debt estimations

Browse default quality gates source code here.

More about writing your own quality gates with C# LINQ queries can be found here.

You can watch this 3 minutes video to get started with the NDepend quality gate: