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

Assert.Multiple produces new tests in the report if inner assertions fail #631

Merged
merged 1 commit into from
Jul 10, 2019
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
42 changes: 27 additions & 15 deletions src/NUnitTestAdapter/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,38 @@ public TestCase GetCachedTestCase(string id)
}

private static readonly string NL = Environment.NewLine;

public TestResultSet GetVSTestResults(XmlNode resultNode, ICollection<XmlNode> outputNodes)
{
var results = new List<VSTestResult>();
XmlNodeList assertions = resultNode.SelectNodes("assertions/assertion");

var testcaseResult = GetBasicResult(resultNode, outputNodes);
foreach (XmlNode assertion in assertions)

if (testcaseResult != null)
{
var oneResult = GetBasicResult(resultNode, outputNodes); // we need a new copy, this is currently the simplest way
if (oneResult != null)
if (testcaseResult.Outcome == TestOutcome.Failed || testcaseResult.Outcome == TestOutcome.NotFound)
{
oneResult.Outcome = GetAssertionOutcome(assertion);
oneResult.ErrorMessage = assertion.SelectSingleNode("message")?.InnerText;
oneResult.ErrorStackTrace = assertion.SelectSingleNode("stack-trace")?.InnerText;
results.Add(oneResult);
testcaseResult.ErrorMessage = resultNode.SelectSingleNode("failure/message")?.InnerText;
testcaseResult.ErrorStackTrace = resultNode.SelectSingleNode("failure/stack-trace")?.InnerText;

// find stacktrace in assertion nodes if not defined (seems .netcore2.0 doesn't provide stack-trace for Assert.Fail("abc"))
if (testcaseResult.ErrorStackTrace == null)
{
string stackTrace = string.Empty;
foreach (XmlNode assertionStacktraceNode in resultNode.SelectNodes("assertions/assertion/stack-trace"))
{
stackTrace += assertionStacktraceNode.InnerText;
}
testcaseResult.ErrorStackTrace = stackTrace;
}
}
else if (testcaseResult.Outcome == TestOutcome.Skipped || testcaseResult.Outcome == TestOutcome.None)
{
testcaseResult.ErrorMessage = resultNode.SelectSingleNode("reason/message")?.InnerText;
}
}

if (testcaseResult!=null && testcaseResult.Outcome == TestOutcome.Failed && results.Any() && results.All(o => o.Outcome != TestOutcome.Failed))
results.First().Outcome = TestOutcome.Failed;
results.Add(testcaseResult);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you might add null. What is the effect of that?

}

if (results.Count == 0)
{
Expand All @@ -129,7 +141,7 @@ public TestResultSet GetVSTestResults(XmlNode resultNode, ICollection<XmlNode> o
results.Add(result);
}

return new TestResultSet {TestCaseResult = testcaseResult, TestResults = results};
return new TestResultSet { TestCaseResult = testcaseResult, TestResults = results };
}

public struct TestResultSet
Expand Down Expand Up @@ -169,7 +181,7 @@ private TestCase MakeTestCaseFromXmlNode(XmlNode testNode)
}
}

testCase.AddTraitsFromTestNode(testNode, TraitsCache,_logger,adapterSettings);
testCase.AddTraitsFromTestNode(testNode, TraitsCache, _logger, adapterSettings);

return testCase;
}
Expand Down Expand Up @@ -271,7 +283,7 @@ private AttachmentSet ParseAttachments(XmlNode resultNode)
var path = attachment.SelectSingleNode("filePath")?.InnerText ?? string.Empty;
var description = attachment.SelectSingleNode("description")?.InnerText;

if ( !(string.IsNullOrEmpty(path) || path.StartsWith(fileUriScheme, StringComparison.OrdinalIgnoreCase)))
if (!(string.IsNullOrEmpty(path) || path.StartsWith(fileUriScheme, StringComparison.OrdinalIgnoreCase)))
{
path = fileUriScheme + path;
}
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitTestAdapterTests/NUnitEventListenerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private void VerifyTestResult(VSTestResult ourResult)

