Skip to content

Commit

Permalink
fix: Fixed ignorance of exceptions in assertable nodes (Should, Then)…
Browse files Browse the repository at this point in the history
…. Now exceptions, i.e. from Mock.Verify are handled as failed tests.
  • Loading branch information
hennadiilu authored Jul 3, 2024
1 parent 0ed7367 commit 59c9c2f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE" Pack="true" PackagePath="/"/>
<None Include="..\..\README.md" Pack="true" PackagePath="/"/>
<None Include="..\..\LICENSE" Pack="true" PackagePath="/" Visible="False" />
<None Include="..\..\README.md" Pack="true" PackagePath="/" Visible="False" />
</ItemGroup>

<PropertyGroup>
Expand Down
21 changes: 17 additions & 4 deletions src/Heleonix.Testing.NUnit/Internal/TestHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ namespace Heleonix.Testing.NUnit.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using global::NUnit.Framework;
using global::NUnit.Framework.Interfaces;
using global::NUnit.Framework.Internal;

/// <summary>
Expand Down Expand Up @@ -89,8 +93,17 @@ public void Execute(SpecNode node)
TestExecutionContext.CurrentContext.OutWriter.WriteLine($"{new string(' ', node.NestingLevel * 4)}\u2713 {node.Description}");
}
}
catch (Exception)
catch (Exception ex)
{
if (ex is not AssertionException)
{
var result = TestExecutionContext.CurrentContext.CurrentResult;

result.RecordAssertion(AssertionStatus.Failed, ex.Message, ex.StackTrace);

result.RecordTestCompletion();
}

if (node.IsAssertable)
{
TestExecutionContext.CurrentContext.OutWriter.WriteLine($"{new string(' ', node.NestingLevel * 4)}\u2717 {node.Description}");
Expand All @@ -110,7 +123,7 @@ private void ValidateAdding(SpecNode child)
{
if (!this.SpecStructureRules.ContainsKey(child.Type))
{
throw new InvalidOperationException($"The spec '{child.Type}' is not valid for the current test pattern");
throw new InvalidOperationException($"The spec '{child.Type}' is not valid for the current test pattern.");
}

var rule = this.SpecStructureRules[child.Type];
Expand All @@ -119,7 +132,7 @@ private void ValidateAdding(SpecNode child)
&& !Regex.IsMatch(string.Join(",", this.specExecutionStack), rule.SpecExecutionStackRule))
{
throw new InvalidOperationException($"Invalid test structure: cannot place the '{child.Type}' " +
$"into the '{string.Join("->", this.specExecutionStack.Reverse())}'");
$"into the '{string.Join("->", this.specExecutionStack.Reverse())}'.");
}

if (rule.PredecessorsRule != null)
Expand All @@ -129,7 +142,7 @@ private void ValidateAdding(SpecNode child)
if (!Regex.IsMatch(string.Join(",", parent.Children.Reverse()), rule.PredecessorsRule))
{
throw new InvalidOperationException($"Invalid test structure: cannot place the '{child.Type}' " +
$"after the '{string.Join("->", parent.Children)}'");
$"after the '{string.Join("->", parent.Children)}'.");
}
}
}
Expand Down
27 changes: 23 additions & 4 deletions test/Heleonix.Testing.NUnit.Tests/Internal/TestHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace Heleonix.Testing.NUnit.Tests.Internal;
using System.Collections.Generic;
using System.Reflection;
using global::NUnit.Framework;
using global::NUnit.Framework.Interfaces;
using global::NUnit.Framework.Internal;
using global::NUnit.Framework.Internal.Builders;
using Heleonix.Testing.NUnit.Internal;
using Moq;
using Moq.Protected;
Expand Down Expand Up @@ -268,7 +270,7 @@ public static void ValidateAdding6()
/// <summary>
/// Tests writing of a failed test with marking into the Output.
/// </summary>
[Test(Description = "When an assertable node is throwing an exception Should write the test description as failed into the output")]
[Test(Description = "When an assertable node is throwing an exception Should write the test description as failed into the output and write failure result")]
public static void Execute1()
{
// Arrange
Expand All @@ -285,16 +287,33 @@ public static void Execute1()

var rootNode = new SpecNode(SpecNodeType.Root, null, () => { });

var node = new SpecNode(SpecNodeType.Should, "description", () => { throw new InvalidOperationException(); }, true);
var node = new SpecNode(
SpecNodeType.Should,
"description",
() => { throw new InvalidOperationException("Custom exception message."); },
true);

rootNode.Add(node);

var prevResult = TestExecutionContext.CurrentContext.CurrentResult;

// Substitute the current test result to catch SpecNode execution.
TestExecutionContext.CurrentContext.CurrentResult =
TestExecutionContext.CurrentContext.CurrentTest.MakeTestResult();

// Act
methodInfo.Invoke(testHostMock.Object, new[] { node });

var failCount = TestExecutionContext.CurrentContext.CurrentResult.FailCount;
var message = TestExecutionContext.CurrentContext.CurrentResult.Message;
var stackTrace = TestExecutionContext.CurrentContext.CurrentResult.StackTrace;

TestExecutionContext.CurrentContext.CurrentResult = prevResult;

// Assert
// Cannot read the NUnit Output, so rely on the 100% test coverage.
Assert.True(true);
Assert.That(failCount, Is.EqualTo(1));
Assert.That(message, Contains.Substring("Custom exception message."));
Assert.That(stackTrace, Is.Not.Empty);
}

/// <summary>
Expand Down

0 comments on commit 59c9c2f

Please sign in to comment.