如何为C#多项目配置Msbuild代码覆盖率阈值,未达标终止构建
Absolutely! The .NET ecosystem has great tools to replicate the Maven+Cobertura threshold enforcement workflow you’re familiar with. Given your specific environment (Visual Studio 2017 Pro, MSBuild 15.3, .NET Framework 4.6.2, NUnit 2.5.7), here are the most practical solutions:
Option 1: OpenCover + ReportGenerator (Open-Source, Free)
This is the most popular free stack for coverage reporting and threshold enforcement, and it works seamlessly with NUnit 2.x and MSBuild.
Step-by-Step Setup
Install NuGet Packages
Add these packages to your test project(s):OpenCover(a compatible version for your environment, e.g., 4.7.922)ReportGenerator(e.g., 4.8.12)- Ensure you already have
NUnit.Runners2.5.7 installed to run tests via command line.
Add MSBuild Target to Your Project/Solution
Edit your test project’s.csprojfile (or create aDirectory.Build.propsin your solution root to apply to all projects) and add this target to run coverage checks after build:<Target Name="EnforceCoverageThreshold" AfterTargets="Build"> <!-- Collect coverage data with OpenCover --> <Exec Command=""$(SolutionDir)packages\OpenCover.4.7.922\tools\OpenCover.Console.exe" -target:"$(SolutionDir)packages\NUnit.Runners.2.5.7\tools\nunit-console.exe" -targetargs:"$(TargetPath) /noshadow" -output:"$(SolutionDir)\Coverage\CoverageResults.xml" -filter:"+[YourProjectNamespace]* -[YourProjectNamespace.Tests]*" -register:user" /> <!-- Generate report and enforce threshold (fails build if below 80%) --> <Exec Command=""$(SolutionDir)packages\ReportGenerator.4.8.12\tools\ReportGenerator.exe" -reports:"$(SolutionDir)\Coverage\CoverageResults.xml" -targetdir:"$(SolutionDir)\Coverage\Report" -threshold:80 -failOnThreshold" /> </Target>- Adjust the
filterparameter to include your main project assemblies and exclude test assemblies. - Change
threshold:80to your desired minimum coverage percentage. The-failOnThresholdflag will make theExectask return a non-zero exit code if coverage drops below the threshold, causing MSBuild to fail the build.
- Adjust the
Option 2: JetBrains dotCover (Commercial, Integrated with ReSharper)
Since you’re already using ReSharper Ultimate 2017.2, dotCover (JetBrains’ coverage tool) is a natural fit—it integrates smoothly with your existing IDE and supports MSBuild command-line execution.
Step-by-Step Setup
Locate dotCover Command Line Tools
The dotCover CLI is included with ReSharper Ultimate 2017.2. You’ll find it at:C:\Program Files (x86)\JetBrains\ReSharper\v2017.2\dotCover\bin\dotCover.exeAdd MSBuild Target
Add this target to your project file to run coverage checks:<Target Name="EnforceCoverageWithDotCover" AfterTargets="Build"> <!-- Collect coverage data --> <Exec Command=""C:\Program Files (x86)\JetBrains\ReSharper\v2017.2\dotCover\bin\dotCover.exe" analyze /TargetExecutable="$(SolutionDir)packages\NUnit.Runners.2.5.7\tools\nunit-console.exe" /TargetArguments="$(TargetPath) /noshadow" /Output="$(SolutionDir)\Coverage\CoverageResults.dcvr" /Filters="+:* -:*.Tests"" /> <!-- Generate report and enforce threshold --> <Exec Command=""C:\Program Files (x86)\JetBrains\ReSharper\v2017.2\dotCover\bin\dotCover.exe" report /Source="$(SolutionDir)\Coverage\CoverageResults.dcvr" /Output="$(SolutionDir)\Coverage\Report.html" /ReportType=Html /Threshold=80 /FailIfCoverageIsLessThanThreshold" /> </Target>- The
/FailIfCoverageIsLessThanThresholdflag ensures the build fails if coverage doesn’t meet your threshold.
- The
Key Notes for Your Environment
- NUnit 2.5.7 Compatibility: Both OpenCover and dotCover support NUnit 2.x runners, so you won’t need to upgrade your test framework for this feature.
- MSBuild Integration: By adding these targets to your project files, the coverage check will run automatically whenever you build via Visual Studio or the
msbuildcommand line.
内容的提问来源于stack exchange,提问作者Abdul Jabbar

