Quality Gates and Build Failure


A Quality Gate is a check on a code quality fact that must be enforced before releasing and eventually, before committing to source control. A Quality Gate can be seen as a PASS/FAIL criterion for software quality.

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.

Notice that special red / yellow / green losange icons shows Quality Gates status: fail / warn / pass.

Quality Gates and Build Failure

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.

Creating and Customizing Quality Gates

What makes NDepend unique is that a Quality Gate is a 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.

Notice the special header if comment that defines the Quality Gate name and unit. The unit here is the string "%". It can be optionally repeated after after the thresholds for readability purpose.

A Quality Gate can also determine a measure based on the diff since baseline. For example the Quality Gate below is ensuring that the overall technical-debt doesn't grow beyond certain thresholds, since the baseline.
Notice that a <Description> tag can be used to describe the Quality Gate for non-developers.

// <QualityGate Name="New Debt since Baseline" Unit="man-days" />
failif value > 2 man-days
warnif value > 0 man-days
let debt = Issues.Sum(i => i.Debt)
let debtInBaseline = IssuesInBaseline.Sum(i => i.Debt)
select (debt - debtInBaseline).ToManDay()

// This Quality Gate fails if the estimated effort to fix new or worsened
// issues (what is called the *New Debt since Baseline*) is higher
// than 2 man-days.
// This Quality Gate warns if this estimated effort is positive.
// Debt documentation: http://www.ndepend.com/docs/technical-debt#Debt

Finally notice that when defining a Quality Gate threshold, the keyword value must be replaced with the keyword count if the LINQ query is returning rows instead of returning a scalar.
This is useful to make the Quality Gate result more informative when it makes sense.

Exploring Quality Gates Status

The set of Quality Gates status is queryable from C# LINQ queries.
The Dashboard summarizes the Quality Gates status. A single click generates a LINQ query that shows the detailed status for each Quality gate. Quality Gates statuses are also provided in HTML+js report.