Skip to content

Commit

Permalink
Opt-in to emit error message in CDATA-element of failure-element (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfoidl committed Jul 8, 2022
1 parent 956cf64 commit 9b9460f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
3 changes: 3 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ This can be configured via environment varialbes (note: if omitted, the default
| failure | `TRX2JUNIT_JENKINS_TESTCASE_STATUS_FAILURE` | `0` |
| skipped | `TRX2JUNIT_JENKINS_TESTCASE_STATUS_SKIPPED` | not set |

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.
See [this comment](https://github.com/gfoidl/trx2junit/issues/104#issuecomment-1178852241) for further info.

### junit to trx

With option `--junit2trx` a conversion from _junit_ to _trx_ can be performed.
Expand Down
9 changes: 5 additions & 4 deletions source/trx2junit.Core/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ namespace gfoidl.Trx2Junit.Core;

internal static class Globals
{
public static readonly string JUnitTestCaseStatusSuccess = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_SUCCESS") ?? "1";
public static readonly string JUnitTestCaseStatusFailure = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_FAILURE") ?? "0";
public static readonly string? JUnitTestCaseStatusSkipped = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_SKIPPED");
public static readonly bool VectorsEnabled = GetEnvironmentVariable("TRX2JUNIT_VECTORS_ENABLED") != null;
public static readonly string JUnitTestCaseStatusSuccess = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_SUCCESS") ?? "1";
public static readonly string JUnitTestCaseStatusFailure = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_FAILURE") ?? "0";
public static readonly string? JUnitTestCaseStatusSkipped = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_SKIPPED");
public static readonly bool JUnitErrorMessageRepeatCData = GetEnvironmentVariable("TRX2JUNIT_JUNIT_ERROR_MESSAGE_IN_CDATA") != null;
public static readonly bool VectorsEnabled = GetEnvironmentVariable("TRX2JUNIT_VECTORS_ENABLED") != null;
//---------------------------------------------------------------------
private static string? GetEnvironmentVariable(string envVariableName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,21 @@ private void AddTestCase(XElement xTestSuite, JUnitTestCase testCase)
}
else if (testCase.Error != null)
{
string failureContent = $"{testCase.Error.Message}\n{testCase.Error.StackTrace?.TrimEnd()}";
string? failureContent = Globals.JUnitErrorMessageRepeatCData
? $"{testCase.Error.Message}\n{testCase.Error.StackTrace?.TrimEnd()}"
: testCase.Error.StackTrace?.TrimEnd();

xTestCase.Add(new XElement("failure",
new XCData(failureContent),
XElement xFailure = new ("failure",
new XAttribute("message", testCase.Error.Message!),
new XAttribute("type" , testCase.Error.Type!)
));
);

if (failureContent is not null)
{
xFailure.Add(new XCData(failureContent));
}

xTestCase.Add(xFailure);
xTestCase.Add(new XAttribute("status", Globals.JUnitTestCaseStatusFailure));
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public void TrxUnitTestResult_with_error___failure_element_populated()
{
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 expectedContent = @"<![CDATA[ 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);
Expand Down

0 comments on commit 9b9460f

Please sign in to comment.