Skip to content

InnerException's are lost in the Testing Platform output #3783

Description

Just been wrapping some exceptions to provide more context to users (e.g. A TearDown method failed, etc.) and noticed that the output is losing some context - And makes it almost impossible for a user to see where an actual crash was.

Reproduction: Wrap some exceptions in a new exception where the original exception is passed in as an InnerException.

Throw your wrapped exception, and you won't see any message or stacktrace from your original exception in the testing platform output.

Repro code:

using Microsoft.Testing.Extensions;
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Capabilities;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestFramework;
using Microsoft.Testing.Platform.TestHost;

var builder = await TestApplication.CreateBuilderAsync(args);
builder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, _) => new DummyAdapter());
var app = await builder.BuildAsync();
return await app.RunAsync();

internal class DummyAdapter() : ITestFramework, IDataProducer
{
    public string Uid => nameof(DummyAdapter);

    public string Version => string.Empty;

    public string DisplayName => string.Empty;

    public string Description => string.Empty;

    public Type[] DataTypesProduced => new[] { typeof(TestNodeUpdateMessage) };

    public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context) => Task.FromResult(new CloseTestSessionResult { IsSuccess = true });

    public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context) => Task.FromResult(new CreateTestSessionResult { IsSuccess = true });

    public Task ExecuteRequestAsync(ExecuteRequestContext context)
    {
        try
        {
            MyService.DoSomething();
        }
        catch (Exception e)
        {
            context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(new SessionUid("1"), new TestNode()
            {
                Uid = "2",
                DisplayName = "Blah",
                Properties = new PropertyBag(new FailedTestNodeStateProperty(e))
            }));
        }
     
        context.Complete();
        
        return Task.CompletedTask;
    }

    public Task<bool> IsEnabledAsync() => Task.FromResult(true);
}

public class MyService
{
    public static void DoSomething()
    {
        try
        {
            InnerDoSomething();
        }
        catch (Exception e)
        {
            throw new WrappedException("Service failed!", e);
        }
    }

    private static void InnerDoSomething()
    {
        throw new Exception("Error code 488");
    }
}

public class WrappedException(string message, Exception innerException) : Exception(message, innerException);

Actual output:

failed Blah (0ms)
  Service failed!
  Stack Trace:
    at MyService.DoSomething() in C:\git\TestPlatformSingleFileCoverageExceptionRepro\TestPlatformSingleFileCoverageExceptionRepro\TestPlatformSingleFileCoverageExceptionRepro\Program.cs:64
    at DummyAdapter.ExecuteRequestAsync(ExecuteRequestContext context) in C:\git\TestPlatformSingleFileCoverageExceptionRepro\TestPlatformSingleFileCoverageExceptionRepro\TestPlatformSingleFileCoverageExceptionRepro\Program.cs:34

Expected:

To include the message of Error code 488 and the stacktrace location of MyService.InnerDoSomething on line 70 of the original exception.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

state/in-prA PR is open for this issue.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions