From 9578b894f7cb5b5e9b9c9cc43939b22e66072a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Sun, 28 Feb 2021 19:12:27 +0100 Subject: [PATCH] Jenkins-JUnit emit status attribute for testcase (#84) * Emit status attribute * Productize the change --- .azure/pipelines/jobs/build_and_test.yml | 3 ++- .azure/pipelines/jobs/e2e_tests.yml | 3 ++- Directory.Build.props | 8 +++--- LICENSE | 2 +- ReadMe.md | 11 ++++++++ source/trx2junit/Globals.cs | 17 ++++++++++++ .../trx2junit/JUnitTestResultXmlBuilder.cs | 11 ++++++++ .../Integration.cs | 27 +++++++++++++++++++ 8 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 source/trx2junit/Globals.cs diff --git a/.azure/pipelines/jobs/build_and_test.yml b/.azure/pipelines/jobs/build_and_test.yml index 91bd2b5..8a976e5 100644 --- a/.azure/pipelines/jobs/build_and_test.yml +++ b/.azure/pipelines/jobs/build_and_test.yml @@ -10,7 +10,8 @@ jobs: release-build: BUILD_CONFIG: Release steps: - - template: steps/dotnet-install.yml + # Already installed on current hosted agents + #- template: steps/dotnet-install.yml - bash: | echo 'installed sdks:' diff --git a/.azure/pipelines/jobs/e2e_tests.yml b/.azure/pipelines/jobs/e2e_tests.yml index bd29ccb..0c815d2 100644 --- a/.azure/pipelines/jobs/e2e_tests.yml +++ b/.azure/pipelines/jobs/e2e_tests.yml @@ -4,7 +4,8 @@ jobs: pool: vmImage: 'ubuntu-18.04' steps: - - template: steps/dotnet-install.yml + # Already installed on current hosted agents + #- template: steps/dotnet-install.yml - bash: | sudo apt update diff --git a/Directory.Build.props b/Directory.Build.props index 4ad9d14..13ac777 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,14 +2,14 @@ 1 - 3 - 0 - 88 + 4 + 2 + 102 dev gfoidl Foidl Günther trx2junit - Copyright © Foidl Günther 2017-2019 + Copyright © Foidl Günther 2017-2021 $(VersionMajor).$(VersionMinor).$(VersionPatch) $(VersionMajor).$(VersionMinor).$(BuildNumber).$(VersionPatch) diff --git a/LICENSE b/LICENSE index 79edb09..b72cc72 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2017-2019 Günther Foidl +Copyright (c) 2017-2021 Günther Foidl Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/ReadMe.md b/ReadMe.md index c6c603f..5a4b2c3 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -43,6 +43,17 @@ $ trx2junit a.trx --output ../results $ trx2junit --output results a.trx ../tests/b.trx ``` +#### Jenkins JUnit + +For Jenkins JUnit on the testcase the status-attribute is set. By default `1` is set for success, and `0` for failure. +This can be configured via environment varialbes (note: if omitted, the default values will be used): + +| Status | Variable | default value | +|---------|---------------------------------------------|---------------| +| success | `TRX2JUNIT_JENKINS_TESTCASE_STATUS_SUCCESS` | `1` | +| failure | `TRX2JUNIT_JENKINS_TESTCASE_STATUS_FAILURE` | `0` | +| skipped | `TRX2JUNIT_JENKINS_TESTCASE_STATUS_SKIPPED` | not set | + ### junit to trx With option `--junit2trx` a conversion from _junit_ to _trx_ can be performed. diff --git a/source/trx2junit/Globals.cs b/source/trx2junit/Globals.cs new file mode 100644 index 0000000..e371556 --- /dev/null +++ b/source/trx2junit/Globals.cs @@ -0,0 +1,17 @@ +using System; + +namespace trx2junit +{ + 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"); + //--------------------------------------------------------------------- + private static string? GetEnvironmentVariable(string envVariableName) + { + return Environment.GetEnvironmentVariable(envVariableName) // Process + ?? Environment.GetEnvironmentVariable(envVariableName, EnvironmentVariableTarget.Machine); + } + } +} diff --git a/source/trx2junit/Internal/trx2junit/JUnitTestResultXmlBuilder.cs b/source/trx2junit/Internal/trx2junit/JUnitTestResultXmlBuilder.cs index 179c3ad..ca0ecf9 100644 --- a/source/trx2junit/Internal/trx2junit/JUnitTestResultXmlBuilder.cs +++ b/source/trx2junit/Internal/trx2junit/JUnitTestResultXmlBuilder.cs @@ -83,6 +83,11 @@ private void AddTestCase(XElement xTestSuite, JUnitTestCase testCase) if (testCase.Skipped) { xTestCase.Add(new XElement("skipped")); + + if (Globals.JUnitTestCaseStatusSkipped is not null) + { + xTestCase.Add(new XAttribute("status", Globals.JUnitTestCaseStatusSkipped)); + } } else if (testCase.Error != null) { @@ -91,6 +96,12 @@ private void AddTestCase(XElement xTestSuite, JUnitTestCase testCase) new XAttribute("message", testCase.Error.Message!), new XAttribute("type" , testCase.Error.Type!) )); + + xTestCase.Add(new XAttribute("status", Globals.JUnitTestCaseStatusFailure)); + } + else + { + xTestCase.Add(new XAttribute("status", Globals.JUnitTestCaseStatusSuccess)); } if (testCase.SystemErr != null) diff --git a/tests/trx2junit.Tests/Internal/JUnitTestResultXmlBuilderTests/Integration.cs b/tests/trx2junit.Tests/Internal/JUnitTestResultXmlBuilderTests/Integration.cs index 7de2a26..6655740 100644 --- a/tests/trx2junit.Tests/Internal/JUnitTestResultXmlBuilderTests/Integration.cs +++ b/tests/trx2junit.Tests/Internal/JUnitTestResultXmlBuilderTests/Integration.cs @@ -138,5 +138,32 @@ public void TrxUnitTestResult_with_stderr___system_err_set_by_testcase() Assert.IsNotNull(systemErr, nameof(systemErr)); Assert.AreEqual("message written to stderr", systemErr.Value); } + //--------------------------------------------------------------------- + [Test] + public void Testcase_status_attribute_set() + { + XElement trx = XElement.Load("./data/trx/nunit.trx"); + var parser = new TrxTestResultXmlParser(trx); + + parser.Parse(); + Models.TrxTest testData = parser.Result; + + var converter = new Trx2JunitTestConverter(testData); + converter.Convert(); + + Models.JUnitTest junitTest = converter.Result; + var sut = new JUnitTestResultXmlBuilder(junitTest); + sut.Build(); + + XElement[] testCases = sut.Result.Descendants("testcase").ToArray(); + + Assert.Multiple(() => + { + Assert.AreEqual(3, testCases.Length); + Assert.AreEqual("1", testCases[0].Attribute("status").Value); + Assert.AreEqual("0", testCases[1].Attribute("status").Value); + Assert.AreEqual("1", testCases[2].Attribute("status").Value); + }); + } } }