testfx icon indicating copy to clipboard operation
testfx copied to clipboard

InnerException's are lost in the Testing Platform output

Open thomhurst opened this issue 1 year ago • 2 comments

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.

thomhurst avatar Sep 07 '24 12:09 thomhurst

Any chance we could get this triaged and fixed? Not being able to see exceptions and stack traces seems like a pretty big deal and one that can really limit fixing tests

thomhurst avatar Oct 04 '24 23:10 thomhurst

Hey @thomhurst,

Thanks for the ping and sorry this got missed. I am on it.

Evangelink avatar Oct 07 '24 07:10 Evangelink