A .NET CLI tool that determines unit test code coverage specifically for newly added or modified code since a given commit. This helps ensure that new changes are adequately tested without being skewed by the overall project coverage percentage.
- .NET 8.0 SDK (or whichever version you compiled the tool with)
- Git installed and accessible from the command line
- Runs
git diff -U0 <base-ref>to find all added or modified lines in the target repository. - Runs
dotnet test --collect:"XPlat Code Coverage"on the target repository to generate a Cobertura XML report. - Intersects the modified lines from Git with the coverage metrics from Cobertura to calculate exactly how much of your new code is covered by tests.
You can run the tool directly from its source code directory. Open a terminal and navigate to the DiffCoverageTool folder, then use dotnet run:
# Syntax
dotnet run --project <path-to-DiffCoverageTool.csproj> "<path-to-target-repo>" <base-git-ref>
# Example: Run against your working directory, comparing against your last commit (HEAD~1)
dotnet run --project DiffCoverageTool.csproj "C:\path\to\your\project" HEAD~1
# Example: To check uncommitted changes only against HEAD
dotnet run --project DiffCoverageTool.csproj "C:\path\to\your\project" HEADIf you want to compile it into a standalone executable:
# Build the tool
dotnet build -c Release
# Run the compiled executable directly
.\bin\Release\net8.0\DiffCoverageTool.exe "C:\path\to\your\project" HEAD~1- "No modified files found": This means
git diff -U0 <base-ref>didn't return any changes. Ensure you have made changes relative to the base reference. - "Could not find coverage.cobertura.xml": The tool executes
dotnet test --collect:"XPlat Code Coverage". If Coverlet doesn't generate this file, make sure your target project actually has tests and referencescoverlet.collector(which is included by default viadotnet new xunit). - "No coverable new lines found": If your coverage file is generated but reports 0 lines, ensure that your application code and your test code are in separate projects (e.g.,
MyApp.CoreandMyApp.Tests). By default, Coverlet excludes the assembly containing the tests from coverage reporting.