Skip to content

Commit 9578b89

Browse files
authored
Jenkins-JUnit emit status attribute for testcase (#84)
* Emit status attribute * Productize the change
1 parent a10c553 commit 9578b89

File tree

8 files changed

+75
-7
lines changed

8 files changed

+75
-7
lines changed

.azure/pipelines/jobs/build_and_test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ jobs:
1010
release-build:
1111
BUILD_CONFIG: Release
1212
steps:
13-
- template: steps/dotnet-install.yml
13+
# Already installed on current hosted agents
14+
#- template: steps/dotnet-install.yml
1415

1516
- bash: |
1617
echo 'installed sdks:'

.azure/pipelines/jobs/e2e_tests.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ jobs:
44
pool:
55
vmImage: 'ubuntu-18.04'
66
steps:
7-
- template: steps/dotnet-install.yml
7+
# Already installed on current hosted agents
8+
#- template: steps/dotnet-install.yml
89

910
- bash: |
1011
sudo apt update

Directory.Build.props

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
<PropertyGroup>
44
<VersionMajor Condition="'$(VersionMajor)' == ''">1</VersionMajor>
5-
<VersionMinor Condition="'$(VersionMinor)' == ''">3</VersionMinor>
6-
<VersionPatch Condition="'$(VersionPatch)' == ''">0</VersionPatch>
7-
<BuildNumber Condition="'$(BuildNumber)' == ''">88</BuildNumber>
5+
<VersionMinor Condition="'$(VersionMinor)' == ''">4</VersionMinor>
6+
<VersionPatch Condition="'$(VersionPatch)' == ''">2</VersionPatch>
7+
<BuildNumber Condition="'$(BuildNumber)' == ''">102</BuildNumber>
88
<VersionSuffix Condition="'$(Configuration)' == 'Debug' and '$(VersionSuffix)' == ''">dev</VersionSuffix>
99
<Authors>gfoidl</Authors>
1010
<Company>Foidl Günther</Company>
1111
<Product>trx2junit</Product>
12-
<Copyright>Copyright © Foidl Günther 2017-2019</Copyright>
12+
<Copyright>Copyright © Foidl Günther 2017-2021</Copyright>
1313
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
1414
<AssemblyVersion>$(VersionMajor).$(VersionMinor).$(BuildNumber).$(VersionPatch)</AssemblyVersion>
1515
</PropertyGroup>

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017-2019 Günther Foidl
3+
Copyright (c) 2017-2021 Günther Foidl
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

ReadMe.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ $ trx2junit a.trx --output ../results
4343
$ trx2junit --output results a.trx ../tests/b.trx
4444
```
4545

46+
#### Jenkins JUnit
47+
48+
For Jenkins JUnit on the testcase the status-attribute is set. By default `1` is set for success, and `0` for failure.
49+
This can be configured via environment varialbes (note: if omitted, the default values will be used):
50+
51+
| Status | Variable | default value |
52+
|---------|---------------------------------------------|---------------|
53+
| success | `TRX2JUNIT_JENKINS_TESTCASE_STATUS_SUCCESS` | `1` |
54+
| failure | `TRX2JUNIT_JENKINS_TESTCASE_STATUS_FAILURE` | `0` |
55+
| skipped | `TRX2JUNIT_JENKINS_TESTCASE_STATUS_SKIPPED` | not set |
56+
4657
### junit to trx
4758

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

source/trx2junit/Globals.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace trx2junit
4+
{
5+
internal static class Globals
6+
{
7+
public static readonly string JUnitTestCaseStatusSuccess = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_SUCCESS") ?? "1";
8+
public static readonly string JUnitTestCaseStatusFailure = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_FAILURE") ?? "0";
9+
public static readonly string? JUnitTestCaseStatusSkipped = GetEnvironmentVariable("TRX2JUNIT_JENKINS_TESTCASE_STATUS_SKIPPED");
10+
//---------------------------------------------------------------------
11+
private static string? GetEnvironmentVariable(string envVariableName)
12+
{
13+
return Environment.GetEnvironmentVariable(envVariableName) // Process
14+
?? Environment.GetEnvironmentVariable(envVariableName, EnvironmentVariableTarget.Machine);
15+
}
16+
}
17+
}

source/trx2junit/Internal/trx2junit/JUnitTestResultXmlBuilder.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ private void AddTestCase(XElement xTestSuite, JUnitTestCase testCase)
8383
if (testCase.Skipped)
8484
{
8585
xTestCase.Add(new XElement("skipped"));
86+
87+
if (Globals.JUnitTestCaseStatusSkipped is not null)
88+
{
89+
xTestCase.Add(new XAttribute("status", Globals.JUnitTestCaseStatusSkipped));
90+
}
8691
}
8792
else if (testCase.Error != null)
8893
{
@@ -91,6 +96,12 @@ private void AddTestCase(XElement xTestSuite, JUnitTestCase testCase)
9196
new XAttribute("message", testCase.Error.Message!),
9297
new XAttribute("type" , testCase.Error.Type!)
9398
));
99+
100+
xTestCase.Add(new XAttribute("status", Globals.JUnitTestCaseStatusFailure));
101+
}
102+
else
103+
{
104+
xTestCase.Add(new XAttribute("status", Globals.JUnitTestCaseStatusSuccess));
94105
}
95106

96107
if (testCase.SystemErr != null)

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,32 @@ public void TrxUnitTestResult_with_stderr___system_err_set_by_testcase()
138138
Assert.IsNotNull(systemErr, nameof(systemErr));
139139
Assert.AreEqual("message written to stderr", systemErr.Value);
140140
}
141+
//---------------------------------------------------------------------
142+
[Test]
143+
public void Testcase_status_attribute_set()
144+
{
145+
XElement trx = XElement.Load("./data/trx/nunit.trx");
146+
var parser = new TrxTestResultXmlParser(trx);
147+
148+
parser.Parse();
149+
Models.TrxTest testData = parser.Result;
150+
151+
var converter = new Trx2JunitTestConverter(testData);
152+
converter.Convert();
153+
154+
Models.JUnitTest junitTest = converter.Result;
155+
var sut = new JUnitTestResultXmlBuilder(junitTest);
156+
sut.Build();
157+
158+
XElement[] testCases = sut.Result.Descendants("testcase").ToArray();
159+
160+
Assert.Multiple(() =>
161+
{
162+
Assert.AreEqual(3, testCases.Length);
163+
Assert.AreEqual("1", testCases[0].Attribute("status").Value);
164+
Assert.AreEqual("0", testCases[1].Attribute("status").Value);
165+
Assert.AreEqual("1", testCases[2].Attribute("status").Value);
166+
});
167+
}
141168
}
142169
}

0 commit comments

Comments
 (0)