Assert.AreEqual(Environment.MachineName, ourResult.ComputerName);
Assert.AreEqual(TestOutcome.Passed, ourResult.Outcome);
Assert.AreEqual("It passed!", ourResult.ErrorMessage);
Assert.AreEqual(null, ourResult.ErrorMessage);
Assert.AreEqual(TimeSpan.FromSeconds(1.234), ourResult.Duration);
}

Expand Down
2 changes: 1 addition & 1 deletion src/NUnitTestAdapterTests/TestConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void CanMakeTestResultFromNUnitTestResult()
CheckTestCase(testCase);

Assert.That(testResult.Outcome, Is.EqualTo(TestOutcome.Passed));
Assert.That(testResult.ErrorMessage, Is.EqualTo("It passed!"));
Assert.That(testResult.ErrorMessage, Is.EqualTo(null));
Assert.That(testResult.Duration, Is.EqualTo(TimeSpan.FromSeconds(1.234)));
}

Expand Down
26 changes: 12 additions & 14 deletions src/NUnitTestAdapterTests/TestExecutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public int TestOutcomeTotalsAreCorrect(TestOutcome outcome)
.Count(e => e.EventType == FakeFrameworkHandle.EventType.RecordResult && e.TestResult.Outcome == outcome);
}

[TestCase("MockTest3", TestOutcome.Passed, "Succeeded!", false)]
[TestCase("MockTest3", TestOutcome.Passed, null, false)]
[TestCase("FailingTest", TestOutcome.Failed, "Intentional failure", true)]
[TestCase("TestWithException", TestOutcome.Failed, "System.Exception : Intentional Exception", true)]
// NOTE: Should Inconclusive be reported as TestOutcome.None?
Expand All @@ -174,7 +174,7 @@ public void TestResultIsReportedCorrectly(string name, TestOutcome outcome, stri
Assert.That(testResult.Outcome, Is.EqualTo(outcome));
Assert.That(testResult.ErrorMessage, Is.EqualTo(message));
if (hasStackTrace)
Assert.NotNull(testResult.ErrorStackTrace);
Assert.NotNull(testResult.ErrorStackTrace, "Unable to find error stacktrace");
}

[Test]
Expand Down Expand Up @@ -202,30 +202,30 @@ public void NativeAssemblyProducesWarning()
var fakeFramework = new FakeFrameworkHandle();

// Find the native exaple dll.
var path = Path.Combine( TestContext.CurrentContext.TestDirectory, "NativeTests.dll" );
Assert.That( File.Exists( path ) );
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "NativeTests.dll");
Assert.That(File.Exists(path));

// Try to execute tests from the dll.
TestAdapterUtils.CreateExecutor().RunTests(
new[] { path },
context,
fakeFramework );
fakeFramework);

// Gather results.
var testResults = fakeFramework.Events
.Where( e => e.EventType == FakeFrameworkHandle.EventType.RecordResult )
.Select( e => e.TestResult )
.Where(e => e.EventType == FakeFrameworkHandle.EventType.RecordResult)
.Select(e => e.TestResult)
.ToList();

// No tests found.
Assert.That( testResults.Count, Is.EqualTo( 0 ) );
Assert.That(testResults.Count, Is.EqualTo(0));

// A warning about unsupported assebly format provided.
var messages = fakeFramework.Events
.Where( e=> e.EventType == FakeFrameworkHandle.EventType.SendMessage &&
e.Message.Level == Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging.TestMessageLevel.Warning )
.Select( o => o.Message.Text );
Assert.That( messages, Has.Some.Contains( "Assembly not supported" ) );
.Where(e => e.EventType == FakeFrameworkHandle.EventType.SendMessage &&
e.Message.Level == Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging.TestMessageLevel.Warning)
.Select(o => o.Message.Text);
Assert.That(messages, Has.Some.Contains("Assembly not supported"));
}
#endif

Expand All @@ -250,8 +250,6 @@ private TestResult GetTestResult(string displayName)
.Select(e => e.TestResult)
.FirstOrDefault();
}


}


Expand Down