Skip to content

Commit

Permalink
feat: [JUnit] added StoreConsoleOutput option (#115)
Browse files Browse the repository at this point in the history
Co-authored-by: Petr Kun <petr.kun@exness.com>
  • Loading branch information
gamerka and PetrKun authored Sep 29, 2024
1 parent 403364e commit 63fc58f
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/JUnit.Xml.TestLogger/JunitXmlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class JunitXmlSerializer : ITestResultSerializer

public const string FailureBodyFormatKey = "FailureBodyFormat";

public const string StoreConsoleOutputKey = "StoreConsoleOutput";

private const string ResultStatusPassed = "Passed";
private const string ResultStatusFailed = "Failed";

Expand Down Expand Up @@ -62,6 +64,8 @@ public enum FailureBodyFormat

public FailureBodyFormat FailureBodyFormatOption { get; private set; } = FailureBodyFormat.Default;

public bool StoreConsoleOutputOption { get; private set; } = true;

public static IEnumerable<TestSuite> GroupTestSuites(IEnumerable<TestSuite> suites)
{
var groups = suites;
Expand Down Expand Up @@ -229,8 +233,8 @@ private XElement CreateTestSuiteElement(List<TestResultInfo> results, TestRunCon
"testsuite",
new XElement("properties"),
testCaseElements,
new XElement("system-out", stdOut.ToString()),
new XElement("system-err", stdErr.ToString()));
new XElement("system-out", this.StoreConsoleOutputOption ? stdOut.ToString() : string.Empty),
new XElement("system-err", this.StoreConsoleOutputOption ? stdErr.ToString() : string.Empty));

element.SetAttributeValue("name", Path.GetFileName(results.First().AssemblyPath));

Expand Down Expand Up @@ -354,6 +358,22 @@ private void Configure(LoggerConfiguration loggerConfiguration)
Console.WriteLine($"JunitXML Logger: The provided Failure Body Format '{failureFormat}' is not a recognized option. Using default");
}
}

if (loggerConfiguration.Values.TryGetValue(StoreConsoleOutputKey, out string storeOutputValue))
{
if (string.Equals(storeOutputValue.Trim(), "true", StringComparison.OrdinalIgnoreCase))
{
this.StoreConsoleOutputOption = true;
}
else if (string.Equals(storeOutputValue.Trim(), "false", StringComparison.OrdinalIgnoreCase))
{
this.StoreConsoleOutputOption = false;
}
else
{
Console.WriteLine($"JunitXML Logger: The provided Store Console Output '{storeOutputValue}' is not a recognized option. Using default");
}
}
}

