Skip to content

Commit

Permalink
JUnit: emit failure content with message as CData element (#101)
Browse files Browse the repository at this point in the history
* Test

* Implementation

* Use CData
  • Loading branch information
gfoidl authored Jun 28, 2022
1 parent 7050e96 commit 1aaef7b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ private void AddTestCase(XElement xTestSuite, JUnitTestCase testCase)
}
else if (testCase.Error != null)
{
string failureContent = $"{testCase.Error.Message}\n{testCase.Error.StackTrace?.TrimEnd()}";

xTestCase.Add(new XElement("failure",
testCase.Error.StackTrace!,
new XCData(failureContent),
new XAttribute("message", testCase.Error.Message!),
new XAttribute("type" , testCase.Error.Type!)
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,44 @@ public void File_given___correct_counts(string trxFile, int expectedTestCount, i
}
//-------------------------------------------------------------------------
[Test]
public void TrxUnitTestResult_with_error___failure_element_populated()
{
XElement trx = XElement.Load("./data/trx/nunit-ignore.trx");
var parser = new TrxTestResultXmlParser(trx);

parser.Parse();
TrxTest testData = parser.Result;

var converter = new Trx2JunitTestConverter(testData);
converter.Convert();

JUnitTest junitTest = converter.Result;
var sut = new JUnitTestResultXmlBuilder(junitTest);
sut.Build();

XElement testsuite = sut.Result.Elements("testsuite").First();
XElement testcase = testsuite.Elements("testcase").Skip(1).First();
XElement failure = testcase.Element("failure");

Assert.Multiple(() =>
{
Assert.AreEqual("Failing for demo purposes", failure.Attribute("message").Value);
string expectedContent = @"<![CDATA[Failing for demo purposes
at NUnitSample.SimpleTests.Failing_test() in D:\Work-Git\github\Mini\trx2junit\samples\NUnitSample\SimpleTests.cs:line 21]]>";
string actualContent = failure.LastNode.ToString();
expectedContent = NormalizeLineEndings(expectedContent);
actualContent = NormalizeLineEndings(actualContent);
Assert.AreEqual(expectedContent, actualContent);
});

static string NormalizeLineEndings(string input)
=> input.Replace("\r\n", "\n");
}
//-------------------------------------------------------------------------
[Test]
public void TrxUnitTestResult_with_stdout___system_out_set()
{
XElement trx = XElement.Load("./data/trx/nunit-with-stdout.trx");
Expand Down

0 comments on commit 1aaef7b

Please sign in to comment.