From 4544fb8fcd987e53790c5bda5cd251a015a518d7 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Wed, 12 Feb 2020 07:38:40 -0500 Subject: [PATCH] [XHarness] Add NUnit3 support to the old bcl tests. (#7846) Move the old bcl tests based on NUnit to use the NUnit V3 output so that we can add logs to the vsts imported tests. Follow up of PR: https://github.com/xamarin/xamarin-macios/pull/7844 --- tests/bcl-test/BCLTests-mac.csproj.in | 6 - tests/bcl-test/BCLTests-tv.csproj.in | 6 - .../BCLTests-watchos-extension.csproj.in | 6 - tests/bcl-test/BCLTests.csproj.in | 6 - .../templates/common/ApplicationOptions.cs | 14 +- .../common/TestRunner.Core/TestRunner.cs | 13 +- .../TestRunner.NUnit/NUnitTestRunner.cs | 38 +- .../TestRunner.NUnit/XmlOutputWriter.cs | 349 ------------------ .../TestRunner.xUnit/XUnitResultFileFormat.cs | 9 - .../TestRunner.xUnit/XUnitTestRunner.cs | 29 +- .../templates/iOSApp/ViewController.cs | 20 +- tests/bcl-test/templates/macOS/MacTestMain.cs | 2 +- .../templates/tvOSApp/ViewController.cs | 16 +- .../watchOS/Extension/InterfaceController.cs | 16 +- 14 files changed, 104 insertions(+), 426 deletions(-) delete mode 100644 tests/bcl-test/templates/common/TestRunner.NUnit/XmlOutputWriter.cs delete mode 100644 tests/bcl-test/templates/common/TestRunner.xUnit/XUnitResultFileFormat.cs diff --git a/tests/bcl-test/BCLTests-mac.csproj.in b/tests/bcl-test/BCLTests-mac.csproj.in index 70a59e6206..684e74731b 100644 --- a/tests/bcl-test/BCLTests-mac.csproj.in +++ b/tests/bcl-test/BCLTests-mac.csproj.in @@ -101,9 +101,6 @@ TestRunner.NUnit\ClassOrNamespaceFilter.cs - - TestRunner.NUnit\XmlOutputWriter.cs - TestRunner.NUnit\TestMethodFilter.cs @@ -146,9 +143,6 @@ TestRunner.xUnit\XUnitFilterType.cs - - TestRunner.xUnit\XUnitResultFileFormat.cs - TestRunner.xUnit\XUnitTestRunner.cs diff --git a/tests/bcl-test/BCLTests-tv.csproj.in b/tests/bcl-test/BCLTests-tv.csproj.in index 9ad5fb0a1b..f407026998 100644 --- a/tests/bcl-test/BCLTests-tv.csproj.in +++ b/tests/bcl-test/BCLTests-tv.csproj.in @@ -176,9 +176,6 @@ TestRunner.NUnit\TestMethodFilter.cs - - TestRunner.NUnit\XmlOutputWriter.cs - TestRunner.Core\Extensions.Bool.cs @@ -218,9 +215,6 @@ TestRunner.xUnit\XUnitFilterType.cs - - TestRunner.xUnit\XUnitResultFileFormat.cs - TestRunner.xUnit\XUnitTestRunner.cs diff --git a/tests/bcl-test/BCLTests-watchos-extension.csproj.in b/tests/bcl-test/BCLTests-watchos-extension.csproj.in index f5e04663a8..691ee366ef 100644 --- a/tests/bcl-test/BCLTests-watchos-extension.csproj.in +++ b/tests/bcl-test/BCLTests-watchos-extension.csproj.in @@ -206,18 +206,12 @@ TestRunner.NUnit\TestMethodFilter.cs - - TestRunner.NUnit\XmlOutputWriter.cs - TestRunner.xUnit\XUnitFilter.cs TestRunner.xUnit\XUnitFilterType.cs - - TestRunner.xUnit\XUnitResultFileFormat.cs - TestRunner.xUnit\XUnitTestRunner.cs diff --git a/tests/bcl-test/BCLTests.csproj.in b/tests/bcl-test/BCLTests.csproj.in index 8d66f9a838..32b7d87b5d 100644 --- a/tests/bcl-test/BCLTests.csproj.in +++ b/tests/bcl-test/BCLTests.csproj.in @@ -189,9 +189,6 @@ TestRunner.NUnit\TestMethodFilter.cs - - TestRunner.NUnit\XmlOutputWriter.cs - TestRunner.Core\Extensions.Bool.cs @@ -231,9 +228,6 @@ TestRunner.xUnit\XUnitFilterType.cs - - TestRunner.xUnit\XUnitResultFileFormat.cs - TestRunner.xUnit\XUnitTestRunner.cs diff --git a/tests/bcl-test/templates/common/ApplicationOptions.cs b/tests/bcl-test/templates/common/ApplicationOptions.cs index 9c2212294c..10a773c794 100644 --- a/tests/bcl-test/templates/common/ApplicationOptions.cs +++ b/tests/bcl-test/templates/common/ApplicationOptions.cs @@ -1,7 +1,8 @@ -// Modified version of the TouchOptions found in +// Modified version of the TouchOptions found in using System; using Foundation; using Mono.Options; +using Xamarin.iOS.UnitTests; namespace BCLTests { @@ -10,6 +11,11 @@ namespace BCLTests { Wrapped = 1, } + public enum XmlVersion { + NUnitV2 = 0, + NUnitV3 = 1, + } + public class ApplicationOptions { static public ApplicationOptions Current = new ApplicationOptions (); @@ -49,6 +55,9 @@ namespace BCLTests { var xml_mode = Environment.GetEnvironmentVariable ("NUNIT_ENABLE_XML_MODE"); if (!string.IsNullOrEmpty (xml_mode)) XmlMode = (XmlMode) Enum.Parse (typeof (XmlMode), xml_mode, true); + var xml_version = Environment.GetEnvironmentVariable ("NUNIT_XML_VERSION"); + if (!string.IsNullOrEmpty (xml_version)) + XmlVersion = (XmlVersion)Enum.Parse (typeof (XmlVersion), xml_version, true); if (!string.IsNullOrEmpty (Environment.GetEnvironmentVariable ("NUNIT_LOG_FILE"))) LogFile = Environment.GetEnvironmentVariable ("NUNIT_LOG_FILE"); @@ -61,6 +70,7 @@ namespace BCLTests { { "transport=", "Select transport method. Either TCP (default), HTTP or FILE.", v => Transport = v }, { "enablexml", "Enable the xml reported.", v => EnableXml = false }, { "xmlmode", "The xml mode.", v => XmlMode = (XmlMode) Enum.Parse (typeof (XmlMode), v, false) }, + { "xmlversion", "The xml version.", v => XmlVersion = (XmlVersion) Enum.Parse (typeof (XmlVersion), v, false) }, { "logfile=", "A path where output will be saved.", v => LogFile = v }, { "result=", "The path to be used to store the result", v => ResultFile = v}, }; @@ -76,6 +86,8 @@ namespace BCLTests { public XmlMode XmlMode { get; set; } + public XmlVersion XmlVersion { get; set; } = XmlVersion.NUnitV2; + public bool EnableXml { get; set; } public string HostName { get; private set; } diff --git a/tests/bcl-test/templates/common/TestRunner.Core/TestRunner.cs b/tests/bcl-test/templates/common/TestRunner.Core/TestRunner.cs index a15b90ce92..74c3f73aef 100644 --- a/tests/bcl-test/templates/common/TestRunner.Core/TestRunner.cs +++ b/tests/bcl-test/templates/common/TestRunner.Core/TestRunner.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Reflection; @@ -9,6 +9,13 @@ namespace Xamarin.iOS.UnitTests { public abstract class TestRunner { + public enum Jargon { + TouchUnit, + NUnitV2, + NUnitV3, + xUnit, + } + public long InconclusiveTests { get; protected set; } = 0; public long FailedTests { get; protected set; } = 0; public long PassedTests { get; protected set; } = 0; @@ -32,8 +39,8 @@ namespace Xamarin.iOS.UnitTests } public abstract Task Run (IEnumerable testAssemblies); - public abstract string WriteResultsToFile (); - public abstract void WriteResultsToFile (TextWriter writer); + public abstract string WriteResultsToFile (Jargon jargon); + public abstract void WriteResultsToFile (TextWriter writer, Jargon jargon); public abstract void SkipTests (IEnumerable tests); public abstract void SkipCategories (IEnumerable categories); diff --git a/tests/bcl-test/templates/common/TestRunner.NUnit/NUnitTestRunner.cs b/tests/bcl-test/templates/common/TestRunner.NUnit/NUnitTestRunner.cs index 9ce39af845..9be9beaa21 100644 --- a/tests/bcl-test/templates/common/TestRunner.NUnit/NUnitTestRunner.cs +++ b/tests/bcl-test/templates/common/TestRunner.NUnit/NUnitTestRunner.cs @@ -12,7 +12,7 @@ using Foundation; using NUnit.Framework.Api; using NUnit.Framework.Internal; using NUnit.Framework.Internal.Filters; - +using NUnitLite.Runner; using NUnitTest = NUnit.Framework.Internal.Test; namespace Xamarin.iOS.UnitTests.NUnit @@ -251,7 +251,7 @@ namespace Xamarin.iOS.UnitTests.NUnit } } - public override string WriteResultsToFile () + public override string WriteResultsToFile (Jargon jargon) { if (results == null) return string.Empty; @@ -259,20 +259,40 @@ namespace Xamarin.iOS.UnitTests.NUnit string ret = GetResultsFilePath (); if (string.IsNullOrEmpty (ret)) return string.Empty; - - - var resultsXml = new NUnit2XmlOutputWriter (DateTime.UtcNow); - resultsXml.WriteResultFile (results, ret); + + OutputWriter formatter; + switch (jargon) { + case Jargon.NUnitV2: + formatter = new NUnit2XmlOutputWriter (DateTime.UtcNow); + break; + case Jargon.NUnitV3: + formatter = new NUnit3XmlOutputWriter (DateTime.UtcNow); + break; + default: + throw new InvalidOperationException ($"Jargon {jargon} is not supported by this runner."); + } + + formatter.WriteResultFile (results, ret); return ret; } - public override void WriteResultsToFile (TextWriter writer) + public override void WriteResultsToFile (TextWriter writer, Jargon jargon) { if (results == null) return; - var resultsXml = new NUnit2XmlOutputWriter (DateTime.UtcNow); - resultsXml.WriteResultFile (results, writer); + OutputWriter formatter; + switch (jargon) { + case Jargon.NUnitV2: + formatter = new NUnit2XmlOutputWriter (DateTime.UtcNow); + break; + case Jargon.NUnitV3: + formatter = new NUnit3XmlOutputWriter (DateTime.UtcNow); + break; + default: + throw new InvalidOperationException ($"Jargon {jargon} is not supported by this runner."); + } + formatter.WriteResultFile (results, writer); } void AppendFilter (ITestFilter filter) diff --git a/tests/bcl-test/templates/common/TestRunner.NUnit/XmlOutputWriter.cs b/tests/bcl-test/templates/common/TestRunner.NUnit/XmlOutputWriter.cs deleted file mode 100644 index 4476fd90d6..0000000000 --- a/tests/bcl-test/templates/common/TestRunner.NUnit/XmlOutputWriter.cs +++ /dev/null @@ -1,349 +0,0 @@ -// *********************************************************************** -// Copyright (c) 2011 Charlie Poole -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// *********************************************************************** - -using System; -using System.Globalization; -using System.Reflection; -using System.Xml; -using System.IO; -using NUnit.Framework.Api; -using NUnit.Framework.Internal; -using NUnitLite.Runner; -#if CLR_2_0 || CLR_4_0 -using System.Collections.Generic; -#else -using System.Collections.Specialized; -#endif - -namespace Xamarin.iOS.UnitTests.NUnit { - /// - /// NUnit2XmlOutputWriter is able to create an xml file representing - /// the result of a test run in NUnit 2.x format. - /// - public class NUnit2XmlOutputWriter : OutputWriter { - private XmlWriter xmlWriter; - private DateTime startTime; - -#if CLR_2_0 || CLR_4_0 - private static Dictionary resultStates = new Dictionary(); -#else - private static StringDictionary resultStates = new StringDictionary (); -#endif - - static NUnit2XmlOutputWriter () - { - resultStates ["Passed"] = "Success"; - resultStates ["Failed"] = "Failure"; - resultStates ["Failed:Error"] = "Error"; - resultStates ["Failed:Cancelled"] = "Cancelled"; - resultStates ["Inconclusive"] = "Inconclusive"; - resultStates ["Skipped"] = "Skipped"; - resultStates ["Skipped:Ignored"] = "Ignored"; - resultStates ["Skipped:Invalid"] = "NotRunnable"; - } - - public NUnit2XmlOutputWriter (DateTime startTime) - { - this.startTime = startTime; - } - - /// - /// Writes the result of a test run to a specified TextWriter. - /// - /// The test result for the run - /// The TextWriter to which the xml will be written - public override void WriteResultFile (ITestResult result, TextWriter writer) - { - // NOTE: Under .NET 1.1, XmlTextWriter does not implement IDisposable, - // but does implement Close(). Hence we cannot use a 'using' clause. - //using (XmlTextWriter xmlWriter = new XmlTextWriter(writer)) -#if SILVERLIGHT - XmlWriter xmlWriter = XmlWriter.Create(writer); -#else - XmlTextWriter xmlWriter = new XmlTextWriter (writer); - xmlWriter.Formatting = Formatting.Indented; -#endif - - try { - WriteXmlOutput (result, xmlWriter); - } finally { - writer.Close (); - } - } - - private void WriteXmlOutput (ITestResult result, XmlWriter xmlWriter) - { - this.xmlWriter = xmlWriter; - - InitializeXmlFile (result); - WriteResultElement (result); - TerminateXmlFile (); - } - - private void InitializeXmlFile (ITestResult result) - { - ResultSummary summaryResults = new ResultSummary (result); - - xmlWriter.WriteStartDocument (false); - xmlWriter.WriteComment ("This file represents the results of running a test suite"); - - xmlWriter.WriteStartElement ("test-results"); - - xmlWriter.WriteAttributeString ("name", result.FullName); - xmlWriter.WriteAttributeString ("total", summaryResults.TestCount.ToString ()); - xmlWriter.WriteAttributeString ("errors", summaryResults.ErrorCount.ToString ()); - xmlWriter.WriteAttributeString ("failures", summaryResults.FailureCount.ToString ()); - xmlWriter.WriteAttributeString ("not-run", summaryResults.NotRunCount.ToString ()); - xmlWriter.WriteAttributeString ("inconclusive", summaryResults.InconclusiveCount.ToString ()); - xmlWriter.WriteAttributeString ("ignored", summaryResults.IgnoreCount.ToString ()); - xmlWriter.WriteAttributeString ("skipped", summaryResults.SkipCount.ToString ()); - xmlWriter.WriteAttributeString ("invalid", summaryResults.InvalidCount.ToString ()); - - xmlWriter.WriteAttributeString ("date", XmlConvert.ToString (startTime, "yyyy-MM-dd")); - xmlWriter.WriteAttributeString ("time", XmlConvert.ToString (startTime, "HH:mm:ss")); - WriteEnvironment (); - WriteCultureInfo (); - xmlWriter.Flush (); - } - - private void WriteCultureInfo () - { - xmlWriter.WriteStartElement ("culture-info"); - xmlWriter.WriteAttributeString ("current-culture", - CultureInfo.CurrentCulture.ToString ()); - xmlWriter.WriteAttributeString ("current-uiculture", - CultureInfo.CurrentUICulture.ToString ()); - xmlWriter.WriteEndElement (); - xmlWriter.Flush (); - } - - private void WriteEnvironment () - { - xmlWriter.WriteStartElement ("environment"); - AssemblyName assemblyName = AssemblyHelper.GetAssemblyName (Assembly.GetExecutingAssembly ()); - xmlWriter.WriteAttributeString ("nunit-version", - assemblyName.Version.ToString ()); - xmlWriter.WriteAttributeString ("clr-version", - Environment.Version.ToString ()); - xmlWriter.WriteAttributeString ("os-version", - Environment.OSVersion.ToString ()); - xmlWriter.WriteAttributeString ("platform", - Environment.OSVersion.Platform.ToString ()); -#if !NETCF - xmlWriter.WriteAttributeString ("cwd", - Environment.CurrentDirectory); -#if !SILVERLIGHT - xmlWriter.WriteAttributeString ("machine-name", - Environment.MachineName); - xmlWriter.WriteAttributeString ("user", - Environment.UserName); - xmlWriter.WriteAttributeString ("user-domain", - Environment.UserDomainName); -#endif -#endif - xmlWriter.WriteEndElement (); - xmlWriter.Flush (); - } - - private void WriteResultElement (ITestResult result) - { - StartTestElement (result); - - WriteProperties (result); - - switch (result.ResultState.Status) { - case TestStatus.Skipped: - WriteReasonElement (result.Message); - break; - case TestStatus.Failed: - WriteFailureElement (result.Message, result.StackTrace); - break; - } - - if (result.Test is TestSuite) - WriteChildResults (result); - - xmlWriter.WriteEndElement (); // test element - xmlWriter.Flush (); - } - - private void TerminateXmlFile () - { - xmlWriter.WriteEndElement (); // test-results - xmlWriter.WriteEndDocument (); - xmlWriter.Flush (); - xmlWriter.Close (); - } - - - #region Element Creation Helpers - - private void StartTestElement (ITestResult result) - { - ITest test = result.Test; - TestSuite suite = test as TestSuite; - - if (suite != null) { - xmlWriter.WriteStartElement ("test-suite"); - xmlWriter.WriteAttributeString ("type", suite.TestType); - xmlWriter.WriteAttributeString ("name", suite.TestType == "Assembly" - ? result.Test.FullName - : result.Test.Name); - } else { - xmlWriter.WriteStartElement ("test-case"); - xmlWriter.WriteAttributeString ("name", result.Name); - } - - if (test.Properties.ContainsKey (PropertyNames.Description)) { - string description = (string)test.Properties.Get (PropertyNames.Description); - xmlWriter.WriteAttributeString ("description", description); - } - - TestStatus status = result.ResultState.Status; - string translatedResult = resultStates [result.ResultState.ToString ()]; - - if (status != TestStatus.Skipped) { - xmlWriter.WriteAttributeString ("executed", "True"); - xmlWriter.WriteAttributeString ("result", translatedResult); - xmlWriter.WriteAttributeString ("success", status == TestStatus.Passed ? "True" : "False"); - xmlWriter.WriteAttributeString ("time", result.Duration.TotalSeconds.ToString ()); - xmlWriter.WriteAttributeString ("asserts", result.AssertCount.ToString ()); - } else { - xmlWriter.WriteAttributeString ("executed", "False"); - xmlWriter.WriteAttributeString ("result", translatedResult); - } - } - - private void WriteProperties (ITestResult result) - { - IPropertyBag properties = result.Test.Properties; - int nprops = 0; - - foreach (string key in properties.Keys) { - if (key != PropertyNames.Category) { - if (nprops++ == 0) - xmlWriter.WriteStartElement ("properties"); - - foreach (object prop in properties [key]) { - xmlWriter.WriteStartElement ("property"); - xmlWriter.WriteAttributeString ("name", key); - xmlWriter.WriteAttributeString ("value", prop.ToString ()); - xmlWriter.WriteEndElement (); - } - } - } - - if (nprops > 0) - xmlWriter.WriteEndElement (); - } - - private void WriteReasonElement (string message) - { - xmlWriter.WriteStartElement ("reason"); - xmlWriter.WriteStartElement ("message"); - xmlWriter.WriteCData (message); - xmlWriter.WriteEndElement (); - xmlWriter.WriteEndElement (); - } - - private void WriteFailureElement (string message, string stackTrace) - { - xmlWriter.WriteStartElement ("failure"); - xmlWriter.WriteStartElement ("message"); - WriteCData (message); - xmlWriter.WriteEndElement (); - xmlWriter.WriteStartElement ("stack-trace"); - if (stackTrace != null) - WriteCData (stackTrace); - xmlWriter.WriteEndElement (); - xmlWriter.WriteEndElement (); - } - - private void WriteChildResults (ITestResult result) - { - xmlWriter.WriteStartElement ("results"); - - foreach (ITestResult childResult in result.Children) - WriteResultElement (childResult); - - xmlWriter.WriteEndElement (); - } - - #endregion - - #region Output Helpers - ///// - ///// Makes string safe for xml parsing, replacing control chars with '?' - ///// - ///// string to make safe - ///// xml safe string - //private static string CharacterSafeString(string encodedString) - //{ - // /*The default code page for the system will be used. - // Since all code pages use the same lower 128 bytes, this should be sufficient - // for finding uprintable control characters that make the xslt processor error. - // We use characters encoded by the default code page to avoid mistaking bytes as - // individual characters on non-latin code pages.*/ - // char[] encodedChars = System.Text.Encoding.Default.GetChars(System.Text.Encoding.Default.GetBytes(encodedString)); - - // System.Collections.ArrayList pos = new System.Collections.ArrayList(); - // for (int x = 0; x < encodedChars.Length; x++) - // { - // char currentChar = encodedChars[x]; - // //unprintable characters are below 0x20 in Unicode tables - // //some control characters are acceptable. (carriage return 0x0D, line feed 0x0A, horizontal tab 0x09) - // if (currentChar < 32 && (currentChar != 9 && currentChar != 10 && currentChar != 13)) - // { - // //save the array index for later replacement. - // pos.Add(x); - // } - // } - // foreach (int index in pos) - // { - // encodedChars[index] = '?';//replace unprintable control characters with ?(3F) - // } - // return System.Text.Encoding.Default.GetString(System.Text.Encoding.Default.GetBytes(encodedChars)); - //} - - private void WriteCData (string text) - { - int start = 0; - while (true) { - int illegal = text.IndexOf ("]]>", start); - if (illegal < 0) - break; - xmlWriter.WriteCData (text.Substring (start, illegal - start + 2)); - start = illegal + 2; - if (start >= text.Length) - return; - } - - if (start > 0) - xmlWriter.WriteCData (text.Substring (start)); - else - xmlWriter.WriteCData (text); - } - - #endregion - } -} \ No newline at end of file diff --git a/tests/bcl-test/templates/common/TestRunner.xUnit/XUnitResultFileFormat.cs b/tests/bcl-test/templates/common/TestRunner.xUnit/XUnitResultFileFormat.cs deleted file mode 100644 index 2d4f9616a2..0000000000 --- a/tests/bcl-test/templates/common/TestRunner.xUnit/XUnitResultFileFormat.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Xamarin.iOS.UnitTests.XUnit -{ - public enum XUnitResultFileFormat - { - XunitV2, - XunitV1, - NUnit - } -} \ No newline at end of file diff --git a/tests/bcl-test/templates/common/TestRunner.xUnit/XUnitTestRunner.cs b/tests/bcl-test/templates/common/TestRunner.xUnit/XUnitTestRunner.cs index ffcd4da80e..3ef9a97927 100644 --- a/tests/bcl-test/templates/common/TestRunner.xUnit/XUnitTestRunner.cs +++ b/tests/bcl-test/templates/common/TestRunner.xUnit/XUnitTestRunner.cs @@ -23,7 +23,6 @@ namespace Xamarin.iOS.UnitTests.XUnit List filters = new List (); bool runAssemblyByDefault; - public XUnitResultFileFormat ResultFileFormat { get; set; } = XUnitResultFileFormat.XunitV2; public AppDomainSupport AppDomainSupport { get; set; } = AppDomainSupport.Denied; protected override string ResultsFileName { get; set; } = "TestResults.xUnit.xml"; @@ -791,7 +790,7 @@ namespace Xamarin.iOS.UnitTests.XUnit } } - public override string WriteResultsToFile () + public override string WriteResultsToFile (Jargon jargon) { if (assembliesElement == null) return String.Empty; @@ -800,22 +799,20 @@ namespace Xamarin.iOS.UnitTests.XUnit string outputFilePath = GetResultsFilePath (); var settings = new XmlWriterSettings { Indent = true}; using (var xmlWriter = XmlWriter.Create (outputFilePath, settings)) { - switch (ResultFileFormat) { - case XUnitResultFileFormat.XunitV2: - assembliesElement.Save (xmlWriter); - break; - case XUnitResultFileFormat.NUnit: + switch (jargon) { + case Jargon.NUnitV2: Transform_Results ("NUnitXml.xslt", assembliesElement, xmlWriter); // TODO: Add resource break; - default: - throw new InvalidOperationException ($"Result output format '{ResultFileFormat}' is not currently supported"); + default: // default to xunit until we implement NUnit v3 support. + assembliesElement.Save (xmlWriter); + break; } } return outputFilePath; } - public override void WriteResultsToFile (TextWriter writer) + public override void WriteResultsToFile (TextWriter writer, Jargon jargon) { if (assembliesElement == null) return; @@ -823,11 +820,8 @@ namespace Xamarin.iOS.UnitTests.XUnit assembliesElement.Descendants ().Where (e => e.Name == "collection" && !e.Descendants ().Any ()).Remove (); var settings = new XmlWriterSettings { Indent = true }; using (var xmlWriter = XmlWriter.Create (writer, settings)) { - switch (ResultFileFormat) { - case XUnitResultFileFormat.XunitV2: - assembliesElement.Save (xmlWriter); - break; - case XUnitResultFileFormat.NUnit: + switch (jargon) { + case Jargon.NUnitV2: try { Transform_Results ("NUnitXml.xslt", assembliesElement, xmlWriter); } catch (Exception e) { @@ -835,8 +829,9 @@ namespace Xamarin.iOS.UnitTests.XUnit } break; - default: - throw new InvalidOperationException ($"Result output format '{ResultFileFormat}' is not currently supported"); + default: // defualt to xunit until we add NUnitv3 support + assembliesElement.Save (xmlWriter); + break; } } } diff --git a/tests/bcl-test/templates/iOSApp/ViewController.cs b/tests/bcl-test/templates/iOSApp/ViewController.cs index b127f4b98a..c1ce0e30ed 100644 --- a/tests/bcl-test/templates/iOSApp/ViewController.cs +++ b/tests/bcl-test/templates/iOSApp/ViewController.cs @@ -1,5 +1,4 @@ -using System; -using System.Reflection; +using System; using System.Collections.Generic; using UIKit; @@ -9,11 +8,8 @@ using Xamarin.iOS.UnitTests; using Xamarin.iOS.UnitTests.NUnit; using BCLTests.TestRunner.Core; using Xamarin.iOS.UnitTests.XUnit; -using System.IO; -using System.Threading.Tasks; using System.Linq; using Foundation; -using NUnit.Framework.Internal.Filters; namespace BCLTests { public partial class ViewController : UIViewController { @@ -95,11 +91,21 @@ namespace BCLTests { } await runner.Run (testAssemblies).ConfigureAwait (false); + Xamarin.iOS.UnitTests.TestRunner.Jargon jargon = Xamarin.iOS.UnitTests.TestRunner.Jargon.NUnitV3; + switch (options.XmlVersion) { + default: + case XmlVersion.NUnitV2: + jargon = Xamarin.iOS.UnitTests.TestRunner.Jargon.NUnitV2; + break; + case XmlVersion.NUnitV3: + jargon = Xamarin.iOS.UnitTests.TestRunner.Jargon.NUnitV3; + break; + } if (options.EnableXml) { - runner.WriteResultsToFile (writer); + runner.WriteResultsToFile (writer, jargon); logger.Info ("Xml file was written to the tcp listener."); } else { - string resultsFilePath = runner.WriteResultsToFile (); + string resultsFilePath = runner.WriteResultsToFile (jargon); logger.Info ($"Xml result can be found {resultsFilePath}"); } diff --git a/tests/bcl-test/templates/macOS/MacTestMain.cs b/tests/bcl-test/templates/macOS/MacTestMain.cs index b43648c7be..0b944ead70 100644 --- a/tests/bcl-test/templates/macOS/MacTestMain.cs +++ b/tests/bcl-test/templates/macOS/MacTestMain.cs @@ -62,7 +62,7 @@ namespace Xamarin.Mac.Tests if (options.ResultFile != null) { using (var writer = new StreamWriter (options.ResultFile)) { - runner.WriteResultsToFile (writer); + runner.WriteResultsToFile (writer, TestRunner.Jargon.NUnitV3); } logger.Info ($"Xml result can be found {options.ResultFile}"); } diff --git a/tests/bcl-test/templates/tvOSApp/ViewController.cs b/tests/bcl-test/templates/tvOSApp/ViewController.cs index 9b738ea218..6fd0a0a288 100644 --- a/tests/bcl-test/templates/tvOSApp/ViewController.cs +++ b/tests/bcl-test/templates/tvOSApp/ViewController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; using System.Collections.Generic; @@ -86,11 +86,21 @@ namespace BCLTests { } await runner.Run (testAssemblies).ConfigureAwait (false); + Xamarin.iOS.UnitTests.TestRunner.Jargon jargon = Xamarin.iOS.UnitTests.TestRunner.Jargon.NUnitV3; + switch (options.XmlVersion) { + default: + case XmlVersion.NUnitV2: + jargon = Xamarin.iOS.UnitTests.TestRunner.Jargon.NUnitV2; + break; + case XmlVersion.NUnitV3: + jargon = Xamarin.iOS.UnitTests.TestRunner.Jargon.NUnitV3; + break; + } if (options.EnableXml) { - runner.WriteResultsToFile (writer); + runner.WriteResultsToFile (writer, jargon); logger.Info ("Xml file was written to the tcp listener."); } else { - string resultsFilePath = runner.WriteResultsToFile (); + string resultsFilePath = runner.WriteResultsToFile (jargon); logger.Info ($"Xml result can be found {resultsFilePath}"); } diff --git a/tests/bcl-test/templates/watchOS/Extension/InterfaceController.cs b/tests/bcl-test/templates/watchOS/Extension/InterfaceController.cs index 796656ee7d..db39ece98e 100644 --- a/tests/bcl-test/templates/watchOS/Extension/InterfaceController.cs +++ b/tests/bcl-test/templates/watchOS/Extension/InterfaceController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Linq; using System.Threading; @@ -131,11 +131,21 @@ namespace monotouchtestWatchKitExtension RenderResults (); cmdRun.SetEnabled (true); cmdRun.SetHidden (false); + Xamarin.iOS.UnitTests.TestRunner.Jargon jargon = Xamarin.iOS.UnitTests.TestRunner.Jargon.NUnitV3; + switch (options.XmlVersion) { + default: + case XmlVersion.NUnitV2: + jargon = Xamarin.iOS.UnitTests.TestRunner.Jargon.NUnitV2; + break; + case XmlVersion.NUnitV3: + jargon = Xamarin.iOS.UnitTests.TestRunner.Jargon.NUnitV3; + break; + } if (options.EnableXml) { - runner.WriteResultsToFile (writer); + runner.WriteResultsToFile (writer, jargon); logger.Info ("Xml file was written to the http listener."); } else { - string resultsFilePath = runner.WriteResultsToFile (); + string resultsFilePath = runner.WriteResultsToFile (jargon); logger.Info ($"Xml result can be found {resultsFilePath}"); } logger.Info ($"Tests run: {runner.TotalTests} Passed: {runner.PassedTests} Inconclusive: {runner.InconclusiveTests} Failed: {runner.FailedTests} Ignored: {runner.FilteredTests}");