This document outlines the integration of Coverlet, a cross-platform code coverage framework for .NET, into the C# test execution process for the AgentSimMiddleware project.
Code coverage helps assess the quality of tests by indicating the percentage of code exercised by those tests. By integrating Coverlet with dotnet test, we can automatically generate coverage reports in a standard format (like Cobertura XML) as part of our continuous integration pipeline.
-
Add
Coverlet.CollectorNuGet Package: TheCoverlet.CollectorNuGet package has been added to thetests/csharp/AgentSimMiddleware.Tests/AgentSimMiddleware.Tests.csprojproject. This package enablesCoverletto collect code coverage data during test execution.<ItemGroup> <PackageReference Include="MSTest" Version="4.0.1" /> <PackageReference Include="Coverlet.Collector" Version="6.0.0" /> </ItemGroup>
-
C# Project Reference in Test Project: To ensure the test project can correctly build and test the C# components of
AgentSimMiddleware, theTestNetStandardLibrary.csprojhas been updated to include the C# source files fromsrc/csharp. This allowsAgentSimMiddleware.Tests.csprojto referenceTestNetStandardLibrary, which now encompasses all relevant C# logic including theUnityAdapter.The
TestNetStandardLibrary.csprojnow includes:<ItemGroup> <Compile Include="..\src\csharp\**\*.cs" /> </ItemGroup>
And the
AgentSimMiddleware.Tests.csprojreferences it:<ItemGroup> <ProjectReference Include="..\..\..\TestNetStandardLibrary\TestNetStandardLibrary.csproj" /> </ItemGroup>
-
Correct
TargetFramework: TheTargetFrameworkforAgentSimMiddleware.Tests.csprojhas been set tonet8.0for compatibility with the current .NET SDK and its dependencies.
To enable C# documentation coverage analysis, all C# projects are configured to generate XML documentation files (.xml) during their build process. This is achieved by setting the <GenerateDocumentationFile>true</GenerateDocumentationFile> property in the <PropertyGroup> section of each .csproj file.
The following projects now generate XML documentation:
benchmarker/benchmarker.csprojTestNetStandardLibrary/TestNetStandardLibrary.csprojtests/csharp/AgentSimMiddleware.Tests/AgentSimMiddleware.Tests.csproj
Verification steps confirmed that .xml files are generated in the respective bin/Debug (or bin/Release) folders alongside the compiled assemblies. For example:
TestNetStandardLibrary/bin/Debug/netstandard2.1/TestNetStandardLibrary.xmlbenchmarker/bin/Debug/net8.0/benchmarker.xmltests/csharp/AgentSimMiddleware.Tests/bin/Debug/net8.0/AgentSimMiddleware.Tests.xml
A PowerShell script, tools/run_csharp_tests.ps1, has been created to automate the execution of C# tests with code coverage collection.
-ProjectPath: (Optional) The relative path to the C# test project (AgentSimMiddleware.Tests.csproj). Defaults totests/csharp/AgentSimMiddleware.Tests/AgentSimMiddleware.Tests.csproj.-ResultsDirectory: (Optional) The relative path where test results and coverage reports should be stored. Defaults tocoverage_report/csharp.
The script performs the following actions:
- Cleans the specified test project (
dotnet clean). - Restores NuGet packages for the test project (
dotnet restore). - Executes
dotnet testwith the following key arguments:--collect:"XPlat Code Coverage": This argument instructsCoverletto collect code coverage data.--results-directory "<path>": Specifies the output directory for test results and coverage reports.--logger:"trx;LogFileName=test_results.trx": Generates a TRX format test results file.--logger:"html;LogFileName=test_results.html": Generates an HTML test results file.
To run the C# tests and collect code coverage, execute the PowerShell script from the project root:
./tools/run_csharp_tests.ps1Coverage reports (e.g., coverage.cobertura.xml) will be generated in a subdirectory within coverage_report/csharp. The exact path will include a GUID (e.g., coverage_report/csharp/<GUID>/coverage.cobertura.xml).
After running the script, verify the presence of a coverage.cobertura.xml file in the specified results directory to confirm successful coverage data generation.