From cdc3755cf8bd3f308c2aae62e68aa0df0a6830de Mon Sep 17 00:00:00 2001 From: Carlos Parra Date: Thu, 23 Aug 2018 12:54:39 -0700 Subject: [PATCH 1/7] Added implementation for AddResultFile method from TestContext --- .../Services/ns10TestContextImplementation.cs | 18 ++++++++++++++++++ .../Extension.Shared/TestContext.cs | 8 ++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs index ed341ce37b..adb566886a 100644 --- a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs +++ b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs @@ -28,6 +28,11 @@ public class TestContextImplementation : UTF.TestContext, ITestContext private static readonly string FullyQualifiedTestClassNameLabel = "FullyQualifiedTestClassName"; private static readonly string TestNameLabel = "TestName"; + /// + /// List of result files associated with the test + /// + private IList testResultFiles; + /// /// Properties /// @@ -61,6 +66,8 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter writer, ID this.properties = new Dictionary(properties); this.stringWriter = writer; this.InitializeProperties(); + + this.testResultFiles = new List(); } #region TestContext impl @@ -131,6 +138,17 @@ public UTF.TestContext Context } } + public override void AddResultFile(string fileName) + { + if (string.IsNullOrEmpty(fileName)) + { + throw new ArgumentException("The parameter should not be null or empty.", "fileName"); + } + + // TODO: Update below line to call Path.GetFullPath(fileName); once GetFullPath method is available + this.testResultFiles.Add(fileName); + } + /// /// Set the unit-test outcome /// diff --git a/src/TestFramework/Extension.Shared/TestContext.cs b/src/TestFramework/Extension.Shared/TestContext.cs index 7902d37377..237659741d 100644 --- a/src/TestFramework/Extension.Shared/TestContext.cs +++ b/src/TestFramework/Extension.Shared/TestContext.cs @@ -56,6 +56,14 @@ public virtual UnitTestOutcome CurrentTestOutcome get { return UnitTestOutcome.Unknown; } } + /// + /// Adds a file name to the list in TestResult.ResultFileNames + /// + /// + /// The file Name. + /// + public abstract void AddResultFile(string fileName); + /// /// Used to write trace messages while the test is running /// From 7ead94c66bcb7bd636cc02973bfaebbae1474111 Mon Sep 17 00:00:00 2001 From: Carlos Parra Date: Thu, 23 Aug 2018 13:07:23 -0700 Subject: [PATCH 2/7] Added implementation for GetResultFiles method --- .../Services/ns10TestContextImplementation.cs | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs index adb566886a..aba2c49a9d 100644 --- a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs +++ b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs @@ -8,7 +8,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices using System.Diagnostics; using System.Globalization; using System.IO; - + using System.Linq; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel; @@ -138,6 +138,12 @@ public UTF.TestContext Context } } + /// + /// Adds a file name to the list in TestResult.ResultFileNames + /// + /// + /// The file Name. + /// public override void AddResultFile(string fileName) { if (string.IsNullOrEmpty(fileName)) @@ -146,6 +152,7 @@ public override void AddResultFile(string fileName) } // TODO: Update below line to call Path.GetFullPath(fileName); once GetFullPath method is available + // this.testResultFiles.Add(Path.GetFullPath(fileName)); this.testResultFiles.Add(fileName); } @@ -191,12 +198,21 @@ public void AddProperty(string propertyName, string propertyValue) } /// - /// Returning null as this feature is not supported in ASP .net and UWP + /// Result files attached /// - /// List of result files. Null presently. + /// List of result files generated in run. public IList GetResultFiles() { - return null; + if (this.testResultFiles.Count == 0) + { + return null; + } + + IList results = this.testResultFiles.ToList(); + + this.testResultFiles.Clear(); + + return results; } /// From f4b81d5e6773e0165658d4a43a4b5859c514804e Mon Sep 17 00:00:00 2001 From: Carlos Parra Date: Thu, 23 Aug 2018 14:44:44 -0700 Subject: [PATCH 3/7] Added unittests for AddResultFile and GetResultFile implementation --- .../ns10TestContextImplementationTests.cs | 75 ++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs b/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs index 61e860d7ad..fac36e1b1b 100644 --- a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs +++ b/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs @@ -18,13 +18,14 @@ namespace MSTestAdapter.PlatformServices.Tests.Services using UnitTestOutcome = FrameworkV2::Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome; #endif + using System; using System.Collections.Generic; using System.IO; using System.Linq; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices; - using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel; using Moq; + using MSTestAdapter.TestUtilities; using ITestMethod = Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel.ITestMethod; #pragma warning disable SA1649 // SA1649FileNameMustMatchTypeName @@ -268,6 +269,78 @@ public void ClearDiagnosticMessagesShouldClearMessagesFromWriteLine() Assert.AreEqual(string.Empty, stringWriter.ToString()); } + + [TestMethod] + public void AddResultFileShouldAddFiletoResultsFiles() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + this.testContextImplementation.AddResultFile("C:\\files\\myfile.txt"); + var resultFile = this.testContextImplementation.GetResultFiles(); + + CollectionAssert.Contains(resultFile.ToList(), "C:\\files\\myfile.txt"); + } + + [TestMethod] + public void AddResultFileShouldThrowIfFileNameIsNull() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + var exception = ActionUtility.PerformActionAndReturnException(() => this.testContextImplementation.AddResultFile(null)); + + Assert.AreEqual(typeof(ArgumentException), exception.GetType()); + StringAssert.Contains(exception.Message, "The parameter should not be null or empty."); + } + + [TestMethod] + public void AddResultFileShouldThrowIfFileNameIsEmpty() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + var exception = ActionUtility.PerformActionAndReturnException(() => this.testContextImplementation.AddResultFile(string.Empty)); + + Assert.AreEqual(typeof(ArgumentException), exception.GetType()); + StringAssert.Contains(exception.Message, "The parameter should not be null or empty."); + } + + [TestMethod] + public void AddResultFileShouldAddMultipleFilestoResultsFiles() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + this.testContextImplementation.AddResultFile("C:\\files\\file1.txt"); + this.testContextImplementation.AddResultFile("C:\\files\\files2.html"); + + var resultsFiles = this.testContextImplementation.GetResultFiles(); + + CollectionAssert.Contains(resultsFiles.ToList(), "C:\\files\\file1.txt"); + CollectionAssert.Contains(resultsFiles.ToList(), "C:\\files\\files2.html"); + } + + [TestMethod] + public void GetResultFilesShouldReturnNullIfNoAddedResultFiles() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + var resultFile = this.testContextImplementation.GetResultFiles(); + + Assert.IsNull(resultFile, "No added result files"); + } + + [TestMethod] + public void GetResultFilesShouldReturnListOfAddedResultFiles() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + this.testContextImplementation.AddResultFile("C:\\files\\myfile.txt"); + this.testContextImplementation.AddResultFile("C:\\files\\myfile2.txt"); + + var resultFiles = this.testContextImplementation.GetResultFiles(); + + Assert.IsTrue(resultFiles.Count > 0, "GetResultFiles returned added elements"); + CollectionAssert.Contains(resultFiles.ToList(), "C:\\files\\myfile.txt"); + CollectionAssert.Contains(resultFiles.ToList(), "C:\\files\\myfile2.txt"); + } } #pragma warning restore SA1649 // SA1649FileNameMustMatchTypeName From 764e141534f62893f763bcdf7516b090f77ce9a6 Mon Sep 17 00:00:00 2001 From: Carlos Parra Date: Wed, 24 Apr 2019 21:52:12 -0400 Subject: [PATCH 4/7] reverted old changes on ns1.0 implementation --- .../Services/ns10TestContextImplementation.cs | 54 +++----------- .../Extension.Shared/TestContext.cs | 8 --- .../ns10TestContextImplementationTests.cs | 72 ------------------- 3 files changed, 10 insertions(+), 124 deletions(-) diff --git a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs index aeabf09464..7fda7d37b4 100644 --- a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs +++ b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs @@ -9,8 +9,8 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices using System.Diagnostics; using System.Globalization; using System.IO; - using System.Threading; using System.Linq; + using System.Threading; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel; @@ -30,11 +30,6 @@ public class TestContextImplementation : UTF.TestContext, ITestContext private static readonly string FullyQualifiedTestClassNameLabel = "FullyQualifiedTestClassName"; private static readonly string TestNameLabel = "TestName"; - /// - /// List of result files associated with the test - /// - private IList testResultFiles; - /// /// Properties /// @@ -69,8 +64,6 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter writer, ID this.stringWriter = writer; this.CancellationTokenSource = new CancellationTokenSource(); this.InitializeProperties(); - - this.testResultFiles = new List(); } #region TestContext impl @@ -141,24 +134,6 @@ public UTF.TestContext Context } } - /// - /// Adds a file name to the list in TestResult.ResultFileNames - /// - /// - /// The file Name. - /// - public override void AddResultFile(string fileName) - { - if (string.IsNullOrEmpty(fileName)) - { - throw new ArgumentException("The parameter should not be null or empty.", "fileName"); - } - - // TODO: Update below line to call Path.GetFullPath(fileName); once GetFullPath method is available - // this.testResultFiles.Add(Path.GetFullPath(fileName)); - this.testResultFiles.Add(fileName); - } - /// /// Set the unit-test outcome /// @@ -200,24 +175,6 @@ public void AddProperty(string propertyName, string propertyValue) this.properties.Add(propertyName, propertyValue); } - /// - /// Result files attached - /// - /// List of result files generated in run. - public IList GetResultFiles() - { - if (this.testResultFiles.Count == 0) - { - return null; - } - - IList results = this.testResultFiles.ToList(); - - this.testResultFiles.Clear(); - - return results; - } - /// /// When overridden in a derived class, used to write trace messages while the /// test is running. @@ -265,6 +222,15 @@ public override void WriteLine(string format, params object[] args) } } + /// + /// Result files attached + /// + /// List of result files generated in run. + public IList GetResultFiles() + { + return null; + } + /// /// Gets messages from the testContext writeLines /// diff --git a/src/TestFramework/Extension.Shared/TestContext.cs b/src/TestFramework/Extension.Shared/TestContext.cs index 85fc97994c..73996020eb 100644 --- a/src/TestFramework/Extension.Shared/TestContext.cs +++ b/src/TestFramework/Extension.Shared/TestContext.cs @@ -48,14 +48,6 @@ public abstract class TestContext /// public virtual UnitTestOutcome CurrentTestOutcome => UnitTestOutcome.Unknown; - /// - /// Adds a file name to the list in TestResult.ResultFileNames - /// - /// - /// The file Name. - /// - public abstract void AddResultFile(string fileName); - /// /// Used to write trace messages while the test is running /// diff --git a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs b/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs index 7b386ca303..976cdd2a4a 100644 --- a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs +++ b/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs @@ -261,78 +261,6 @@ public void ClearDiagnosticMessagesShouldClearMessagesFromWriteLine() Assert.AreEqual(string.Empty, stringWriter.ToString()); } - - [TestMethod] - public void AddResultFileShouldAddFiletoResultsFiles() - { - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); - - this.testContextImplementation.AddResultFile("C:\\files\\myfile.txt"); - var resultFile = this.testContextImplementation.GetResultFiles(); - - CollectionAssert.Contains(resultFile.ToList(), "C:\\files\\myfile.txt"); - } - - [TestMethod] - public void AddResultFileShouldThrowIfFileNameIsNull() - { - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); - - var exception = ActionUtility.PerformActionAndReturnException(() => this.testContextImplementation.AddResultFile(null)); - - Assert.AreEqual(typeof(ArgumentException), exception.GetType()); - StringAssert.Contains(exception.Message, "The parameter should not be null or empty."); - } - - [TestMethod] - public void AddResultFileShouldThrowIfFileNameIsEmpty() - { - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); - - var exception = ActionUtility.PerformActionAndReturnException(() => this.testContextImplementation.AddResultFile(string.Empty)); - - Assert.AreEqual(typeof(ArgumentException), exception.GetType()); - StringAssert.Contains(exception.Message, "The parameter should not be null or empty."); - } - - [TestMethod] - public void AddResultFileShouldAddMultipleFilestoResultsFiles() - { - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); - - this.testContextImplementation.AddResultFile("C:\\files\\file1.txt"); - this.testContextImplementation.AddResultFile("C:\\files\\files2.html"); - - var resultsFiles = this.testContextImplementation.GetResultFiles(); - - CollectionAssert.Contains(resultsFiles.ToList(), "C:\\files\\file1.txt"); - CollectionAssert.Contains(resultsFiles.ToList(), "C:\\files\\files2.html"); - } - - [TestMethod] - public void GetResultFilesShouldReturnNullIfNoAddedResultFiles() - { - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); - - var resultFile = this.testContextImplementation.GetResultFiles(); - - Assert.IsNull(resultFile, "No added result files"); - } - - [TestMethod] - public void GetResultFilesShouldReturnListOfAddedResultFiles() - { - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); - - this.testContextImplementation.AddResultFile("C:\\files\\myfile.txt"); - this.testContextImplementation.AddResultFile("C:\\files\\myfile2.txt"); - - var resultFiles = this.testContextImplementation.GetResultFiles(); - - Assert.IsTrue(resultFiles.Count > 0, "GetResultFiles returned added elements"); - CollectionAssert.Contains(resultFiles.ToList(), "C:\\files\\myfile.txt"); - CollectionAssert.Contains(resultFiles.ToList(), "C:\\files\\myfile2.txt"); - } } #pragma warning restore SA1649 // SA1649FileNameMustMatchTypeName From 32505c9dd5a89747468cb0b72f785b32aafdadf2 Mon Sep 17 00:00:00 2001 From: Carlos Parra Date: Thu, 25 Apr 2019 00:42:20 -0400 Subject: [PATCH 5/7] implementing 'AddResultFile' for netcore --- .../NetCoreTestContextImplementation.cs | 34 ++++++++++++++++--- .../Services/ns10TestContextImplementation.cs | 20 +++++++++-- .../Extension.Core/NetCoreTestContext.cs | 8 +++++ 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/Adapter/PlatformServices.NetCore/Services/NetCoreTestContextImplementation.cs b/src/Adapter/PlatformServices.NetCore/Services/NetCoreTestContextImplementation.cs index 26011945cd..b2b8537fa3 100644 --- a/src/Adapter/PlatformServices.NetCore/Services/NetCoreTestContextImplementation.cs +++ b/src/Adapter/PlatformServices.NetCore/Services/NetCoreTestContextImplementation.cs @@ -10,6 +10,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; + using System.Linq; using System.Threading; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel; @@ -31,7 +32,7 @@ public class TestContextImplementation : UTF.TestContext, ITestContext /// /// List of result files associated with the test /// - private IList testResultFiles; + private readonly IList testResultFiles; /// /// Properties @@ -213,6 +214,22 @@ public UTF.TestContext Context } } + /// + /// Adds a file name to the list in TestResult.ResultFileNames + /// + /// + /// The file Name. + /// + public override void AddResultFile(string fileName) + { + if (string.IsNullOrEmpty(fileName)) + { + throw new ArgumentException("The parameter should not be null or empty.", "fileName"); + } + + this.testResultFiles.Add(Path.GetFullPath(fileName)); + } + /// /// Set the unit-test outcome /// @@ -255,12 +272,21 @@ public void AddProperty(string propertyName, string propertyValue) } /// - /// Returning null as this feature is not supported in ASP .net and UWP + /// Result files attached /// - /// List of result files. Null presently. + /// List of result files generated in run. public IList GetResultFiles() { - return null; + if (this.testResultFiles.Count == 0) + { + return null; + } + + IList results = this.testResultFiles.ToList(); + + this.testResultFiles.Clear(); + + return results; } /// diff --git a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs index 7fda7d37b4..09e46596d3 100644 --- a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs +++ b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs @@ -30,6 +30,11 @@ public class TestContextImplementation : UTF.TestContext, ITestContext private static readonly string FullyQualifiedTestClassNameLabel = "FullyQualifiedTestClassName"; private static readonly string TestNameLabel = "TestName"; + /// + /// List of result files associated with the test + /// + private readonly IList testResultFiles; + /// /// Properties /// @@ -62,6 +67,7 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter writer, ID this.testMethod = testMethod; this.properties = new Dictionary(properties); this.stringWriter = writer; + this.testResultFiles = new List(); this.CancellationTokenSource = new CancellationTokenSource(); this.InitializeProperties(); } @@ -134,6 +140,16 @@ public UTF.TestContext Context } } + public override void AddResultFile(string fileName) + { + if (string.IsNullOrEmpty(fileName)) + { + throw new ArgumentException("The parameter should not be null or empty.", "fileName"); + } + + this.testResultFiles.Add(Path.GetFullPath(fileName)); + } + /// /// Set the unit-test outcome /// @@ -223,9 +239,9 @@ public override void WriteLine(string format, params object[] args) } /// - /// Result files attached + /// Returns null as this feature is not supported in ASP .net and UWP /// - /// List of result files generated in run. + /// List of result files. Null presently. public IList GetResultFiles() { return null; diff --git a/src/TestFramework/Extension.Core/NetCoreTestContext.cs b/src/TestFramework/Extension.Core/NetCoreTestContext.cs index 644d75ce01..ed503ae80d 100644 --- a/src/TestFramework/Extension.Core/NetCoreTestContext.cs +++ b/src/TestFramework/Extension.Core/NetCoreTestContext.cs @@ -97,6 +97,14 @@ public abstract class TestContext /// public virtual UnitTestOutcome CurrentTestOutcome => UnitTestOutcome.Unknown; + /// + /// Adds a file name to the list in TestResult.ResultFileNames + /// + /// + /// The file Name. + /// + public abstract void AddResultFile(string fileName); + /// /// Used to write trace messages while the test is running /// From 65d99cabf3a1c152b56df933040031ae1e09252a Mon Sep 17 00:00:00 2001 From: Carlos Parra Date: Thu, 25 Apr 2019 21:35:44 -0400 Subject: [PATCH 6/7] fixed build issue and adding test for netcore testcontext impl --- .../Services/ns10TestContextImplementation.cs | 15 +- ...PlatformServices.NetCore.Unit.Tests.csproj | 2 +- .../NetCoreTestContextImplementationTests.cs | 340 ++++++++++++++++++ 3 files changed, 350 insertions(+), 7 deletions(-) create mode 100644 test/UnitTests/PlatformServices.Shared.Unit.Tests/netcore/NetCoreTestContextImplementationTests.cs diff --git a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs index 09e46596d3..1ff34545ef 100644 --- a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs +++ b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs @@ -140,14 +140,17 @@ public UTF.TestContext Context } } + /// + /// Adds a file name to the list in TestResult.ResultFileNames + /// + /// + /// The file Name. + /// public override void AddResultFile(string fileName) { - if (string.IsNullOrEmpty(fileName)) - { - throw new ArgumentException("The parameter should not be null or empty.", "fileName"); - } - - this.testResultFiles.Add(Path.GetFullPath(fileName)); + // No-op function + // will be replaced at runtime time by PlatformServices Desktop/NetCore + // depending the target framework } /// diff --git a/test/UnitTests/PlatformServices.NetCore.Unit.Tests/PlatformServices.NetCore.Unit.Tests.csproj b/test/UnitTests/PlatformServices.NetCore.Unit.Tests/PlatformServices.NetCore.Unit.Tests.csproj index fd9d5671fc..1be8af14f7 100644 --- a/test/UnitTests/PlatformServices.NetCore.Unit.Tests/PlatformServices.NetCore.Unit.Tests.csproj +++ b/test/UnitTests/PlatformServices.NetCore.Unit.Tests/PlatformServices.NetCore.Unit.Tests.csproj @@ -19,7 +19,7 @@ - + diff --git a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netcore/NetCoreTestContextImplementationTests.cs b/test/UnitTests/PlatformServices.Shared.Unit.Tests/netcore/NetCoreTestContextImplementationTests.cs new file mode 100644 index 0000000000..7b386ca303 --- /dev/null +++ b/test/UnitTests/PlatformServices.Shared.Unit.Tests/netcore/NetCoreTestContextImplementationTests.cs @@ -0,0 +1,340 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace MSTestAdapter.PlatformServices.Tests.Services +{ +#if NETCOREAPP1_1 + using Microsoft.VisualStudio.TestTools.UnitTesting; +#else + extern alias FrameworkV1; + extern alias FrameworkV2; + + using Assert = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.Assert; + using CollectionAssert = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert; + using StringAssert = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert; + using TestClass = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; + using TestInitialize = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute; + using TestMethod = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; + using UnitTestOutcome = FrameworkV2::Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome; +#endif + + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + + using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices; + using Moq; + using MSTestAdapter.TestUtilities; + using ITestMethod = Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel.ITestMethod; + + [TestClass] + public class TestContextImplementationTests + { + private Mock testMethod; + + private IDictionary properties; + + private TestContextImplementation testContextImplementation; + + [TestInitialize] + public void TestInit() + { + this.testMethod = new Mock(); + this.properties = new Dictionary(); + } + + [TestMethod] + public void TestContextConstructorShouldInitializeProperties() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + Assert.IsNotNull(this.testContextImplementation.Properties); + } + + [TestMethod] + public void TestContextConstructorShouldInitializeDefaultProperties() + { + this.testMethod.Setup(tm => tm.FullClassName).Returns("A.C.M"); + this.testMethod.Setup(tm => tm.Name).Returns("M"); + + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + Assert.IsNotNull(this.testContextImplementation.Properties); + + CollectionAssert.Contains( + this.testContextImplementation.Properties, + new KeyValuePair("FullyQualifiedTestClassName", "A.C.M")); + CollectionAssert.Contains( + this.testContextImplementation.Properties, + new KeyValuePair("TestName", "M")); + } + + [TestMethod] + public void CurrentTestOutcomeShouldReturnDefaultOutcome() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + Assert.AreEqual(UnitTestOutcome.Failed, this.testContextImplementation.CurrentTestOutcome); + } + + [TestMethod] + public void CurrentTestOutcomeShouldReturnOutcomeSet() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + this.testContextImplementation.SetOutcome(UnitTestOutcome.InProgress); + + Assert.AreEqual(UnitTestOutcome.InProgress, this.testContextImplementation.CurrentTestOutcome); + } + + [TestMethod] + public void FullyQualifiedTestClassNameShouldReturnTestMethodsFullClassName() + { + this.testMethod.Setup(tm => tm.FullClassName).Returns("A.C.M"); + + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + Assert.AreEqual("A.C.M", this.testContextImplementation.FullyQualifiedTestClassName); + } + + [TestMethod] + public void TestNameShouldReturnTestMethodsName() + { + this.testMethod.Setup(tm => tm.Name).Returns("M"); + + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + Assert.AreEqual("M", this.testContextImplementation.TestName); + } + + [TestMethod] + public void PropertiesShouldReturnPropertiesPassedToTestContext() + { + var property1 = new KeyValuePair("IntProperty", 1); + var property2 = new KeyValuePair("DoubleProperty", 2.023); + + this.properties.Add(property1); + this.properties.Add(property2); + + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + CollectionAssert.Contains(this.testContextImplementation.Properties, property1); + CollectionAssert.Contains(this.testContextImplementation.Properties, property2); + } + + [TestMethod] + public void ContextShouldReturnTestContextObject() + { + this.testMethod.Setup(tm => tm.Name).Returns("M"); + + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + Assert.IsNotNull(this.testContextImplementation.Context); + Assert.AreEqual("M", this.testContextImplementation.Context.TestName); + } + + [TestMethod] + public void TryGetPropertyValueShouldReturnTrueIfPropertyIsPresent() + { + this.testMethod.Setup(tm => tm.Name).Returns("M"); + + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + Assert.IsTrue(this.testContextImplementation.TryGetPropertyValue("TestName", out object propValue)); + Assert.AreEqual("M", propValue); + } + + [TestMethod] + public void TryGetPropertyValueShouldReturnFalseIfPropertyIsNotPresent() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + Assert.IsFalse(this.testContextImplementation.TryGetPropertyValue("Random", out object propValue)); + Assert.IsNull(propValue); + } + + [TestMethod] + public void AddPropertyShouldAddPropertiesToThePropertyBag() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + this.testContextImplementation.AddProperty("SomeNewProperty", "SomeValue"); + + CollectionAssert.Contains( + this.testContextImplementation.Properties, + new KeyValuePair("SomeNewProperty", "SomeValue")); + } + + [TestMethod] + public void WriteLineShouldWriteToStringWriter() + { + var stringWriter = new StringWriter(); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties); + + this.testContextImplementation.WriteLine("{0} Testing write", 1); + + StringAssert.Contains(stringWriter.ToString(), "1 Testing write"); + } + + [TestMethod] + public void WriteLineShouldWriteToStringWriterForNullCharacters() + { + var stringWriter = new StringWriter(); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties); + + this.testContextImplementation.WriteLine("{0} Testing \0 write \0", 1); + + StringAssert.Contains(stringWriter.ToString(), "1 Testing \\0 write \\0"); + } + + [TestMethod] + public void WriteLineShouldNotThrowIfStringWriterIsDisposed() + { + var stringWriter = new StringWriter(); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties); + + stringWriter.Dispose(); + + this.testContextImplementation.WriteLine("{0} Testing write", 1); + + // Calling it twice to cover the direct return when we know the object has been disposed. + this.testContextImplementation.WriteLine("{0} Testing write", 1); + } + + [TestMethod] + public void WriteLineWithMessageShouldWriteToStringWriter() + { + var stringWriter = new StringWriter(); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties); + + this.testContextImplementation.WriteLine("1 Testing write"); + + StringAssert.Contains(stringWriter.ToString(), "1 Testing write"); + } + + [TestMethod] + public void WriteLineWithMessageShouldWriteToStringWriterForNullCharacters() + { + var stringWriter = new StringWriter(); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties); + + this.testContextImplementation.WriteLine("1 Testing \0 write \0"); + + StringAssert.Contains(stringWriter.ToString(), "1 Testing \\0 write \\0"); + } + + [TestMethod] + public void WriteLineWithMessageShouldNotThrowIfStringWriterIsDisposed() + { + var stringWriter = new StringWriter(); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties); + + stringWriter.Dispose(); + + this.testContextImplementation.WriteLine("1 Testing write"); + + // Calling it twice to cover the direct return when we know the object has been disposed. + this.testContextImplementation.WriteLine("1 Testing write"); + } + + [TestMethod] + public void GetDiagnosticMessagesShouldReturnMessagesFromWriteLine() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + this.testContextImplementation.WriteLine("1 Testing write"); + this.testContextImplementation.WriteLine("2 Its a happy day"); + + StringAssert.Contains(this.testContextImplementation.GetDiagnosticMessages(), "1 Testing write"); + StringAssert.Contains(this.testContextImplementation.GetDiagnosticMessages(), "2 Its a happy day"); + } + + [TestMethod] + public void ClearDiagnosticMessagesShouldClearMessagesFromWriteLine() + { + var stringWriter = new StringWriter(); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties); + + this.testContextImplementation.WriteLine("1 Testing write"); + this.testContextImplementation.WriteLine("2 Its a happy day"); + + this.testContextImplementation.ClearDiagnosticMessages(); + + Assert.AreEqual(string.Empty, stringWriter.ToString()); + } + + [TestMethod] + public void AddResultFileShouldAddFiletoResultsFiles() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + this.testContextImplementation.AddResultFile("C:\\files\\myfile.txt"); + var resultFile = this.testContextImplementation.GetResultFiles(); + + CollectionAssert.Contains(resultFile.ToList(), "C:\\files\\myfile.txt"); + } + + [TestMethod] + public void AddResultFileShouldThrowIfFileNameIsNull() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + var exception = ActionUtility.PerformActionAndReturnException(() => this.testContextImplementation.AddResultFile(null)); + + Assert.AreEqual(typeof(ArgumentException), exception.GetType()); + StringAssert.Contains(exception.Message, "The parameter should not be null or empty."); + } + + [TestMethod] + public void AddResultFileShouldThrowIfFileNameIsEmpty() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + var exception = ActionUtility.PerformActionAndReturnException(() => this.testContextImplementation.AddResultFile(string.Empty)); + + Assert.AreEqual(typeof(ArgumentException), exception.GetType()); + StringAssert.Contains(exception.Message, "The parameter should not be null or empty."); + } + + [TestMethod] + public void AddResultFileShouldAddMultipleFilestoResultsFiles() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + this.testContextImplementation.AddResultFile("C:\\files\\file1.txt"); + this.testContextImplementation.AddResultFile("C:\\files\\files2.html"); + + var resultsFiles = this.testContextImplementation.GetResultFiles(); + + CollectionAssert.Contains(resultsFiles.ToList(), "C:\\files\\file1.txt"); + CollectionAssert.Contains(resultsFiles.ToList(), "C:\\files\\files2.html"); + } + + [TestMethod] + public void GetResultFilesShouldReturnNullIfNoAddedResultFiles() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + var resultFile = this.testContextImplementation.GetResultFiles(); + + Assert.IsNull(resultFile, "No added result files"); + } + + [TestMethod] + public void GetResultFilesShouldReturnListOfAddedResultFiles() + { + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); + + this.testContextImplementation.AddResultFile("C:\\files\\myfile.txt"); + this.testContextImplementation.AddResultFile("C:\\files\\myfile2.txt"); + + var resultFiles = this.testContextImplementation.GetResultFiles(); + + Assert.IsTrue(resultFiles.Count > 0, "GetResultFiles returned added elements"); + CollectionAssert.Contains(resultFiles.ToList(), "C:\\files\\myfile.txt"); + CollectionAssert.Contains(resultFiles.ToList(), "C:\\files\\myfile2.txt"); + } + } + +#pragma warning restore SA1649 // SA1649FileNameMustMatchTypeName + +} From 5cd99f89e70f23f73d6b9058bcaa874ef2f02b39 Mon Sep 17 00:00:00 2001 From: Carlos Parra Date: Sat, 27 Apr 2019 20:09:07 -0400 Subject: [PATCH 7/7] fix review comments --- .../Services/NetCoreTestContextImplementation.cs | 2 +- .../Services/ns10TestContextImplementation.cs | 7 ------- .../PlatformServices.NetCore.Unit.Tests.csproj | 1 - .../Services}/NetCoreTestContextImplementationTests.cs | 4 ++-- .../netstandard1.0/ns10TestContextImplementationTests.cs | 3 --- 5 files changed, 3 insertions(+), 14 deletions(-) rename test/UnitTests/{PlatformServices.Shared.Unit.Tests/netcore => PlatformServices.NetCore.Unit.Tests/Services}/NetCoreTestContextImplementationTests.cs (98%) diff --git a/src/Adapter/PlatformServices.NetCore/Services/NetCoreTestContextImplementation.cs b/src/Adapter/PlatformServices.NetCore/Services/NetCoreTestContextImplementation.cs index b2b8537fa3..c7802c2cde 100644 --- a/src/Adapter/PlatformServices.NetCore/Services/NetCoreTestContextImplementation.cs +++ b/src/Adapter/PlatformServices.NetCore/Services/NetCoreTestContextImplementation.cs @@ -224,7 +224,7 @@ public override void AddResultFile(string fileName) { if (string.IsNullOrEmpty(fileName)) { - throw new ArgumentException("The parameter should not be null or empty.", "fileName"); + throw new ArgumentException(Resource.Common_CannotBeNullOrEmpty, "fileName"); } this.testResultFiles.Add(Path.GetFullPath(fileName)); diff --git a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs index 1ff34545ef..7442e7301d 100644 --- a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs +++ b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs @@ -9,7 +9,6 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices using System.Diagnostics; using System.Globalization; using System.IO; - using System.Linq; using System.Threading; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel; @@ -30,11 +29,6 @@ public class TestContextImplementation : UTF.TestContext, ITestContext private static readonly string FullyQualifiedTestClassNameLabel = "FullyQualifiedTestClassName"; private static readonly string TestNameLabel = "TestName"; - /// - /// List of result files associated with the test - /// - private readonly IList testResultFiles; - /// /// Properties /// @@ -67,7 +61,6 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter writer, ID this.testMethod = testMethod; this.properties = new Dictionary(properties); this.stringWriter = writer; - this.testResultFiles = new List(); this.CancellationTokenSource = new CancellationTokenSource(); this.InitializeProperties(); } diff --git a/test/UnitTests/PlatformServices.NetCore.Unit.Tests/PlatformServices.NetCore.Unit.Tests.csproj b/test/UnitTests/PlatformServices.NetCore.Unit.Tests/PlatformServices.NetCore.Unit.Tests.csproj index 1be8af14f7..8d199cbd96 100644 --- a/test/UnitTests/PlatformServices.NetCore.Unit.Tests/PlatformServices.NetCore.Unit.Tests.csproj +++ b/test/UnitTests/PlatformServices.NetCore.Unit.Tests/PlatformServices.NetCore.Unit.Tests.csproj @@ -19,7 +19,6 @@ - diff --git a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netcore/NetCoreTestContextImplementationTests.cs b/test/UnitTests/PlatformServices.NetCore.Unit.Tests/Services/NetCoreTestContextImplementationTests.cs similarity index 98% rename from test/UnitTests/PlatformServices.Shared.Unit.Tests/netcore/NetCoreTestContextImplementationTests.cs rename to test/UnitTests/PlatformServices.NetCore.Unit.Tests/Services/NetCoreTestContextImplementationTests.cs index 7b386ca303..b375b9470c 100644 --- a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netcore/NetCoreTestContextImplementationTests.cs +++ b/test/UnitTests/PlatformServices.NetCore.Unit.Tests/Services/NetCoreTestContextImplementationTests.cs @@ -281,7 +281,7 @@ public void AddResultFileShouldThrowIfFileNameIsNull() var exception = ActionUtility.PerformActionAndReturnException(() => this.testContextImplementation.AddResultFile(null)); Assert.AreEqual(typeof(ArgumentException), exception.GetType()); - StringAssert.Contains(exception.Message, "The parameter should not be null or empty."); + StringAssert.Contains(exception.Message, Resource.Common_CannotBeNullOrEmpty); } [TestMethod] @@ -292,7 +292,7 @@ public void AddResultFileShouldThrowIfFileNameIsEmpty() var exception = ActionUtility.PerformActionAndReturnException(() => this.testContextImplementation.AddResultFile(string.Empty)); Assert.AreEqual(typeof(ArgumentException), exception.GetType()); - StringAssert.Contains(exception.Message, "The parameter should not be null or empty."); + StringAssert.Contains(exception.Message, Resource.Common_CannotBeNullOrEmpty); } [TestMethod] diff --git a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs b/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs index 976cdd2a4a..e0c26889da 100644 --- a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs +++ b/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs @@ -18,14 +18,11 @@ namespace MSTestAdapter.PlatformServices.Tests.Services using UnitTestOutcome = FrameworkV2::Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome; #endif - using System; using System.Collections.Generic; using System.IO; - using System.Linq; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices; using Moq; - using MSTestAdapter.TestUtilities; using ITestMethod = Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel.ITestMethod; [TestClass]