From 84d33e9b7652253eaeeeed632b81f7b32492ebe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Fri, 8 Jul 2022 16:38:31 +0200 Subject: [PATCH] Opt-in to emit error message in CDATA-element of failure-element --- ReadMe.md | 3 +++ source/trx2junit.Core/Globals.cs | 9 +++++---- .../trx2junit/JUnitTestResultXmlBuilder.cs | 15 +++++++++++---- .../JUnitTestResultXmlBuilderTests/Integration.cs | 3 +-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index ca290a8..f51f03e 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -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 `` 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. diff --git a/source/trx2junit.Core/Globals.cs b/source/trx2junit.Core/Globals.cs index c26cbbc..36d2948 100644 --- a/source/trx2junit.Core/Globals.cs +++ b/source/trx2junit.Core/Globals.cs @@ -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) { diff --git a/source/trx2junit.Core/Internal/trx2junit/JUnitTestResultXmlBuilder.cs b/source/trx2junit.Core/Internal/trx2junit/JUnitTestResultXmlBuilder.cs index 9bcf6b6..3e0f00b 100644 --- a/source/trx2junit.Core/Internal/trx2junit/JUnitTestResultXmlBuilder.cs +++ b/source/trx2junit.Core/Internal/trx2junit/JUnitTestResultXmlBuilder.cs @@ -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 diff --git a/tests/trx2junit.Core.Tests/Internal/JUnitTestResultXmlBuilderTests/Integration.cs b/tests/trx2junit.Core.Tests/Internal/JUnitTestResultXmlBuilderTests/Integration.cs index 385e1e6..c4e0977 100644 --- a/tests/trx2junit.Core.Tests/Internal/JUnitTestResultXmlBuilderTests/Integration.cs +++ b/tests/trx2junit.Core.Tests/Internal/JUnitTestResultXmlBuilderTests/Integration.cs @@ -74,8 +74,7 @@ public void TrxUnitTestResult_with_error___failure_element_populated() { Assert.AreEqual("Failing for demo purposes", failure.Attribute("message").Value); - string expectedContent = @""; + string expectedContent = @""; string actualContent = failure.LastNode.ToString(); expectedContent = NormalizeLineEndings(expectedContent);