public class TestSuite
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) Spekt Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace JUnit.Xml.TestLogger.AcceptanceTests
{
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using global::TestLogger.Fixtures;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Spekt.TestLogger.Core;

/// <summary>
/// Acceptance tests evaluate the most recent output of the build.ps1 script, NOT the most
/// recent build performed by visual studio or dotnet.build
///
/// These acceptance tests look at the specific places output is expected to change because of
/// the format option specified. Accordingly, these tests cannot protect against other changes
/// occurring due to the formatting option.
/// </summary>
[TestClass]
public class JUnitTestLoggerStoreConsoleOutputOptionsAcceptanceTests
{
private const string AssetName = "JUnit.Xml.TestLogger.NetCore.Tests";

[TestMethod]
public void StoreConsoleOutput_Default_ContainsConsoleOut()
{
var loggerArgs = "junit;LogFilePath=output-default-test-results.xml";

var resultsFile = DotnetTestFixture
.Create()
.WithBuild()
.Execute("JUnit.Xml.TestLogger.NetCore.Tests", loggerArgs, collectCoverage: false, "output-default-test-results.xml");

XDocument resultsXml = XDocument.Load(resultsFile);
var node = resultsXml.XPathSelectElement("/testsuites/testsuite/system-out");

Assert.IsTrue(node.Value.Contains("{2010CAE3-7BC0-4841-A5A3-7D5F947BB9FB}"));
Assert.IsTrue(node.Value.Contains("{998AC9EC-7429-42CD-AD55-72037E7AF3D8}"));
Assert.IsTrue(node.Value.Contains("{EEEE1DA6-6296-4486-BDA5-A50A19672F0F}"));
Assert.IsTrue(node.Value.Contains("{C33FF4B5-75E1-4882-B968-DF9608BFE7C2}"));
}

[TestMethod]
public void StoreConsoleOutput_Default_ContainsConsoleErr()
{
var loggerArgs = "junit;LogFilePath=output-default-test-results.xml";

var resultsFile = DotnetTestFixture
.Create()
.WithBuild()
.Execute("JUnit.Xml.TestLogger.NetCore.Tests", loggerArgs, collectCoverage: false, "output-default-test-results.xml");

XDocument resultsXml = XDocument.Load(resultsFile);
var node = resultsXml.XPathSelectElement("/testsuites/testsuite/system-err");

Assert.IsTrue(node.Value.Contains("{D46DFA10-EEDD-49E5-804D-FE43051331A7}"));
Assert.IsTrue(node.Value.Contains("{33F5FD22-6F40-499D-98E4-481D87FAEAA1}"));
}

[TestMethod]
public void StoreConsoleOutput_True_ContainsConsoleOut()
{
var loggerArgs = "junit;LogFilePath=output-true-test-results.xml;StoreConsoleOutput=true";

var resultsFile = DotnetTestFixture
.Create()
.WithBuild()
.Execute("JUnit.Xml.TestLogger.NetCore.Tests", loggerArgs, collectCoverage: false, "output-true-test-results.xml");

XDocument resultsXml = XDocument.Load(resultsFile);
var node = resultsXml.XPathSelectElement("/testsuites/testsuite/system-out");

Assert.IsTrue(node.Value.Contains("{2010CAE3-7BC0-4841-A5A3-7D5F947BB9FB}"));
Assert.IsTrue(node.Value.Contains("{998AC9EC-7429-42CD-AD55-72037E7AF3D8}"));
Assert.IsTrue(node.Value.Contains("{EEEE1DA6-6296-4486-BDA5-A50A19672F0F}"));
Assert.IsTrue(node.Value.Contains("{C33FF4B5-75E1-4882-B968-DF9608BFE7C2}"));
}

[TestMethod]
public void StoreConsoleOutput_True_ContainsConsoleErr()
{
var loggerArgs = "junit;LogFilePath=output-true-test-results.xml;StoreConsoleOutput=true";

var resultsFile = DotnetTestFixture
.Create()
.WithBuild()
.Execute("JUnit.Xml.TestLogger.NetCore.Tests", loggerArgs, collectCoverage: false, "output-true-test-results.xml");

XDocument resultsXml = XDocument.Load(resultsFile);
var node = resultsXml.XPathSelectElement("/testsuites/testsuite/system-err");

Assert.IsTrue(node.Value.Contains("{D46DFA10-EEDD-49E5-804D-FE43051331A7}"));
Assert.IsTrue(node.Value.Contains("{33F5FD22-6F40-499D-98E4-481D87FAEAA1}"));
}

[TestMethod]
public void StoreConsoleOutput_False_ContainsConsoleOut()
{
var loggerArgs = "junit;LogFilePath=output-false-test-results.xml;StoreConsoleOutput=false";

var resultsFile = DotnetTestFixture
.Create()
.WithBuild()
.Execute("JUnit.Xml.TestLogger.NetCore.Tests", loggerArgs, collectCoverage: false, "output-false-test-results.xml");

XDocument resultsXml = XDocument.Load(resultsFile);
var node = resultsXml.XPathSelectElement("/testsuites/testsuite/system-out");

Assert.IsTrue(node.Value.Equals(string.Empty));
}

[TestMethod]
public void StoreConsoleOutput_False_ContainsConsoleErr()
{
var loggerArgs = "junit;LogFilePath=output-false-test-results.xml;StoreConsoleOutput=false";

var resultsFile = DotnetTestFixture
.Create()
.WithBuild()
.Execute("JUnit.Xml.TestLogger.NetCore.Tests", loggerArgs, collectCoverage: false, "output-false-test-results.xml");

XDocument resultsXml = XDocument.Load(resultsFile);
var node = resultsXml.XPathSelectElement("/testsuites/testsuite/system-err");

Assert.IsTrue(node.Value.Equals(string.Empty));
}
}
}

0 comments on commit 63fc58f

Please sign in to comment.