Interface IDotnetTestHelper
- Namespace
- Invex.Atom.Module.Dotnet.Helpers
- Assembly
- Invex.Atom.Module.Dotnet.dll
Provides helper methods for running .NET tests and staging their results and code coverage reports.
public interface IDotnetTestHelper : IDotnetCliHelper, IBuildInfo, IDotnetToolInstallHelper, IReportsHelper, IBuildAccessor
- Inherited Members
Remarks
This interface extends IDotnetCliHelper for executing dotnet test commands,
IBuildInfo for build version information, IDotnetToolInstallHelper
for installing necessary tools, and IReportsHelper for generating build reports.
Methods
DotnetTestAndStage(RootedPath, DotnetTestAndStageOptions?, CancellationToken)
Runs tests for a .NET project specified by its path, then stages the test results and optionally code coverage reports.
Task<int> DotnetTestAndStage(RootedPath projectPath, DotnetTestAndStageOptions? options = null, CancellationToken cancellationToken = default)
Parameters
projectPathRootedPathThe full path to the project file (e.g., "tests/MyProject.Tests/MyProject.Tests.csproj").
optionsDotnetTestAndStageOptionsOptional. Configuration options for the testing and staging process.
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task<int>
A System.Threading.Tasks.Task<TResult> that returns the exit code of the
dotnet testcommand.
Remarks
This method performs the following steps:
- Configures `dotnet test` options, including TRX and HTML loggers, and optionally code coverage.
- Cleans up existing test result directories.
-
Applies version transformations to project files if
SetVersionsFromProviders is
true. - Executes the `dotnet test` command using Test(RootedPath, DotnetTestOptions?, ProcessRunOptions?, CancellationToken).
- Copies the generated HTML test report to the Atom publish directory.
- Generates a detailed test report using GenerateTestReport(string, string?, string?, string, bool).
-
If IncludeCoverage is
true, installs `dotnet-reportgenerator-globaltool` and generates code coverage reports. - Generates a code coverage report using GenerateCoverageReport(string, string?, string?, string, bool).
The BuildVersion is used for version transformation.
Exceptions
- StepFailedException
Thrown when the
dotnet testinvocation fails before producing any test results (e.g. a build error, invalid arguments, or a crashed test host), as opposed to the run completing with failing tests. This is detected by the absence of a TRX results file on a non-zero exit code. Failing tests do not throw and instead surface via the returned exit code and the generated test report.
DotnetTestAndStage(string, DotnetTestAndStageOptions?, CancellationToken)
Runs tests for a .NET project by its name, then stages the test results and optionally code coverage reports.
Task<int> DotnetTestAndStage(string projectName, DotnetTestAndStageOptions? options = null, CancellationToken cancellationToken = default)
Parameters
projectNamestringThe name of the project to test (e.g., "MyProject.Tests").
optionsDotnetTestAndStageOptionsOptional. Configuration options for the testing and staging process.
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task<int>
A System.Threading.Tasks.Task<TResult> that returns the exit code of the
dotnet testcommand.
Remarks
This method first locates the project file based on its name, then calls DotnetTestAndStage(RootedPath, DotnetTestAndStageOptions?, CancellationToken) with the resolved path.
Exceptions
- StepFailedException
Thrown if the project file cannot be located.
GenerateCoverageReport(string, string?, string?, string, bool)
Generates a code coverage report from a JSON summary file and adds it to the build report.
void GenerateCoverageReport(string projectName, string? configuration, string? framework, string coverageJsonFile, bool includeTitle = true)
Parameters
projectNamestringThe name of the project for which coverage was generated.
configurationstringThe build configuration (e.g., "Release", "Debug").
frameworkstringThe target framework (e.g., "net8.0").
coverageJsonFilestringThe path to the JSON code coverage summary file.
includeTitleboolWhether to include a title in the generated report data.
GenerateTestReport(string, string?, string?, string, bool)
Generates a test report from a TRX file and adds it to the build report.
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", Justification = "Deserialized type `TestRun` is manually preserved via DynamicallyAccessedMembers.")]
void GenerateTestReport(string projectName, string? configuration, string? framework, string trxFile, bool includeTitle = true)
Parameters
projectNamestringThe name of the project that was tested.
configurationstringThe build configuration (e.g., "Release", "Debug").
frameworkstringThe target framework (e.g., "net8.0").
trxFilestringThe path to the TRX test results file.
includeTitleboolWhether to include a title in the generated report data.