Skip to content

Commit

Permalink
feat: enable custom test output result file (#1112)
Browse files Browse the repository at this point in the history
* feat: enable TestOutputXmlFileNameWithoutExtension run setting to setup a custom result file name rather than the name of the assembly to be tested

* chore: adjust name to TestOutputXmlFileName

---------

Co-authored-by: Marcus Lorentschk <[email protected]>
  • Loading branch information
Lorilatschki and Lorilatschki authored Aug 3, 2023
1 parent 76b0d3d commit b3b5f4a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/NUnitTestAdapter/AdapterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public AdapterSettings(ITestLogger logger)
public string WorkDirectory { get; private set; }
public string Where { get; private set; }
public string TestOutputXml { get; private set; }
public string TestOutputXmlFileName { get; private set; }
public bool UseTestOutputXml => !string.IsNullOrEmpty(TestOutputXml) || OutputXmlFolderMode == OutputXmlFolderMode.UseResultDirectory;

public OutputXmlFolderMode OutputXmlFolderMode { get; private set; } = OutputXmlFolderMode.RelativeToWorkFolder;
Expand Down Expand Up @@ -329,6 +330,7 @@ public void Load(string settingsXml)
private void ParseOutputXml(XmlNode nunitNode)
{
TestOutputXml = GetInnerTextWithLog(nunitNode, nameof(TestOutputXml));
TestOutputXmlFileName = GetInnerTextWithLog(nunitNode, nameof(TestOutputXmlFileName));
if (Path.IsPathRooted(TestOutputXml))
{
OutputXmlFolderMode = OutputXmlFolderMode.AsSpecified;
Expand Down
1 change: 1 addition & 0 deletions src/NUnitTestAdapter/IAdapterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public interface IAdapterSettings

VsTestCategoryType VsTestCategoryType { get; }
string TestOutputXml { get; }
string TestOutputXmlFileName { get; }
bool UseTestOutputXml { get; }
OutputXmlFolderMode OutputXmlFolderMode { get; }

Expand Down
11 changes: 10 additions & 1 deletion src/NUnitTestAdapter/NUnitEngine/NUnitEngineAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void GenerateTestOutput(NUnitResults testResults, string assemblyPath, st
if (!settings.UseTestOutputXml)
return;

string path = GetXmlFilePath(testOutputXmlFolder, Path.GetFileNameWithoutExtension(assemblyPath), "xml");
string path = GetXmlFilePath(testOutputXmlFolder, GetTestOutputFileName(assemblyPath), "xml");
var resultService = GetService<IResultService>();

// Following null argument should work for nunit3 format. Empty array is OK as well.
Expand All @@ -173,6 +173,15 @@ public void GenerateTestOutput(NUnitResults testResults, string assemblyPath, st
logger.Info($" Test results written to {path}");
}

public string GetTestOutputFileName(string assemblyPath)
{
if (string.IsNullOrWhiteSpace(settings.TestOutputXmlFileName))
{
return Path.GetFileNameWithoutExtension(assemblyPath);
}
return settings.TestOutputXmlFileName;
}

public string GetXmlFilePath(string folder, string defaultFileName, string extension)
{
if (!settings.NewOutputXmlFileForEachRun)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public void TestXmlFileNameGeneration()
Assert.That(path, Is.EqualTo("c:/assembly.xml"));
}

[TestCase("", "myAssemblyFilename")]
[TestCase("<TestOutputXmlFileName>whateverFileName</TestOutputXmlFileName>", "whateverFileName")]
public void When_setting_up_defined_result_GetTestOutputFileName_returns_defined_filename(string testOutputXmlFileNameWithoutExtensionXmlNode, string expectedFileName)
{
var logger = Substitute.For<ITestLogger>();
var settings = new AdapterSettings(logger);
settings.Load($@"<RunSettings><NUnit><WorkDirectory>c:\whatever</WorkDirectory><TestOutputXml>/my/work/dir</TestOutputXml>{testOutputXmlFileNameWithoutExtensionXmlNode}</NUnit></RunSettings>");
var sut = new NUnitEngineAdapter();
sut.InitializeSettingsAndLogging(settings, logger);
string filename = sut.GetTestOutputFileName("c:/myAssemblyFilename.dll");
Assert.That(filename, Is.EqualTo(expectedFileName));
}

[Test]
public void TestXmlFileNameGenerationNewOutputXmlFileForEachRun()
{
Expand Down

0 comments on commit b3b5f4a

Please sign in to comment.