In this lesson, you will learn how to enforce quality standards by configuring your CI/CD pipeline to automatically reject builds that fail to meet your testing and code coverage requirements.
The Concept of a Build Gate
A "Build Gate" is an automated checkpoint in your CI/CD pipeline that prevents substandard code from progressing to deployment. Even if your code compiles perfectly, it may lack sufficient test coverage or contain regression errors. By implementing gates, you ensure that every merge request adheres to the project's "Definition of Done."
Failing on Test Results
The most fundamental gate is the Test Execution Gate. When you run dotnet test in your pipeline, the command returns a non-zero exit code if any tests fail. Most CI providers (like GitHub Actions or Azure DevOps) treat this exit code as a signal to abort the pipeline immediately, effectively blocking the build.
Enforcing Coverage Thresholds
Code coverage gates are more nuanced. They prevent the "coverage debt" that accumulates when developers add new logic without accompanying unit tests. Using tools like Coverlet, you can generate reports and enforce a strict percentage.
For example, you can configure your test project to fail if the coverage drops below a specific threshold:
<!-- In your .csproj or coverlet configuration -->
<PropertyGroup>
<CollectCoverage>true</CollectCoverage>
<Threshold>80</Threshold>
<ThresholdType>line</ThresholdType>
<ThresholdStat>total</ThresholdStat>
</PropertyGroup>
When you run dotnet test /p:CollectCoverage=true, Coverlet calculates the coverage. If the result is 79%, the process exits with a failure code, triggering the CI gate to stop the build.
Why This Matters
As you reach the advanced stages of your development lifecycle, your test suite likely includes complex integrations—using Testcontainers for databases or mocking external services. A build gate acts as a safety net, ensuring that these expensive integration tests aren't ignored. It forces the team to maintain the integrity of the system by ensuring that changes don't silently degrade the quality of your services.
Key Takeaway: Build gates transform "best practices" into mandatory requirements, ensuring that no code reaches production unless it passes your automated test suites and meets your defined coverage thresholds.