Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<PackageVersion Include="Cake.Frosting" Version="6.3.0" />
<PackageVersion Include="Cake.Testing" Version="6.3.0" />
<PackageVersion Include="Castle.Core" Version="5.2.1" />
<PackageVersion Include="coverlet.collector" Version="10.0.1" />
<PackageVersion Include="coverlet.collector" Version="10.1.0" />
<PackageVersion Include="FakeItEasy" Version="9.0.1" />
<PackageVersion Include="JetBrains.Annotations" Version="2026.2.0" />
<PackageVersion Include="LiteDB" Version="5.0.21" />
<PackageVersion Include="log4net" Version="3.4.0" />
<PackageVersion Include="log4net" Version="3.5.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.12" />
<PackageVersion Include="Microsoft.AspNetCore.Components" Version="10.0.12" />
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="10.0.12" />
Expand Down Expand Up @@ -45,8 +45,8 @@
<PackageVersion Include="Polly" Version="8.8.0" />
<PackageVersion Include="Spectre.Console" Version="0.57.2" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="10.2.3" />
<PackageVersion Include="System.IO.Abstractions" Version="22.2.0" />
<PackageVersion Include="System.IO.Abstractions.TestingHelpers" Version="22.2.0" />
<PackageVersion Include="System.IO.Abstractions" Version="22.3.0" />
<PackageVersion Include="System.IO.Abstractions.TestingHelpers" Version="22.3.0" />
<PackageVersion Include="System.IO.Ports" Version="10.0.12" />
<PackageVersion Include="System.Reactive" Version="7.0.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,26 @@ IEnumerable<FilePath> TestProjects

bool GenerateCoverageReport => true;

/// <summary>
/// Gets a value that indicates whether <c>dotnet test</c> is run in Microsoft.Testing.Platform (MTP) mode
/// instead of VSTest mode.
/// </summary>
/// <value>
/// <see langword="true" /> to pass Microsoft.Testing.Platform arguments to <c>dotnet test</c>; otherwise,
/// <see langword="false" /> to pass VSTest arguments. The default is <see langword="false" />.
/// </value>
/// <remarks>
/// This setting only selects the arguments passed to <c>dotnet test</c>; it does not switch the test mode of the
/// .NET SDK. The repository must enable the Microsoft.Testing.Platform mode in its <c>global.json</c>
/// (<c>"test": { "runner": "Microsoft.Testing.Platform" }</c>). The test projects must reference
/// <c>Microsoft.Testing.Extensions.TrxReport</c> and, if <see cref="GenerateCoverageReport" /> is
/// <see langword="true" />, <c>Microsoft.Testing.Extensions.CodeCoverage</c>. In this mode, the TRX file
/// (<c>&lt;Project&gt;.trx</c>) is written to <see cref="ICakeBuildContext.CodeCoverageDir" /> instead of
/// <see cref="ICakeBuildContext.TestResultsDir" />, and the coverage report is written in Cobertura format to
/// <c>&lt;Project&gt;.cobertura.xml</c> in <see cref="ICakeBuildContext.CodeCoverageDir" />. Because these files are
/// named after the test project file, the file names of all test projects must be unique.
/// </remarks>
bool UseMicrosoftTestingPlatform => false;

DirectoryPath TestSourceDir => Context.RootDir.Combine("tests");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Cake.Common.Diagnostics;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Test;
using Cake.Core;
using Cake.Core.IO;
using CreativeCoders.CakeBuild.Tasks.Templates.Settings;
using JetBrains.Annotations;
Expand All @@ -16,6 +17,10 @@ protected override Task RunAsyncCore(T context, ITestTaskSettings taskSettings)

context.Information($"Found {testProjects.Length} test project(s)");

context.Information(taskSettings.UseMicrosoftTestingPlatform
? "Test platform: Microsoft Testing Platform"
: "Test platform: VSTest");

foreach (var testProject in testProjects)
{
context.Information($"Test project found: {testProject.GetFilename()}");
Expand All @@ -27,25 +32,71 @@ protected override Task RunAsyncCore(T context, ITestTaskSettings taskSettings)
return Task.CompletedTask;
}

/// <summary>
/// Customizes the <c>dotnet test</c> settings for a single test project before the test run starts.
/// </summary>
/// <param name="context">The build context.</param>
/// <param name="dotNetBuildSettings">The <c>dotnet test</c> settings to customize.</param>
/// <remarks>
/// The hook is called after the base settings (configuration, build, results directory and, in VSTest mode,
/// loggers and collectors) have been applied. If <see cref="ITestTaskSettings.UseMicrosoftTestingPlatform" /> is
/// <see langword="true" />, the Microsoft.Testing.Platform arguments are appended after the hook has run; an
/// <see cref="Cake.Core.Tooling.ToolSettings.ArgumentCustomization" /> set by the hook is kept and runs before
/// these arguments. In this mode, do not set <see cref="DotNetTestSettings.Loggers" /> or
/// <see cref="DotNetTestSettings.Collectors" />, because <c>dotnet test</c> does not support them.
/// </remarks>
protected virtual void ApplyDotNetTestSettings(T context, DotNetTestSettings dotNetBuildSettings) { }

private DotNetTestSettings CreateDotNetBuildSettings(T context, FilePath testProject,
ITestTaskSettings testSettings)
{
var testResultFile =
context.TestResultsDir.CombineWithFilePath($"{testProject.GetFilenameWithoutExtension()}.trx");

var dotNetTestSettings = new DotNetTestSettings
{
Configuration = context.BuildConfiguration,
NoBuild = context.HasExecutedTask(typeof(BuildTask<T>)),
Loggers = [$"trx;LogFileName={testResultFile}"],
Collectors = testSettings.GenerateCoverageReport ? ["XPlat Code Coverage"] : [],
ResultsDirectory = context.CodeCoverageDir
};

if (testSettings.UseMicrosoftTestingPlatform)
{
dotNetTestSettings.PathType = DotNetTestPathType.Project;
}
else
{
var testResultFile =
context.TestResultsDir.CombineWithFilePath($"{testProject.GetFilenameWithoutExtension()}.trx");

dotNetTestSettings.Loggers = [$"trx;LogFileName={testResultFile}"];
dotNetTestSettings.Collectors = testSettings.GenerateCoverageReport ? ["XPlat Code Coverage"] : [];
}

ApplyDotNetTestSettings(context, dotNetTestSettings);

if (testSettings.UseMicrosoftTestingPlatform)
{
var customArgumentCustomization = dotNetTestSettings.ArgumentCustomization;

dotNetTestSettings.ArgumentCustomization = args =>
{
args = customArgumentCustomization?.Invoke(args) ?? args;

args.Append("--report-trx");
args.Append("--report-trx-filename");
args.AppendQuoted($"{testProject.GetFilenameWithoutExtension()}.trx");

if (testSettings.GenerateCoverageReport)
{
args.Append("--coverage");
args.Append("--coverage-output-format");
args.Append("cobertura");
args.Append("--coverage-output");
args.AppendQuoted($"{testProject.GetFilenameWithoutExtension()}.cobertura.xml");
}

return args;
};
}

if (dotNetTestSettings.NoBuild)
{
context.Information("Skip build");
Expand Down
Loading
Loading