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
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ private async Task ExecuteChunksInTopologicalOrderAsync(
bool usesAppDomains,
int parallelWorkers)
{
_testRunCancellationToken?.ThrowIfCancellationRequested();

int chunkCount = graph.ParallelChunks.Length;
int[] pendingPrerequisites = new int[chunkCount];
var dependents = new List<int>[chunkCount];
Expand Down Expand Up @@ -147,10 +149,9 @@ private async Task ExecuteChunksInTopologicalOrderAsync(
var resourceLockManager = new ResourceLockManager();
var tasks = new List<Task>(effectiveWorkers);

// Once workers are scheduled, let them observe cancellation so we always await their completion.
for (int worker = 0; worker < effectiveWorkers; worker++)
{
_testRunCancellationToken?.ThrowIfCancellationRequested();

tasks.Add(_taskFactory(async () =>
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ .. parallelizableTestSet
var tasks = new List<Task>();
var resourceLockManager = new ResourceLockManager();

_testRunCancellationToken?.ThrowIfCancellationRequested();

// Once workers are scheduled, let them observe cancellation so we always await their completion.
for (int i = 0; i < parallelWorkers; i++)
{
_testRunCancellationToken?.ThrowIfCancellationRequested();

tasks.Add(_taskFactory(async () =>
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ This package extends Microsoft.Testing.Platform with:

Enable the report via the `--report-ctrf` command line option. The report file name can be overridden with `--report-ctrf-filename <name>.json`.

## Exception diagnostics

The `message` field preserves the framework's explanation or the exception's top-level message. For nested and aggregate exceptions, `trace` also includes the inner causes. Diagnostic text remains subject to the existing capture limits.

## Identity and attachment semantics

- `reportId` identifies one report document. Each physical report has its own UUID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ This package extends Microsoft.Testing.Platform with:

Enable the report via the `--report-html` command line option. The report file name can be overridden with `--report-html-filename <name>.html`.

## Exception diagnostics

Expanded failure details preserve the framework's explanation or the exception's top-level message. For nested and aggregate exceptions, the stack trace also includes the inner causes. Diagnostic text remains subject to the existing capture limits.

## Documentation

For comprehensive documentation, see <https://aka.ms/testingplatform>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ This package extends Microsoft.Testing.Platform with:

Enable the report via the `--report-junit` command line option. The report file name can be overridden with `--report-junit-filename <name>.xml`.

## Exception diagnostics

Failure and error messages preserve the framework's explanation or the exception's top-level message. Their text also includes the inner causes of nested and aggregate exceptions. Diagnostic text remains subject to the existing capture limits.

## Documentation

For comprehensive documentation, see <https://aka.ms/testingplatform>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ internal static CapturedExceptionDetails ExtractExceptionDetails(TestNodeStatePr
if (exception is not null)
{
errorMessage ??= exception.Message;
stackTrace = exception.StackTrace;
// StackTrace alone omits inner causes, including the other branches of an AggregateException.
stackTrace = exception.InnerException is null ? exception.StackTrace : exception.ToString();
exceptionType = exception.GetType().FullName;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Text.Json;

namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;

[TestClass]
public class ReportExceptionDiagnosticsTests : AcceptanceTestBase<NopAssetFixture>
{
[TestMethod]
public async Task Reports_NestedExceptions_PreserveAssociatedCausesAndExplanations()
{
using TempDirectory directory = new();
using TestAsset asset = await TestAsset.GenerateAssetAsync(
AssetName,
TestCode
.PatchTargetFrameworks([TargetFrameworks.NetCurrent])
.PatchCodeWithReplace("$MicrosoftTestingExtensionsCtrfReportVersion$", MicrosoftTestingExtensionsCtrfReportVersion)
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion)
.PatchCodeWithReplace("$MicrosoftTestingExtensionsJUnitReportVersion$", MicrosoftTestingExtensionsJUnitReportVersion),
directory);
await DotnetCli.RunAsync(
$"build \"{asset.TargetAssetPath}\" -c Release",
cancellationToken: TestContext.CancellationToken);

string resultsDirectory = Path.Combine(directory.Path, "results");
var testHost = TestInfrastructure.TestHost.LocateFrom(asset.TargetAssetPath, AssetName, TargetFrameworks.NetCurrent);
TestHostResult result = await testHost.ExecuteAsync(
$"--report-ctrf --report-ctrf-filename diagnostics.ctrf.json"
+ " --report-html --report-html-filename diagnostics.html"
+ " --report-junit --report-junit-filename diagnostics.xml"
+ $" --results-directory \"{resultsDirectory}\"",
cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCode.AtLeastOneTestFailed);

using var ctrf = JsonDocument.Parse(File.ReadAllText(Path.Combine(resultsDirectory, "diagnostics.ctrf.json")));
JsonElement[] ctrfTests = [.. ctrf.RootElement.GetProperty("results").GetProperty("tests").EnumerateArray()];
Assert.HasCount(5, ctrfTests);
Dictionary<string, JsonElement> ctrfById = ctrfTests.ToDictionary(test => test.GetProperty("testId").GetString()!, StringComparer.Ordinal);

string html = File.ReadAllText(Path.Combine(resultsDirectory, "diagnostics.html"));
Match payload = Regex.Match(html, """<script id="mtp-data" type="application/json">(?<data>.*?)</script>""", RegexOptions.Singleline);
Assert.IsTrue(payload.Success, "The HTML report must contain its consumed result data.");
using var htmlData = JsonDocument.Parse(payload.Groups["data"].Value);
JsonElement[] htmlTests = [.. htmlData.RootElement.GetProperty("tests").EnumerateArray()];
Assert.HasCount(5, htmlTests);
Dictionary<string, JsonElement> htmlById = htmlTests.ToDictionary(test => test.GetProperty("uid").GetString()!, StringComparer.Ordinal);

var junit = XDocument.Load(Path.Combine(resultsDirectory, "diagnostics.xml"));
XElement[] junitTests = [.. junit.Descendants("testcase")];
Assert.HasCount(5, junitTests);
Dictionary<string, XElement> junitById = junitTests.ToDictionary(
test => test.Element("properties")!.Elements("property").Single(property => (string?)property.Attribute("name") == "uid").Attribute("value")!.Value,
StringComparer.Ordinal);

AssertFailure("nested", "NestedFailure", "Invocation failed.", "System.InvalidOperationException", "failed",
["System.ArgumentException: [INNER_CAUSE] Service rejected <401> & \"credential\"."]);
AssertFailure("aggregate", "AggregateFailure", "Parallel failure. (Read failed.) ([AUTH_CAUSE] Credential rejected.)", "System.AggregateException", "errored",
["System.IO.IOException: [READ_CAUSE] Missing file.", "System.ArgumentException: [AUTH_CAUSE] Credential rejected."]);
AssertFailure("explained", "ExplainedFailure", "Framework explanation.", "System.InvalidOperationException", "failed",
["System.IO.IOException: [EXPLAINED_CAUSE] Missing file."]);
AssertFailure("simple", "SimpleFailure", "Simple failure.", "System.InvalidOperationException", "failed", []);

Assert.AreEqual("PassingTest", ctrfById["passed"].GetProperty("name").GetString());
Assert.AreEqual("passed", ctrfById["passed"].GetProperty("status").GetString());
Assert.AreEqual("PassingTest", htmlById["passed"].GetProperty("displayName").GetString());
Assert.AreEqual("passed", htmlById["passed"].GetProperty("outcome").GetString());
Assert.AreEqual("PassingTest", (string?)junitById["passed"].Attribute("name"));
Assert.IsNull(junitById["passed"].Element("failure"));
Assert.IsNull(junitById["passed"].Element("error"));

void AssertFailure(string uid, string name, string message, string exceptionType, string htmlOutcome, string[] causes)
{
JsonElement ctrfTest = ctrfById[uid];
Assert.AreEqual(name, ctrfTest.GetProperty("name").GetString());
Assert.AreEqual("failed", ctrfTest.GetProperty("status").GetString());
Assert.AreEqual(message, ctrfTest.GetProperty("message").GetString());

JsonElement htmlTest = htmlById[uid];
Assert.AreEqual(name, htmlTest.GetProperty("displayName").GetString());
Assert.AreEqual(htmlOutcome, htmlTest.GetProperty("outcome").GetString());
Assert.AreEqual(message, htmlTest.GetProperty("errorMessage").GetString());
Assert.AreEqual(exceptionType, htmlTest.GetProperty("exceptionType").GetString());

XElement junitTest = junitById[uid];
Assert.AreEqual(name, (string?)junitTest.Attribute("name"));
XElement? failure = junitTest.Element(htmlOutcome == "errored" ? "error" : "failure");
Assert.IsNotNull(failure);
Assert.AreEqual(message, (string?)failure.Attribute("message"));
Assert.AreEqual(exceptionType, (string?)failure.Attribute("type"));

if (causes.Length == 0)
{
Assert.IsFalse(ctrfTest.TryGetProperty("trace", out _));
Assert.IsFalse(htmlTest.TryGetProperty("stackTrace", out _));
Assert.AreEqual($"{exceptionType}: {message}", failure.Value);
return;
}

Assert.IsTrue(ctrfTest.TryGetProperty("trace", out JsonElement ctrfTrace), $"CTRF result '{uid}' must retain its nested exception trace.");
Assert.IsTrue(htmlTest.TryGetProperty("stackTrace", out JsonElement htmlTrace), $"HTML result '{uid}' must retain its nested exception trace.");
foreach (string cause in causes)
{
Assert.Contains(cause, ctrfTrace.GetString()!, $"CTRF result '{uid}' lost a cause.");
Assert.Contains(cause, htmlTrace.GetString()!, $"HTML result '{uid}' lost a cause.");
Assert.Contains(cause, failure.Value, $"JUnit result '{uid}' lost a cause.");
}
}
}

private const string AssetName = "ReportExceptionDiagnostics";
private const string TestCode = """
#file ReportExceptionDiagnostics.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Testing.Extensions.CtrfReport" Version="$MicrosoftTestingExtensionsCtrfReportVersion$" />
<PackageReference Include="Microsoft.Testing.Extensions.HtmlReport" Version="$MicrosoftTestingPlatformVersion$" />
<PackageReference Include="Microsoft.Testing.Extensions.JUnitReport" Version="$MicrosoftTestingExtensionsJUnitReportVersion$" />
</ItemGroup>
</Project>

#file Program.cs
using Microsoft.Testing.Extensions;
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestFramework;

ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args);
builder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, _) => new DiagnosticFramework());
builder.AddCtrfReportProvider();
#pragma warning disable TPEXP
builder.AddHtmlReportProvider();
builder.AddJUnitReportProvider();
#pragma warning restore TPEXP
using ITestApplication app = await builder.BuildAsync();
return await app.RunAsync();

public sealed class DiagnosticFramework : ITestFramework, IDataProducer
{
public string Uid => nameof(DiagnosticFramework);
public string Version => "1.0.0";
public string DisplayName => nameof(DiagnosticFramework);
public string Description => nameof(DiagnosticFramework);
public Type[] DataTypesProduced => [typeof(TestNodeUpdateMessage)];
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context)
=> Task.FromResult(new CreateTestSessionResult { IsSuccess = true });
public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context)
=> Task.FromResult(new CloseTestSessionResult { IsSuccess = true });

public async Task ExecuteRequestAsync(ExecuteRequestContext context)
{
await PublishAsync("nested", "NestedFailure", new FailedTestNodeStateProperty(
new InvalidOperationException("Invocation failed.", new ArgumentException("[INNER_CAUSE] Service rejected <401> & \"credential\"."))));
await PublishAsync("aggregate", "AggregateFailure", new ErrorTestNodeStateProperty(
new AggregateException("Parallel failure.",
new InvalidOperationException("Read failed.", new IOException("[READ_CAUSE] Missing file.")),
new ArgumentException("[AUTH_CAUSE] Credential rejected."))));
await PublishAsync("explained", "ExplainedFailure", new FailedTestNodeStateProperty(
new InvalidOperationException("Outer message.", new IOException("[EXPLAINED_CAUSE] Missing file.")), "Framework explanation."));
await PublishAsync("simple", "SimpleFailure", new FailedTestNodeStateProperty(new InvalidOperationException("Simple failure.")));
await PublishAsync("passed", "PassingTest", PassedTestNodeStateProperty.CachedInstance);
context.Complete();

Task PublishAsync(string uid, string displayName, TestNodeStateProperty state)
=> context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
context.Request.Session.SessionUid,
new TestNode { Uid = uid, DisplayName = displayName, Properties = new PropertyBag(state) }));
}
}
""";

public TestContext TestContext { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using AwesomeAssertions;
using AwesomeAssertions.Execution;

using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
Expand Down Expand Up @@ -509,6 +510,87 @@ public async Task RunTestsForMultipleSourcesShouldRunEachTestJustOnce()

#region Parallel tests

public Task RunTests_CancelledWhileStartingParallelWorkers_WaitsForScheduledWorkers()
=> RunTestsCancelledWhileStartingWorkersAsync(withDependencies: false);

public Task RunTests_CancelledWhileStartingDependencyWorkers_WaitsForScheduledWorkers()
=> RunTestsCancelledWhileStartingWorkersAsync(withDependencies: true);

private async Task RunTestsCancelledWhileStartingWorkersAsync(bool withDependencies)
{
UnitTestElement[] tests = ToUnitTestElements(
GetTestCase(typeof(DummyTestClassForParallelize), nameof(DummyTestClassForParallelize.TestMethod1)),
GetTestCase(typeof(DummyTestClassForParallelize2), nameof(DummyTestClassForParallelize2.TestMethod1)));
if (withDependencies)
{
tests[1].Dependencies =
[
new TestDependencyInfo(typeof(DummyTestClassForParallelize).FullName, nameof(DummyTestClassForParallelize.TestMethod1), proceedOnFailure: false),
];
}

_runContext.MockRunSettings.Setup(rs => rs.SettingsXml).Returns(
"""
<RunSettings>
<RunConfiguration>
<DisableAppDomain>True</DisableAppDomain>
</RunConfiguration>
<MSTest>
<Parallelize>
<Workers>2</Workers>
<Scope>MethodLevel</Scope>
</Parallelize>
</MSTest>
</RunSettings>
""");
MSTestSettings.PopulateSettings(_runContext.RunSettings?.SettingsXml, _mockMessageLogger.Object.ToAdapterMessageLogger(), null);

var releaseWorker = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
int scheduledWorkers = 0;
var manager = new TestExecutionManager(action =>
{
if (++scheduledWorkers == 1)
{
_cancellationToken.Cancel();
return releaseWorker.Task;
}

return action();
});
Task run = manager.RunTestsAsync(
tests,
CurrentDeploymentContext,
_frameworkHandle.ToAdapterMessageLogger(),
TestResultRecorder,
new TestElementFilterProvider(_runContext),
_cancellationToken);

using var assertions = new AssertionScope();
try
{
scheduledWorkers.Should().BeGreaterThan(0);
run.IsCompleted.Should().BeFalse("cancellation must not abandon an already scheduled worker");
}
finally
{
releaseWorker.TrySetResult(true);
}

const int WorkerCleanupTimeoutMilliseconds = 60_000;
if (await Task.WhenAny(run, Task.Delay(WorkerCleanupTimeoutMilliseconds)) != run)
{
throw new TimeoutException("Parallel workers did not finish after their release.");
}

Func<Task> completeRun = () => run;
await completeRun.Should().NotThrowAsync();

scheduledWorkers.Should().Be(2);
_frameworkHandle.TestCaseStartList.Should().BeEmpty();
_frameworkHandle.ResultsList.Should().BeEmpty();
_frameworkHandle.TestCaseEndList.Should().BeEmpty();
}

public async Task RunTestsForTestShouldRunTestsInParallelWhenEnabledInRunsettings()
{
TestCase testCase11 = GetTestCase(typeof(DummyTestClassForParallelize), "TestMethod1");
Expand Down
Loading
Loading