Skip to content

Commit

Permalink
Merge branch 'skbkontur/anton92nd/ShowTestException'
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton92nd committed Nov 7, 2019
2 parents c3b0b60 + 73d0128 commit 46980fd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ public NonConstructableByContainerClass(string fieldForWhichContainerIsNotConfig
}
}

[Explicit("Intentionally fails with 'Error in FailureInTestAndTearDown_ExplicitTest.Test()' error and 'Error in AndB.TearDown()' error")]
[GroboTestFixture, AndATearDownFailure]
public class FailureInTestAndTearDown_ExplicitTest
{
[Test]
public void Test()
{
GroboTestMachineryTrace.Log($"{nameof(FailureInTestAndTearDown_ExplicitTest)}.Test()");
throw new InvalidOperationException($"Error in {nameof(FailureInTestAndTearDown_ExplicitTest)}.Test()");
}
}

public class TestFailuresInDifferentPartsTest
{
/// <summary>
Expand Down Expand Up @@ -227,6 +239,22 @@ public void TestFailureInFieldInjection()
var result = testResults[nameof(FailureInFieldInjection_ExplicitTest.Test)];
result.Message.Should().Contain("GroboContainer.Impl.Exceptions.AmbiguousConstructorException");
result.Output.Should().Be(@"FailureInFieldInjection_ExplicitTest.TestFixtureSetUp()
");
}

[Test]
public void TestFailureInTestAndTearDown()
{
var testResults = TestRunner.RunTestClass<FailureInTestAndTearDown_ExplicitTest>();
var result = testResults[nameof(FailureInTestAndTearDown_ExplicitTest.Test)];
result.Message.Should().Contain($"Error in {nameof(FailureInTestAndTearDown_ExplicitTest)}.Test()").And.Contain("Error in AndB.TearDown()");
result.Output.Should().Be(@"AndC.SetUp()
AndB.SetUp()
AndA.SetUp()
FailureInTestAndTearDown_ExplicitTest.Test()
AndA.TearDown()
AndB.TearDown()
AndC.TearDown()
");
}
}
Expand Down
17 changes: 17 additions & 0 deletions GroboContainer.NUnitExtensions/Impl/FailedTestException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace GroboContainer.NUnitExtensions.Impl
{
public class FailedTestException : Exception
{
public FailedTestException(string message, string stackTrace)
{
Message = message;
StackTrace = stackTrace;
}

public override string Message { get; }

public override string StackTrace { get; }
}
}
14 changes: 13 additions & 1 deletion GroboContainer.NUnitExtensions/Impl/GroboTestAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

using NUnit.Framework.Interfaces;

using NUnitTestContext = NUnit.Framework.TestContext;

namespace GroboContainer.NUnitExtensions.Impl
{
public static class GroboTestAction
Expand Down Expand Up @@ -130,9 +132,19 @@ public static void AfterTest([NotNull] ITest testDetails)
}
if (errors.Count > 0)
{
var aggregateExceptionMessage = $"{errors.Count} TearDown methods failed.";

var testResult = NUnitTestContext.CurrentContext.Result;
if (testResult.Outcome.Status == TestStatus.Failed && testResult.Outcome.Site == FailureSite.Test)
{
aggregateExceptionMessage = $"Test method and {errors.Count} TearDown method(s) failed.";
errors.Insert(0, new FailedTestException(testResult.Message, testResult.StackTrace));
}

if (errors.Count == 1)
throw errors[0];
throw new AggregateException("After test methods failed.", errors);

throw new AggregateException(aggregateExceptionMessage, errors);
}
}

Expand Down

0 comments on commit 46980fd

Please sign in to comment.