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
3 changes: 2 additions & 1 deletion src/NUnitFramework/framework/Internal/ExceptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ public static Exception Unwrap(this Exception exception)
}

while (exception is TargetInvocationException targetInvocationException &&
targetInvocationException.InnerException is not null)
targetInvocationException.InnerException is not null &&
targetInvocationException.StackTrace?.Contains("NUnit.Framework") is true)
{
exception = targetInvocationException.InnerException;
}
Expand Down
12 changes: 12 additions & 0 deletions src/NUnitFramework/testdata/UnexpectedExceptionFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace NUnit.TestData.UnexpectedExceptionFixture
[TestFixture]
public class UnexpectedExceptionFixture
{
[Test]
public void ThrowsException()
{
throw new Exception("Thrown Exception");
}

[Test]
public void ThrowsWithInnerException()
{
Expand Down Expand Up @@ -50,6 +56,12 @@ public void ThrowsCustomException()
throw new CustomException("message", new CustomType());
}

[Test]
public void ThrowsTargetInvocationException()
{
GetType().GetMethod(nameof(ThrowsException))!.Invoke(this, null);
}

[Test]
public void AssertThatWithRecursivelyThrowingExceptionAsActual()
{
Expand Down
39 changes: 39 additions & 0 deletions src/NUnitFramework/tests/Internal/UnexpectedExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ private static ITestResult RunDataTestCase(string methodName)
return TestBuilder.RunTestCase(typeof(UnexpectedExceptionFixture), methodName);
}

[Test]
public void FailRecordsException()
{
string expectedMessage =
"System.Exception : Thrown Exception";

ITestResult result = RunDataTestCase(nameof(UnexpectedExceptionFixture.ThrowsException));

Assert.Multiple(() =>
{
Assert.That(result.ResultState, Is.EqualTo(ResultState.Error));
Assert.That(result.Message, Is.EqualTo(expectedMessage));
Assert.That(result.StackTrace, Does.Contain(nameof(UnexpectedExceptionFixture.ThrowsException)));
});
}

[Test]
public void FailRecordsInnerException()
{
Expand All @@ -28,6 +44,7 @@ public void FailRecordsInnerException()
{
Assert.That(result.ResultState, Is.EqualTo(ResultState.Error));
Assert.That(result.Message, Is.EqualTo(expectedMessage));
Assert.That(result.StackTrace, Does.Contain(nameof(UnexpectedExceptionFixture.ThrowsWithInnerException)));
});
}

Expand All @@ -45,6 +62,7 @@ public void FailRecordsNestedInnerException()
{
Assert.That(result.ResultState, Is.EqualTo(ResultState.Error));
Assert.That(result.Message, Is.EqualTo(expectedMessage));
Assert.That(result.StackTrace, Does.Contain(nameof(UnexpectedExceptionFixture.ThrowsWithNestedInnerException)));
});
}

Expand All @@ -63,6 +81,7 @@ public void FailRecordsInnerExceptionsAsPartOfAggregateException()
Assert.That(result.ResultState, Is.EqualTo(ResultState.Error));
Assert.That(result.Message, Does.StartWith(expectedStartOfMessage));
Assert.That(result.Message, Does.EndWith(expectedEndOfMessage));
Assert.That(result.StackTrace, Does.Contain(nameof(UnexpectedExceptionFixture.ThrowsWithAggregateException)));
});
}

Expand All @@ -81,6 +100,7 @@ public void FailRecordsNestedInnerExceptionAsPartOfAggregateException()
Assert.That(result.ResultState, Is.EqualTo(ResultState.Error));
Assert.That(result.Message, Does.StartWith(expectedStartOfMessage));
Assert.That(result.Message, Does.EndWith(expectedEndOfMessage));
Assert.That(result.StackTrace, Does.Contain(nameof(UnexpectedExceptionFixture.ThrowsWithAggregateExceptionContainingNestedInnerException)));
});
}

Expand All @@ -106,6 +126,25 @@ public void CustomExceptionIsHandled()
{
Assert.That(result.ResultState, Is.EqualTo(ResultState.Error));
Assert.That(result.Message, Is.EqualTo("NUnit.TestData.UnexpectedExceptionFixture.CustomException : message"));
Assert.That(result.StackTrace, Does.Contain(nameof(UnexpectedExceptionFixture.ThrowsCustomException)));
});
}

[Test]
public void TargetInvocationExceptionInUserCodeIsPassedThrough()
{
string expectedStartOfMessage = "System.Reflection.TargetInvocationException";
string expectedEndOfMessage = " ----> System.Exception : Thrown Exception";

ITestResult result = RunDataTestCase(nameof(UnexpectedExceptionFixture.ThrowsTargetInvocationException));

Assert.Multiple(() =>
{
Assert.That(result.ResultState, Is.EqualTo(ResultState.Error));
Assert.That(result.Message, Does.StartWith(expectedStartOfMessage));
Assert.That(result.Message, Does.EndWith(expectedEndOfMessage));
Assert.That(result.StackTrace, Does.Contain(nameof(UnexpectedExceptionFixture.ThrowsTargetInvocationException)));
Assert.That(result.StackTrace, Does.Contain(nameof(UnexpectedExceptionFixture.ThrowsException)));
});
}

Expand Down