Skip to content

Commit 9b9460f

Browse files
authored
Opt-in to emit error message in CDATA-element of failure-element (#108)
1 parent 956cf64 commit 9b9460f

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

ReadMe.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ This can be configured via environment varialbes (note: if omitted, the default
5454
| failure | `TRX2JUNIT_JENKINS_TESTCASE_STATUS_FAILURE` | `0` |
5555
| skipped | `TRX2JUNIT_JENKINS_TESTCASE_STATUS_SKIPPED` | not set |
5656

57+
With environment variable `TRX2JUNIT_JUNIT_ERROR_MESSAGE_IN_CDATA` set, the error message from a failing test will be repeated in the _CDATA_-content of the `<failure>` element.
58+
See [this comment](https://github.com/gfoidl/trx2junit/issues/104#issuecomment-1178852241) for further info.
59+
5760
### junit to trx
5861

5962
With option `--junit2trx` a conversion from _junit_ to _trx_ can be performed.

source/trx2junit.Core/Globals.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ namespace gfoidl.Trx2Junit.Core;
66

77
internal static class Globals
88
{
9-
public static readonly string JUnitTestCaseStatusSuccess = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_SUCCESS") ?? "1";
10-
public static readonly string JUnitTestCaseStatusFailure = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_FAILURE") ?? "0";
11-
public static readonly string? JUnitTestCaseStatusSkipped = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_SKIPPED");
12-
public static readonly bool VectorsEnabled = GetEnvironmentVariable("TRX2JUNIT_VECTORS_ENABLED") != null;
9+
public static readonly string JUnitTestCaseStatusSuccess = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_SUCCESS") ?? "1";
10+
public static readonly string JUnitTestCaseStatusFailure = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_FAILURE") ?? "0";
11+
public static readonly string? JUnitTestCaseStatusSkipped = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_SKIPPED");
12+
public static readonly bool JUnitErrorMessageRepeatCData = GetEnvironmentVariable("TRX2JUNIT_JUNIT_ERROR_MESSAGE_IN_CDATA") != null;
13+
public static readonly bool VectorsEnabled = GetEnvironmentVariable("TRX2JUNIT_VECTORS_ENABLED") != null;
1314
//---------------------------------------------------------------------
1415
private static string? GetEnvironmentVariable(string envVariableName)
1516
{

source/trx2junit.Core/Internal/trx2junit/JUnitTestResultXmlBuilder.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,21 @@ private void AddTestCase(XElement xTestSuite, JUnitTestCase testCase)
9494
}
9595
else if (testCase.Error != null)
9696
{
97-
string failureContent = $"{testCase.Error.Message}\n{testCase.Error.StackTrace?.TrimEnd()}";
97+
string? failureContent = Globals.JUnitErrorMessageRepeatCData
98+
? $"{testCase.Error.Message}\n{testCase.Error.StackTrace?.TrimEnd()}"
99+
: testCase.Error.StackTrace?.TrimEnd();
98100

99-
xTestCase.Add(new XElement("failure",
100-
new XCData(failureContent),
101+
XElement xFailure = new ("failure",
101102
new XAttribute("message", testCase.Error.Message!),
102103
new XAttribute("type" , testCase.Error.Type!)
103-
));
104+
);
104105

106+
if (failureContent is not null)
107+
{
108+
xFailure.Add(new XCData(failureContent));
109+
}
110+
111+
xTestCase.Add(xFailure);
105112
xTestCase.Add(new XAttribute("status", Globals.JUnitTestCaseStatusFailure));
106113
}
107114
else

tests/trx2junit.Core.Tests/Internal/JUnitTestResultXmlBuilderTests/Integration.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ public void TrxUnitTestResult_with_error___failure_element_populated()
7474
{
7575
Assert.AreEqual("Failing for demo purposes", failure.Attribute("message").Value);
7676

77-
string expectedContent = @"<![CDATA[Failing for demo purposes
78-
at NUnitSample.SimpleTests.Failing_test() in D:\Work-Git\github\Mini\trx2junit\samples\NUnitSample\SimpleTests.cs:line 21]]>";
77+
string expectedContent = @"<![CDATA[ at NUnitSample.SimpleTests.Failing_test() in D:\Work-Git\github\Mini\trx2junit\samples\NUnitSample\SimpleTests.cs:line 21]]>";
7978
string actualContent = failure.LastNode.ToString();
8079

8180
expectedContent = NormalizeLineEndings(expectedContent);

0 commit comments

Comments
 (0)