From 284b10d6b26238976c181847e5a6df6ee2703e22 Mon Sep 17 00:00:00 2001 From: Tomas Matousek Date: Thu, 16 Feb 2017 15:22:04 -0800 Subject: [PATCH] Convert test stdout to xunit xml --- build/Toolset/Test.StdOutToXml.csx | 118 +++++++++++++++++++++++++++++ build/Toolset/Test.targets | 17 ++++- 2 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 build/Toolset/Test.StdOutToXml.csx diff --git a/build/Toolset/Test.StdOutToXml.csx b/build/Toolset/Test.StdOutToXml.csx new file mode 100644 index 0000000..ddb7171 --- /dev/null +++ b/build/Toolset/Test.StdOutToXml.csx @@ -0,0 +1,118 @@ +#r "System.Xml.Linq" +using System.IO; +using System.Xml.Linq; +using System.Text.RegularExpressions; + +if (Args.Count != 2) +{ + WriteLine("Usage: ConvertTestOutputToXml.csx "); + return 1; +} + +int passed = 0; +int failed = 0; +int skipped = 0; + +var lines = File.ReadAllLines(Args[0]); + +if (lines.Length == 0) +{ + WriteLine("No results found"); + return 2; +} + +string firstLine = lines[0]; +if (!firstLine.StartsWith("Test run for ")) +{ + WriteLine("Unexpected data"); + return 3; +} + +var collectionXml = new XElement("collection"); + +int i = 1; +while (i < lines.Length) +{ + string line = lines[i]; + + XElement failureXml; + string testName; + if (line.StartsWith("Passed")) + { + testName = line.Substring("Passed".Length).Trim(); + failureXml = null; + passed++; + i++; + } + else if (line.StartsWith("Skipped")) + { + testName = line.Substring("Skipped".Length).Trim(); + failureXml = null; + skipped++; + i++; + } + else if(line.StartsWith("Failed")) + { + testName = line.Substring("Failed".Length).Trim(); + failed++; + i++; + + var message = new StringBuilder(); + while (i < lines.Length && !lines[i].StartsWith("Passed") && !lines[i].StartsWith("Failed") && !lines[i].StartsWith("Stack Trace:")) + { + message.AppendLine(lines[i]); + i++; + } + + var stackTrace = new StringBuilder(); + while (i < lines.Length && !lines[i].StartsWith("Passed") && !lines[i].StartsWith("Failed")) + { + stackTrace.AppendLine(lines[i]); + i++; + } + + failureXml = new XElement("failure"); + var messageXml = new XElement("message"); + messageXml.SetValue(message.ToString()); + var stackTraceXml = new XElement("stack-trace"); + stackTraceXml.SetValue(stackTrace.ToString()); + failureXml.Add(messageXml); + failureXml.Add(stackTraceXml); + + } + else + { + i++; + continue; + } + + var testXml = new XElement("test"); + testXml.SetAttributeValue("name", testName); + testXml.SetAttributeValue("result", failureXml != null ? "Fail" : "Pass"); + if (failureXml != null) + { + testXml.Add(failureXml); + } + + collectionXml.Add(testXml); +} + +collectionXml.SetAttributeValue("total", (passed + failed + skipped).ToString()); +collectionXml.SetAttributeValue("passed", passed.ToString()); +collectionXml.SetAttributeValue("failed", failed.ToString()); +collectionXml.SetAttributeValue("skipped", skipped.ToString()); +collectionXml.SetAttributeValue("name", "all tests"); + +var xml = new XDocument(); +var assembliesXml = new XElement("assemblies"); +var assemblyXml = new XElement("assembly"); + +var parts = firstLine.Substring("Test run for ".Length).Split(new[] { '(', ')' }); +assemblyXml.SetAttributeValue("name", parts[0]); +assemblyXml.SetAttributeValue("environment", parts[1]); +assemblyXml.Add(collectionXml); + +assembliesXml.Add(assemblyXml); +xml.Add(assembliesXml); +xml.Save(Args[1]); + diff --git a/build/Toolset/Test.targets b/build/Toolset/Test.targets index 5d5acf5..b0ecb68 100644 --- a/build/Toolset/Test.targets +++ b/build/Toolset/Test.targets @@ -15,13 +15,16 @@ - <_TestLogPath>$(ArtifactsTestResultsDir)$(MSBuildProjectName)_$(TargetFramework)_%(_TestArchitectureItems.Identity).txt + <_TestOutPathNoExt>$(ArtifactsTestResultsDir)$(MSBuildProjectName)_$(TargetFramework)_%(_TestArchitectureItems.Identity) + <_TestStdOutPath>$(_TestOutPathNoExt).txt + <_TestLogPath>$(_TestOutPathNoExt).xml - + + - @@ -29,8 +32,14 @@ + + + - +