Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include inner exceptions when tests fail. #1327

Open
benjaminsampica opened this issue Dec 1, 2024 · 13 comments
Open

Include inner exceptions when tests fail. #1327

benjaminsampica opened this issue Dec 1, 2024 · 13 comments

Comments

@benjaminsampica
Copy link

benjaminsampica commented Dec 1, 2024

It would be helpful to include inner exceptions in the test failure if possible. I'm thinking like when using EF Core and you have a foreign key conflict, currently the library only spits out the following:

An error occurred while saving the entity changes. See the inner exception for details.
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.SqlServerModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChangesAsync(IList`1 entries, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.<>c__DisplayClass30_0`2.<<ExecuteAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at SendTrucks.Ui.Tests.IntegrationTestBase.AddAsync[TEntity](TEntity entity) in /Users/benjaminsampica/Repos/sendtrucks/src/SendTrucks.Ui.Tests/IntegrationTestBase.cs:line 96
   at SendTrucks.Ui.Tests.Features.Loads.Detail.Dispatches.Records.Detail.ExportTests.GivenIWantToExport_WhenTheDispatchExists_ThenExports() in /Users/benjaminsampica/Repos/sendtrucks/src/SendTrucks.Ui.Tests/Features/Loads/Detail/Dispatches/Records/Detail/ExportTests.cs:line 13
   at TUnit.Core.AsyncConvert.Convert(Func`1 action)
   at TUnit.Core.DiscoveredTest`1.ExecuteTest(CancellationToken cancellationToken)
   at TUnit.Engine.Helpers.Timings.Record(String name, TestContext context, Func`1 action)
   at TUnit.Engine.Services.TestInvoker.Invoke(DiscoveredTest discoveredTest, CancellationToken cancellationToken, List`1 cleanupExceptions)
   at TUnit.Engine.Services.TestInvoker.Invoke(DiscoveredTest discoveredTest, CancellationToken cancellationToken, List`1 cleanupExceptions)
   at TUnit.Engine.Services.SingleTestExecutor.ExecuteTestMethodWithTimeout(DiscoveredTest discoveredTest, CancellationToken cancellationToken, List`1 cleanupExceptions)
   at TUnit.Engine.Services.SingleTestExecutor.ExecuteWithCancellationTokens(DiscoveredTest discoveredTest, List`1 cleanupExceptions)
   at TUnit.Engine.Services.SingleTestExecutor.ExecuteWithRetries(DiscoveredTest discoveredTest, List`1 cleanupExceptions)
   at TUnit.Engine.Services.SingleTestExecutor.ExecuteWithRetries(DiscoveredTest discoveredTest, List`1 cleanupExceptions)
   at TUnit.Engine.Services.SingleTestExecutor.ExecuteTestInternalAsync(DiscoveredTest test, ITestExecutionFilter filter, ExecuteRequestContext context, Boolean isStartedAsDependencyForAnotherTest)
   at TUnit.Engine.Services.SingleTestExecutor.ExecuteTestInternalAsync(DiscoveredTest test, ITestExecutionFilter filter, ExecuteRequestContext context, Boolean isStartedAsDependencyForAnotherTest)
   at TUnit.Engine.Services.SingleTestExecutor.ExecuteTestInternalAsync(DiscoveredTest test, ITestExecutionFilter filter, ExecuteRequestContext context, Boolean isStartedAsDependencyForAnotherTest)
   at TUnit.Engine.Services.TestsExecutor.ProcessTest(DiscoveredTest test, ITestExecutionFilter filter, ExecuteRequestContext context, CancellationToken cancellationToken)

Of course, this can be waved away with hooking via after test and pulling it out but having it be baked in would be great.

@thomhurst
Copy link
Owner

This is actually a Testing Platform issue. I send an object to the testing platform with the raw exception on it. Since output is handled by either them, or the IDE, then they need to output it correctly.

@thomhurst
Copy link
Owner

I did actually raise this on the testing platform a while back, but it should've been sorted and in the current release: microsoft/testfx#3783

Where are you looking at this output? CLI / VS / Rider?

@benjaminsampica
Copy link
Author

Interesting! I'm seeing this in Rider via the test explorer window.

@thomhurst
Copy link
Owner

It might be a Rider issue then. I'm actually not sure if it's testing platform or rider to be honest!

@thomhurst
Copy link
Owner

The CLI seems to show them properly. But Rider and VS don't. Not sure if both have issues or not.

@thomhurst
Copy link
Owner

@Evangelink @nohwnd Do you happen to know?

@Evangelink
Copy link

I'll double check VS behavior and what the platform sends and get back to you.

@Evangelink
Copy link

I believe this is working fine on platform level.

My MSTest code:

[TestClass]
public class TestClass
{
    [TestMethod]
    public void Test3()
    {
        try
        {
            try
            {
                Throw();
            }
            catch (Exception ex)
            {
                throw new Exception("inner", ex);
            }
        }
        catch (Exception ex)
        {
            throw new Exception("root", ex);
        }
    }

    private void Throw() => throw new Exception("bottom");
}

The output in Test Explorer:
image

Console output:
image

@thomhurst
Copy link
Owner

It shows in TUnit console too:

failed Test3 (61ms)
  System.Exception: root
    at TestClass.Test3() in Program.cs:19
    at TUnit.SourceGenerated.<TestClass__ConsoleApp8_e7893e468b134cb5a0d9c378b0c3ea7f_Generated>F9A1DAB0743430E24826D0CC045A343AE06EDBD6B3496FB02F9E90B56294935FD__TestClass__ConsoleApp8_e7893e468b134cb5a0d9c378b0c3ea7f.<>c__DisplayClass2_2.<Tests0>b__3() in obj\Debug\net8.0\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.CodeGenerators.TestsGenerator\TestClass__ConsoleApp8_e7893e468b134cb5a0d9c378b0c3ea7f.Generated.cs:51
    at TUnit.SourceGenerated.<TestClass__ConsoleApp8_e7893e468b134cb5a0d9c378b0c3ea7f_Generated>F9A1DAB0743430E24826D0CC045A343AE06EDBD6B3496FB02F9E90B56294935FD__TestClass__ConsoleApp8_e7893e468b134cb5a0d9c378b0c3ea7f.<>c.<Tests0>b__2_2(TestClass classInstance, CancellationToken cancellationToken) in obj\Debug\net8.0\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.CodeGenerators.TestsGenerator\TestClass__ConsoleApp8_e7893e468b134cb5a0d9c378b0c3ea7f.Generated.cs:51
    at TUnit.Core.TestMetadata`1.<BuildDiscoveredTest>b__11_0(TClassType classInstance, CancellationToken cancellationToken)
    at TUnit.Core.DiscoveredTest`1.<>c__DisplayClass8_0.<ExecuteTest>b__0()
    at TUnit.Core.DefaultExecutor.ExecuteAsync(Func`1 action)
    at TUnit.Core.GenericAbstractExecutor.ExecuteTest(TestContext context, Func`1 action)
    at TUnit.Core.DiscoveredTest`1.ExecuteTest(CancellationToken cancellationToken)
    at TUnit.Engine.Helpers.Timings.Record(String name, TestContext context, Func`1 action)
    at TUnit.Engine.Services.TestInvoker.Invoke(DiscoveredTest discoveredTest, CancellationToken cancellationToken, List`1 cleanupExceptions)
    at TUnit.Engine.Services.TestInvoker.Invoke(DiscoveredTest discoveredTest, CancellationToken cancellationToken, List`1 cleanupExceptions)
    at TUnit.Engine.Services.SingleTestExecutor.ExecuteTestMethodWithTimeout(DiscoveredTest discoveredTest, CancellationToken cancellationToken, List`1 cleanupExceptions)
    at TUnit.Engine.Services.SingleTestExecutor.ExecuteWithCancellationTokens(DiscoveredTest discoveredTest, List`1 cleanupExceptions)
    at TUnit.Engine.Services.SingleTestExecutor.ExecuteWithRetries(DiscoveredTest discoveredTest, List`1 cleanupExceptions)
    at TUnit.Engine.Services.SingleTestExecutor.ExecuteWithRetries(DiscoveredTest discoveredTest, List`1 cleanupExceptions)
    at TUnit.Engine.Services.SingleTestExecutor.ExecuteTestInternalAsync(DiscoveredTest test, ITestExecutionFilter filter, ExecuteRequestContext context, Boolean isStartedAsDependencyForAnotherTest)
    at TUnit.Engine.Services.SingleTestExecutor.ExecuteTestInternalAsync(DiscoveredTest test, ITestExecutionFilter filter, ExecuteRequestContext context, Boolean isStartedAsDependencyForAnotherTest)
    at TUnit.Engine.Services.SingleTestExecutor.ExecuteTestInternalAsync(DiscoveredTest test, ITestExecutionFilter filter, ExecuteRequestContext context, Boolean isStartedAsDependencyForAnotherTest)
    at TUnit.Engine.Services.TestsExecutor.ProcessTest(DiscoveredTest test, ITestExecutionFilter filter, ExecuteRequestContext context, CancellationToken cancellationToken)
  --->  System.Exception: inner
    at TestClass.Test3() in Program.cs:14
  --->  System.Exception: bottom
    at TestClass.Throw() in Program.cs:23
    at TestClass.Test3() in Program.cs:10

It doesn't in Rider, but since the CLI is fine, I'd go with it being a Rider issue

@Evangelink
Copy link

I don't have contact on Rider side, do you have some @thomhurst?

@thomhurst
Copy link
Owner

Not a specific contact, but I can raise on YouTrack and they can triage it

Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Jan 20, 2025
@benjaminsampica
Copy link
Author

@thomhurst do you have a link to the Youtrack issue? Otherwise I can open one and this can probably be closed here.

@github-actions github-actions bot removed the Stale label Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants