From fbd60d6a6a4c7797ec53849d572d726d1ecced6e Mon Sep 17 00:00:00 2001 From: Shiva Shankar Thangadurai Date: Thu, 7 Feb 2019 01:28:38 -0800 Subject: [PATCH 1/5] Ensured that the TestContext exposed is always IDictionary for back compat reasons. --- .../Services/ns10TestContextImplementation.cs | 5 ++- .../Extension.Shared/TestContext.cs | 35 ++++----------- .../Execution/TestExecutionManagerTests.cs | 2 +- .../ns10TestContextImplementationTests.cs | 43 ++++++++----------- 4 files changed, 31 insertions(+), 54 deletions(-) diff --git a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs index ed341ce37b..48d1ffd431 100644 --- a/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs +++ b/src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10TestContextImplementation.cs @@ -4,6 +4,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices { using System; + using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -115,11 +116,11 @@ public override string TestName /// An System.Collections.IDictionary object that contains key/value pairs that /// represent the test properties. /// - public override IDictionary Properties + public override IDictionary Properties { get { - return this.properties as IDictionary; + return this.properties as IDictionary; } } diff --git a/src/TestFramework/Extension.Shared/TestContext.cs b/src/TestFramework/Extension.Shared/TestContext.cs index 7902d37377..225a7f3c18 100644 --- a/src/TestFramework/Extension.Shared/TestContext.cs +++ b/src/TestFramework/Extension.Shared/TestContext.cs @@ -4,6 +4,7 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting { using System; + using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -18,7 +19,7 @@ public abstract class TestContext /// /// Gets test properties for a test. /// - public abstract IDictionary Properties { get; } + public abstract IDictionary Properties { get; } /// /// Gets Fully-qualified name of the class containing the test method currently being executed @@ -29,32 +30,17 @@ public abstract class TestContext /// in the test results. Users can benefit from messages that include the fully-qualified /// class name in addition to the name of the test method currently being executed. /// - public virtual string FullyQualifiedTestClassName - { - get - { - return this.GetProperty("FullyQualifiedTestClassName"); - } - } + public virtual string FullyQualifiedTestClassName => this.GetProperty("FullyQualifiedTestClassName"); /// /// Gets the Name of the test method currently being executed /// - public virtual string TestName - { - get - { - return this.GetProperty("TestName"); - } - } + public virtual string TestName => this.GetProperty("TestName"); /// /// Gets the current test outcome. /// - public virtual UnitTestOutcome CurrentTestOutcome - { - get { return UnitTestOutcome.Unknown; } - } + public virtual UnitTestOutcome CurrentTestOutcome => UnitTestOutcome.Unknown; /// /// Used to write trace messages while the test is running @@ -72,21 +58,18 @@ public virtual UnitTestOutcome CurrentTestOutcome private T GetProperty(string name) where T : class { - object o; - - if (!this.Properties.TryGetValue(name, out o)) + if (!((IDictionary)this.Properties).TryGetValue(name, out object propertyValue)) { return null; } - if (o != null && !(o is T)) + if (propertyValue != null && !(propertyValue is T)) { // If o has a value, but it's not the right type - Debug.Assert(false, "How did an invalid value get in here?"); - throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, FrameworkMessages.InvalidPropertyType, name, o.GetType(), typeof(T))); + throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, FrameworkMessages.InvalidPropertyType, name, propertyValue.GetType(), typeof(T))); } - return (T)o; + return (T)propertyValue; } } } diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestExecutionManagerTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestExecutionManagerTests.cs index 737129604c..f144f36826 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestExecutionManagerTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestExecutionManagerTests.cs @@ -883,7 +883,7 @@ public static IDictionary TestContextProperties [UTF.TestCategory("Foo")] public void PassingTest() { - TestContextProperties = this.TestContext.Properties; + TestContextProperties = this.TestContext.Properties as IDictionary; } [UTF.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 61e860d7ad..1e703cca8d 100644 --- a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs +++ b/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs @@ -23,7 +23,6 @@ namespace MSTestAdapter.PlatformServices.Tests.Services using System.Linq; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices; - using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel; using Moq; using ITestMethod = Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel.ITestMethod; @@ -48,7 +47,7 @@ public void TestInit() [TestMethod] public void TestContextConstructorShouldInitializeProperties() { - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); Assert.IsNotNull(this.testContextImplementation.Properties); } @@ -59,22 +58,22 @@ 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 System.IO.StringWriter(), this.properties); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); Assert.IsNotNull(this.testContextImplementation.Properties); CollectionAssert.Contains( - this.testContextImplementation.Properties.ToList(), + ((IDictionary)this.testContextImplementation.Properties).ToList(), new KeyValuePair("FullyQualifiedTestClassName", "A.C.M")); CollectionAssert.Contains( - this.testContextImplementation.Properties.ToList(), + ((IDictionary)this.testContextImplementation.Properties).ToList(), new KeyValuePair("TestName", "M")); } [TestMethod] public void CurrentTestOutcomeShouldReturnDefaultOutcome() { - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); Assert.AreEqual(UnitTestOutcome.Failed, this.testContextImplementation.CurrentTestOutcome); } @@ -82,7 +81,7 @@ public void CurrentTestOutcomeShouldReturnDefaultOutcome() [TestMethod] public void CurrentTestOutcomeShouldReturnOutcomeSet() { - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); this.testContextImplementation.SetOutcome(UnitTestOutcome.InProgress); @@ -94,7 +93,7 @@ public void FullyQualifiedTestClassNameShouldReturnTestMethodsFullClassName() { this.testMethod.Setup(tm => tm.FullClassName).Returns("A.C.M"); - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); Assert.AreEqual("A.C.M", this.testContextImplementation.FullyQualifiedTestClassName); } @@ -104,7 +103,7 @@ public void TestNameShouldReturnTestMethodsName() { this.testMethod.Setup(tm => tm.Name).Returns("M"); - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); Assert.AreEqual("M", this.testContextImplementation.TestName); } @@ -118,10 +117,10 @@ public void PropertiesShouldReturnPropertiesPassedToTestContext() this.properties.Add(property1); this.properties.Add(property2); - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); - CollectionAssert.Contains(this.testContextImplementation.Properties.ToList(), property1); - CollectionAssert.Contains(this.testContextImplementation.Properties.ToList(), property2); + CollectionAssert.Contains(((IDictionary)this.testContextImplementation.Properties).ToList(), property1); + CollectionAssert.Contains(((IDictionary)this.testContextImplementation.Properties).ToList(), property2); } [TestMethod] @@ -129,7 +128,7 @@ public void ContextShouldReturnTestContextObject() { this.testMethod.Setup(tm => tm.Name).Returns("M"); - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); Assert.IsNotNull(this.testContextImplementation.Context); Assert.AreEqual("M", this.testContextImplementation.Context.TestName); @@ -140,34 +139,28 @@ public void TryGetPropertyValueShouldReturnTrueIfPropertyIsPresent() { this.testMethod.Setup(tm => tm.Name).Returns("M"); - this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); - - object propValue; - - Assert.IsTrue(this.testContextImplementation.TryGetPropertyValue("TestName", out propValue)); + 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 System.IO.StringWriter(), this.properties); - - object propValue; - - Assert.IsFalse(this.testContextImplementation.TryGetPropertyValue("Random", out propValue)); + 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 System.IO.StringWriter(), this.properties); + this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); this.testContextImplementation.AddProperty("SomeNewProperty", "SomeValue"); CollectionAssert.Contains( - this.testContextImplementation.Properties.ToList(), + ((IDictionary)this.testContextImplementation.Properties).ToList(), new KeyValuePair("SomeNewProperty", "SomeValue")); } From fb8ae9233ec2928f3b0f3f538254a4ac4e530f51 Mon Sep 17 00:00:00 2001 From: Shiva Shankar Thangadurai Date: Thu, 7 Feb 2019 03:40:02 -0800 Subject: [PATCH 2/5] Fixed a build failure due to TestContext signature changes --- .../MSTest.CoreAdapter.Unit.Tests/Execution/TypeCacheTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TypeCacheTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TypeCacheTests.cs index db1c33b763..8e2e4c8806 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TypeCacheTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TypeCacheTests.cs @@ -922,7 +922,7 @@ public void GetTestMethodInfoShouldSetTestContextWithCustomProperty() new Dictionary()); this.typeCache.GetTestMethodInfo(testMethod, testContext, false); - var customProperty = testContext.Properties.FirstOrDefault(p => p.Key.Equals("WhoAmI")); + var customProperty = ((IDictionary)testContext.Properties).FirstOrDefault(p => p.Key.Equals("WhoAmI")); Assert.IsNotNull(customProperty); Assert.AreEqual("Me", customProperty.Value); @@ -1009,7 +1009,7 @@ public void GetTestMethodInfoShouldNotAddDuplicateTestPropertiesToTestContext() // Verify that the first value gets set. object value; - Assert.IsTrue(testContext.Properties.TryGetValue("WhoAmI", out value)); + Assert.IsTrue(((IDictionary)testContext.Properties).TryGetValue("WhoAmI", out value)); Assert.AreEqual("Me", value); } From f613e411b296213733542aacedd9d1b7622e3e9a Mon Sep 17 00:00:00 2001 From: Shiva Shankar Thangadurai Date: Fri, 8 Feb 2019 00:20:09 -0800 Subject: [PATCH 3/5] Fixed a test failure --- .../PlatformServiceProviderTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/PlatformServiceProviderTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/PlatformServiceProviderTests.cs index a65a2226a9..3de25de523 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/PlatformServiceProviderTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/PlatformServiceProviderTests.cs @@ -94,7 +94,8 @@ public void GetTestContextShouldReturnAValidTestContext() // Assert. Assert.AreEqual("A.C.M", testContext.Context.FullyQualifiedTestClassName); Assert.AreEqual("M", testContext.Context.TestName); - Assert.IsTrue(testContext.Context.Properties.Contains(properties.ToArray()[0])); + Assert.IsTrue(testContext.Context.Properties.Contains(properties.ToArray()[0].Key)); + Assert.IsTrue(((IDictionary)testContext.Context.Properties).Contains(properties.ToArray()[0])); } } } From 4c01db8cabc9ff1a9a62f3d0c0aea5616ee6db8b Mon Sep 17 00:00:00 2001 From: Shiva Shankar Thangadurai Date: Fri, 8 Feb 2019 02:07:32 -0800 Subject: [PATCH 4/5] Updated the desktop implementation as well. --- .../Services/DesktopTestContextImplementation.cs | 4 ++-- src/TestFramework/Extension.Desktop/TestContext.cs | 3 ++- .../Services/DesktopTestContextImplTests.cs | 12 ++++++------ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Adapter/PlatformServices.Desktop/Services/DesktopTestContextImplementation.cs b/src/Adapter/PlatformServices.Desktop/Services/DesktopTestContextImplementation.cs index e5ae798c05..b625eebbfc 100644 --- a/src/Adapter/PlatformServices.Desktop/Services/DesktopTestContextImplementation.cs +++ b/src/Adapter/PlatformServices.Desktop/Services/DesktopTestContextImplementation.cs @@ -118,11 +118,11 @@ public override DataRow DataRow } /// - public override IDictionary Properties + public override IDictionary Properties { get { - return this.properties; + return this.properties as IDictionary; } } diff --git a/src/TestFramework/Extension.Desktop/TestContext.cs b/src/TestFramework/Extension.Desktop/TestContext.cs index 2cefc01d8f..a6f019bbcc 100644 --- a/src/TestFramework/Extension.Desktop/TestContext.cs +++ b/src/TestFramework/Extension.Desktop/TestContext.cs @@ -4,6 +4,7 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting { using System; + using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.Common; @@ -19,7 +20,7 @@ public abstract class TestContext /// /// Gets test properties for a test. /// - public abstract IDictionary Properties { get; } + public abstract IDictionary Properties { get; } /// /// Gets the current data row when test is used for data driven testing. diff --git a/test/UnitTests/PlatformServices.Desktop.Unit.Tests/Services/DesktopTestContextImplTests.cs b/test/UnitTests/PlatformServices.Desktop.Unit.Tests/Services/DesktopTestContextImplTests.cs index 005b9c1c3d..068909eb94 100644 --- a/test/UnitTests/PlatformServices.Desktop.Unit.Tests/Services/DesktopTestContextImplTests.cs +++ b/test/UnitTests/PlatformServices.Desktop.Unit.Tests/Services/DesktopTestContextImplTests.cs @@ -59,10 +59,10 @@ public void TestContextConstructorShouldInitializeDefaultProperties() Assert.IsNotNull(this.testContextImplementation.Properties); CollectionAssert.Contains( - this.testContextImplementation.Properties.ToList(), + ((IDictionary)this.testContextImplementation.Properties).ToList(), new KeyValuePair("FullyQualifiedTestClassName", "A.C.M")); CollectionAssert.Contains( - this.testContextImplementation.Properties.ToList(), + ((IDictionary)this.testContextImplementation.Properties).ToList(), new KeyValuePair("TestName", "M")); } @@ -115,8 +115,9 @@ public void PropertiesShouldReturnPropertiesPassedToTestContext() this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); - CollectionAssert.Contains(this.testContextImplementation.Properties.ToList(), property1); - CollectionAssert.Contains(this.testContextImplementation.Properties.ToList(), property2); + var propertyList = ((IDictionary)this.testContextImplementation.Properties).ToList(); + CollectionAssert.Contains(propertyList, property1); + CollectionAssert.Contains(propertyList, property2); } [TestMethod] @@ -158,11 +159,10 @@ public void TryGetPropertyValueShouldReturnFalseIfPropertyIsNotPresent() public void AddPropertyShouldAddPropertiesToThePropertyBag() { this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); - this.testContextImplementation.AddProperty("SomeNewProperty", "SomeValue"); CollectionAssert.Contains( - this.testContextImplementation.Properties.ToList(), + ((IDictionary)this.testContextImplementation.Properties).ToList(), new KeyValuePair("SomeNewProperty", "SomeValue")); } From d83d693892e564f331642fc165a6e38306afd63e Mon Sep 17 00:00:00 2001 From: Shiva Shankar Thangadurai Date: Fri, 8 Feb 2019 02:27:55 -0800 Subject: [PATCH 5/5] Removed some redudant cast and ToList operations in the test code. --- .../Services/DesktopTestContextImplTests.cs | 11 +++++------ .../ns10TestContextImplementationTests.cs | 10 +++++----- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/test/UnitTests/PlatformServices.Desktop.Unit.Tests/Services/DesktopTestContextImplTests.cs b/test/UnitTests/PlatformServices.Desktop.Unit.Tests/Services/DesktopTestContextImplTests.cs index 068909eb94..e5d26ae794 100644 --- a/test/UnitTests/PlatformServices.Desktop.Unit.Tests/Services/DesktopTestContextImplTests.cs +++ b/test/UnitTests/PlatformServices.Desktop.Unit.Tests/Services/DesktopTestContextImplTests.cs @@ -59,10 +59,10 @@ public void TestContextConstructorShouldInitializeDefaultProperties() Assert.IsNotNull(this.testContextImplementation.Properties); CollectionAssert.Contains( - ((IDictionary)this.testContextImplementation.Properties).ToList(), + this.testContextImplementation.Properties, new KeyValuePair("FullyQualifiedTestClassName", "A.C.M")); CollectionAssert.Contains( - ((IDictionary)this.testContextImplementation.Properties).ToList(), + this.testContextImplementation.Properties, new KeyValuePair("TestName", "M")); } @@ -115,9 +115,8 @@ public void PropertiesShouldReturnPropertiesPassedToTestContext() this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties); - var propertyList = ((IDictionary)this.testContextImplementation.Properties).ToList(); - CollectionAssert.Contains(propertyList, property1); - CollectionAssert.Contains(propertyList, property2); + CollectionAssert.Contains(this.testContextImplementation.Properties, property1); + CollectionAssert.Contains(this.testContextImplementation.Properties, property2); } [TestMethod] @@ -162,7 +161,7 @@ public void AddPropertyShouldAddPropertiesToThePropertyBag() this.testContextImplementation.AddProperty("SomeNewProperty", "SomeValue"); CollectionAssert.Contains( - ((IDictionary)this.testContextImplementation.Properties).ToList(), + this.testContextImplementation.Properties, new KeyValuePair("SomeNewProperty", "SomeValue")); } 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 1e703cca8d..6e02a56289 100644 --- a/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs +++ b/test/UnitTests/PlatformServices.Shared.Unit.Tests/netstandard1.0/ns10TestContextImplementationTests.cs @@ -63,10 +63,10 @@ public void TestContextConstructorShouldInitializeDefaultProperties() Assert.IsNotNull(this.testContextImplementation.Properties); CollectionAssert.Contains( - ((IDictionary)this.testContextImplementation.Properties).ToList(), + this.testContextImplementation.Properties, new KeyValuePair("FullyQualifiedTestClassName", "A.C.M")); CollectionAssert.Contains( - ((IDictionary)this.testContextImplementation.Properties).ToList(), + this.testContextImplementation.Properties, new KeyValuePair("TestName", "M")); } @@ -119,8 +119,8 @@ public void PropertiesShouldReturnPropertiesPassedToTestContext() this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties); - CollectionAssert.Contains(((IDictionary)this.testContextImplementation.Properties).ToList(), property1); - CollectionAssert.Contains(((IDictionary)this.testContextImplementation.Properties).ToList(), property2); + CollectionAssert.Contains(this.testContextImplementation.Properties, property1); + CollectionAssert.Contains(this.testContextImplementation.Properties, property2); } [TestMethod] @@ -160,7 +160,7 @@ public void AddPropertyShouldAddPropertiesToThePropertyBag() this.testContextImplementation.AddProperty("SomeNewProperty", "SomeValue"); CollectionAssert.Contains( - ((IDictionary)this.testContextImplementation.Properties).ToList(), + this.testContextImplementation.Properties, new KeyValuePair("SomeNewProperty", "SomeValue")); }