From 27a17d6fb0fa0cb5233f904c741a3ba453a6a189 Mon Sep 17 00:00:00 2001 From: Steve Ashman Date: Thu, 11 Apr 2019 21:59:54 -0400 Subject: [PATCH 1/7] add ResolveArguments to handle methods with optional arguments. --- .../Execution/TestMethodInfo.cs | 43 ++++++++++++++++++- .../Attributes/DataRowAttribute.cs | 8 ++++ .../DataRowTestProject/BaseClass.cs | 19 ++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs b/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs index 059adf2d0a..8d930103fd 100644 --- a/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs +++ b/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs @@ -171,7 +171,48 @@ public virtual TestResult Invoke(object[] arguments) internal void SetArguments(object[] arguments) { - this.arguments = arguments; + this.arguments = this.ResolveArguments(arguments); + } + + internal object[] ResolveArguments(object[] arguments) + { + ParameterInfo[] parameterInfos = this.TestMethod.GetParameters(); + int requiredParameterCount = 0; + foreach (var parameter in parameterInfos) + { + if (!parameter.IsOptional) + { + requiredParameterCount++; + } + } + + if (requiredParameterCount == parameterInfos.Length) + { + return arguments; + } + + if (arguments == null) + { + return null; + } + + if (arguments.Length < requiredParameterCount || arguments.Length > parameterInfos.Length) + { + return arguments; + } + + object[] newParameters = new object[parameterInfos.Length]; + for (int i = 0; i < arguments.Length; i++) + { + newParameters[i] = arguments[i]; + } + + for (int i = arguments.Length; i < parameterInfos.Length; i++) + { + newParameters[i] = parameterInfos[i].DefaultValue; + } + + return newParameters; } /// diff --git a/src/TestFramework/MSTest.Core/Attributes/DataRowAttribute.cs b/src/TestFramework/MSTest.Core/Attributes/DataRowAttribute.cs index 93832693eb..05b3e57e1e 100644 --- a/src/TestFramework/MSTest.Core/Attributes/DataRowAttribute.cs +++ b/src/TestFramework/MSTest.Core/Attributes/DataRowAttribute.cs @@ -15,6 +15,14 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public class DataRowAttribute : Attribute, ITestDataSource { + /// + /// Initializes a new instance of the class. + /// + public DataRowAttribute() + { + this.Data = new object[0]; + } + /// /// Initializes a new instance of the class. /// diff --git a/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs b/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs index 75183e05a5..293e2a19c3 100644 --- a/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs +++ b/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs @@ -16,5 +16,24 @@ public virtual void DataRowTestMethod(string a) { Assert.IsTrue(true); } + + [TestMethod] + [DataRow(42)] + [DataRow(42,"OptionalString1")] + [DataRow(42,"OptionalString1","OptionalString2")] + public virtual void DataRowTestMethodWithSomeOptionalParameters(int i, string s1 = null, string s2 = null) + { + Assert.IsTrue(true); + } + + [TestMethod] + [DataRow()] + [DataRow(42)] + [DataRow(42, "OptionalString1")] + [DataRow(42, "OptionalString1", "OptionalString2")] + public virtual void DataRowTestMethodWithAllOptionalParameters(int i = 0, string s1 = null, string s2 = null) + { + Assert.IsTrue(true); + } } } From c3f0968023f889bb6407c7ae50c55aa576befc3e Mon Sep 17 00:00:00 2001 From: Steve Ashman Date: Sat, 20 Apr 2019 09:59:46 -0400 Subject: [PATCH 2/7] update E2E unit test for new DataRow base/derived class tests Signed-off-by: Steve Ashman --- test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs | 43 +++++++++++++++---- .../DataRowTestProject/BaseClass.cs | 10 +++-- .../DataRowTestProject/DerivedClass.cs | 18 ++++++++ .../Attributes/DataRowAttributeTests.cs | 8 ++++ 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs b/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs index 6967befb57..df3e772669 100644 --- a/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs +++ b/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs @@ -21,12 +21,31 @@ public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRow "DataRowTestMethod (BaseString2)", "DataRowTestMethod (BaseString3)", "DataRowTestMethod (DerivedString1)", - "DataRowTestMethod (DerivedString2)"); + "DataRowTestMethod (DerivedString2)", + "DataRowTestMethodWithSomeOptionalParameters (42)", + "DataRowTestMethodWithSomeOptionalParameters (42,BaseOptionalString1)", + "DataRowTestMethodWithSomeOptionalParameters (42,BaseOptionalString2,BaseOptionalString3)", + "DataRowTestMethodWithSomeOptionalParameters (42,BaseOptionalString4,BaseOptionalString5)", + "DataRowTestMethodWithSomeOptionalParameters (123)", + "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString1)", + "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString2,DerivedOptionalString3)", + "DataRowTestMethodWithAllOptionalParameters ()", + "DataRowTestMethodWithAllOptionalParameters (42)", + "DataRowTestMethodWithAllOptionalParameters (42,BaseOptionalString6)", + "DataRowTestMethodWithAllOptionalParameters (42,BaseOptionalString7,BaseOptionalString8)", + "DataRowTestMethodWithAllOptionalParameters (42,BaseOptionalString9,BaseOptionalString10)", + "DataRowTestMethodWithAllOptionalParameters (123)", + "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString4)", + "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString5,DerivedOptionalString6)"); - // 4 tests of BaseClass - 3 datarow result and 1 parent result - // 3 tests of DerivedClass - 2 datarow result and 1 parent result - // Total 7 tests - Making sure that DerivedClass doesn't run BaseClass tests - this.ValidatePassedTestsCount(7); + // 4 tests of BaseClass.DataRowTestMethod - 3 datarow result and 1 parent result + // 5 tests of BaseClass.DataRowTestMethodWithSomeOptionalParameters - 4 datarow result and 1 parent result + // 6 tests of BaseClass.DataRowTestMethodWithAllOptionalParameters - 5 datarow result and 1 parent result + // 3 tests of DerivedClass.DataRowTestMethod - 2 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result + // Total 26 tests - Making sure that DerivedClass doesn't run BaseClass tests + this.ValidatePassedTestsCount(26); } [TestMethod] @@ -36,10 +55,18 @@ public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows() this.ValidatePassedTestsContain( "DataRowTestMethod (DerivedString1)", - "DataRowTestMethod (DerivedString2)"); + "DataRowTestMethod (DerivedString2)", + "DataRowTestMethodWithSomeOptionalParameters (123)", + "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString1)", + "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString2,DerivedOptionalString3)", + "DataRowTestMethodWithAllOptionalParameters (123)", + "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString4)", + "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString5,DerivedOptionalString6)"); - // 3 tests of DerivedClass - 2 datarow result and 1 parent result - this.ValidatePassedTestsCount(3); + // 3 tests of DerivedClass.DataRowTestMethod - 2 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result + this.ValidatePassedTestsCount(11); } } } diff --git a/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs b/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs index 293e2a19c3..a5132f0c6c 100644 --- a/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs +++ b/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs @@ -19,8 +19,9 @@ public virtual void DataRowTestMethod(string a) [TestMethod] [DataRow(42)] - [DataRow(42,"OptionalString1")] - [DataRow(42,"OptionalString1","OptionalString2")] + [DataRow(42, "BaseOptionalString1")] + [DataRow(42, "BaseOptionalString2", "BaseOptionalString3")] + [DataRow(42, "BaseOptionalString4", "BaseOptionalString5")] public virtual void DataRowTestMethodWithSomeOptionalParameters(int i, string s1 = null, string s2 = null) { Assert.IsTrue(true); @@ -29,8 +30,9 @@ public virtual void DataRowTestMethodWithSomeOptionalParameters(int i, string s1 [TestMethod] [DataRow()] [DataRow(42)] - [DataRow(42, "OptionalString1")] - [DataRow(42, "OptionalString1", "OptionalString2")] + [DataRow(42, "BaseOptionalString6")] + [DataRow(42, "BaseOptionalString7", "BaseOptionalString8")] + [DataRow(42, "BaseOptionalString9", "BaseOptionalString10")] public virtual void DataRowTestMethodWithAllOptionalParameters(int i = 0, string s1 = null, string s2 = null) { Assert.IsTrue(true); diff --git a/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs b/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs index 0ecc6ecf05..a01818c288 100644 --- a/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs +++ b/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs @@ -15,5 +15,23 @@ public override void DataRowTestMethod(string a) { Assert.IsTrue(true); } + + [TestMethod] + [DataRow(123)] + [DataRow(123, "DerivedOptionalString1")] + [DataRow(123, "DerivedOptionalString2", "DerivedOptionalString3")] + public override void DataRowTestMethodWithSomeOptionalParameters(int i, string s1 = null, string s2 = null) + { + Assert.IsTrue(true); + } + + [TestMethod] + [DataRow(123)] + [DataRow(123, "DerivedOptionalString4")] + [DataRow(123, "DerivedOptionalString5", "DerivedOptionalString6")] + public override void DataRowTestMethodWithAllOptionalParameters(int i = 0, string s1 = null, string s2 = null) + { + Assert.IsTrue(true); + } } } diff --git a/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DataRowAttributeTests.cs b/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DataRowAttributeTests.cs index 095c0c91a0..93b8f5076f 100644 --- a/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DataRowAttributeTests.cs +++ b/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DataRowAttributeTests.cs @@ -20,6 +20,14 @@ public class DataRowAttributeTests private DummyTestClass dummyTestClass; private MethodInfo testMethodInfo; + [TestMethod] + public void DefaultConstructorSetsEmptyArrayPassed() + { + var dataRow = new DataRowAttribute(); + + CollectionAssert.AreEqual(new object[] { }, dataRow.Data); + } + [TestMethod] public void ConstructorShouldSetDataPassed() { From 31ce0e497db4b5d9c649f60fd32f16aab26f584b Mon Sep 17 00:00:00 2001 From: Steve Ashman Date: Sat, 27 Apr 2019 08:25:28 -0400 Subject: [PATCH 3/7] add some TestMethodInfo tests Signed-off-by: Steve Ashman --- .../Execution/TestMethodInfoTests.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestMethodInfoTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestMethodInfoTests.cs index 2d6408bc16..123807f940 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestMethodInfoTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestMethodInfoTests.cs @@ -1301,6 +1301,42 @@ public void TestMethodInfoInvokeShouldFailOnTokenSourceCancellation() #endregion + [TestMethodV1] + public void ResolveArgumentsShouldReturnAdditionalOptionalParametersWithNoneProvided() + { + var optionalArgumentsMethod = typeof(DummyTestClass).GetMethod("DummyOptionalArgumentsMethod"); + + var method = new TestMethodInfo( + optionalArgumentsMethod, + this.testClassInfo, + this.testMethodOptions); + + object[] arguments = new object[] { "RequiredStr1" }; + object[] expectedArguments = new object[] { "RequiredStr1", null, null }; + var resolvedArguments = method.ResolveArguments(arguments); + + Assert.AreEqual(3, resolvedArguments.Length); + CollectionAssert.AreEqual(expectedArguments, resolvedArguments); + } + + [TestMethodV1] + public void ResolveArgumentsShouldReturnAdditionalOptionalParametersWithSomeProvided() + { + var optionalArgumentsMethod = typeof(DummyTestClass).GetMethod("DummyOptionalArgumentsMethod"); + + var method = new TestMethodInfo( + optionalArgumentsMethod, + this.testClassInfo, + this.testMethodOptions); + + object[] arguments = new object[] { "RequiredStr1", "OptionalStr1" }; + object[] expectedArguments = new object[] { "RequiredStr1", "OptionalStr1", null }; + var resolvedArguments = method.ResolveArguments(arguments); + + Assert.AreEqual(3, resolvedArguments.Length); + CollectionAssert.AreEqual(expectedArguments, resolvedArguments); + } + #region helper methods private void RunWithTestablePlatformService(TestablePlatformServiceProvider testablePlatformServiceProvider, Action action) @@ -1399,6 +1435,11 @@ public Task DummyAsyncTestMethod() // We use this method to validate async TestInitialize, TestCleanup, TestMethod return DummyAsyncTestMethodBody(); } + + public void DummyOptionalArgumentsMethod(string str1, string str2 = null, string str3 = null) + { + TestMethodBody(this); + } } public class DummyTestClassWithParameterizedCtor From 8636d82be9eb0b73fe3561d310fc7837ec957cc2 Mon Sep 17 00:00:00 2001 From: Steve Ashman Date: Sat, 27 Apr 2019 12:24:54 -0400 Subject: [PATCH 4/7] split E2E datarow tests into multiple; add explanatory comments; add unit tests for TestMethodInfo.SetParameters; add support for methods with params arguments --- .../Execution/TestMethodInfo.cs | 74 +++++++-- test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs | 151 +++++++++++++++--- .../DataRowTestProject/BaseClass.cs | 25 +++ .../DataRowTestProject/DerivedClass.cs | 23 +++ .../Execution/TestMethodInfoTests.cs | 86 ++++++++++ 5 files changed, 323 insertions(+), 36 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs b/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs index 8d930103fd..2cc5bdeb5b 100644 --- a/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs +++ b/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs @@ -171,32 +171,46 @@ public virtual TestResult Invoke(object[] arguments) internal void SetArguments(object[] arguments) { - this.arguments = this.ResolveArguments(arguments); + if (arguments == null) + { + this.arguments = null; + } + else + { + this.arguments = this.ResolveArguments(arguments); + } } internal object[] ResolveArguments(object[] arguments) { ParameterInfo[] parameterInfos = this.TestMethod.GetParameters(); int requiredParameterCount = 0; + bool hasParamsValue = false; + object paramsValues = null; foreach (var parameter in parameterInfos) { + // If this is a params array parameter, create an instance to + // populate with any extra values provided. Don't increment + // required parameter count - params arguments are not actually required + if (parameter.GetCustomAttribute(typeof(ParamArrayAttribute)) != null) + { + hasParamsValue = true; + break; + } + + // Count required parameters from method if (!parameter.IsOptional) { requiredParameterCount++; } } - if (requiredParameterCount == parameterInfos.Length) - { - return arguments; - } - - if (arguments == null) - { - return null; - } - - if (arguments.Length < requiredParameterCount || arguments.Length > parameterInfos.Length) + // If all the parameters are required, we have fewer arguments + // supplied than required, or more arguments than the method takes + // and it doesn't have a params paramenter don't try and resolve anything + if (requiredParameterCount == parameterInfos.Length || + arguments.Length < requiredParameterCount || + (!hasParamsValue && arguments.Length > parameterInfos.Length)) { return arguments; } @@ -204,12 +218,44 @@ internal object[] ResolveArguments(object[] arguments) object[] newParameters = new object[parameterInfos.Length]; for (int i = 0; i < arguments.Length; i++) { - newParameters[i] = arguments[i]; + // We have reached the end of the regular parameters and any additional + // values will go in a params array + if (i >= parameterInfos.Length - 1 && hasParamsValue) + { + // If this is the params parameter, instantiate a new object of that type + if (i == parameterInfos.Length - 1) + { + paramsValues = Activator.CreateInstance(parameterInfos[i].ParameterType, new object[] { arguments.Length - i }); + newParameters[i] = paramsValues; + } + + // The params parameters is an array but the type is not known + // set the values as a generic array + if (paramsValues is Array paramsArray) + { + paramsArray.SetValue(arguments[i], i - (parameterInfos.Length - 1)); + } + } + else + { + newParameters[i] = arguments[i]; + } } + // If arguments supplied are less than total possible arguments set + // the values supplied to the default values for those parameters for (int i = arguments.Length; i < parameterInfos.Length; i++) { - newParameters[i] = parameterInfos[i].DefaultValue; + // If this is the params parameters, set it to an empty + // array of that type as DefaultValue is DBNull + if (hasParamsValue && i == parameterInfos.Length - 1) + { + newParameters[i] = Activator.CreateInstance(parameterInfos[i].ParameterType, 0); + } + else + { + newParameters[i] = parameterInfos[i].DefaultValue; + } } return newParameters; diff --git a/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs b/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs index df3e772669..5123c7ec53 100644 --- a/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs +++ b/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs @@ -12,23 +12,76 @@ public class DataRowTests : CLITestBase private const string TestAssembly = "DataRowTestProject.dll"; [TestMethod] - public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRows() + public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRows_SimpleDataRows() { - this.InvokeVsTestForExecution(new string[] { TestAssembly }); + this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowSimple"); this.ValidatePassedTestsContain( "DataRowTestMethod (BaseString1)", "DataRowTestMethod (BaseString2)", "DataRowTestMethod (BaseString3)", "DataRowTestMethod (DerivedString1)", - "DataRowTestMethod (DerivedString2)", + "DataRowTestMethod (DerivedString2)"); + + // 4 tests of BaseClass.DataRowTestMethod - 3 datarow result and 1 parent result + // 3 tests of DerivedClass.DataRowTestMethod - 2 datarow result and 1 parent result + // Total 7 tests - Making sure that DerivedClass doesn't run BaseClass tests + this.ValidatePassedTestsCount(7); + } + + [TestMethod] + public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_SimpleDataRows() + { + this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "FullyQualifiedName~DerivedClass&TestCategory~DataRowSimple"); + + this.ValidatePassedTestsContain( + "DataRowTestMethod (DerivedString1)", + "DataRowTestMethod (DerivedString2)"); + + // 3 tests of DerivedClass.DataRowTestMethod - 2 datarow result and 1 parent result + this.ValidatePassedTestsCount(3); + } + + [TestMethod] + public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRows_DataRowSomeOptional() + { + this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowSomeOptional"); + + this.ValidatePassedTestsContain( "DataRowTestMethodWithSomeOptionalParameters (42)", "DataRowTestMethodWithSomeOptionalParameters (42,BaseOptionalString1)", "DataRowTestMethodWithSomeOptionalParameters (42,BaseOptionalString2,BaseOptionalString3)", "DataRowTestMethodWithSomeOptionalParameters (42,BaseOptionalString4,BaseOptionalString5)", "DataRowTestMethodWithSomeOptionalParameters (123)", "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString1)", - "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString2,DerivedOptionalString3)", + "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString2,DerivedOptionalString3)"); + + // 5 tests of BaseClass.DataRowTestMethod - 4 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + // Total 9 tests - Making sure that DerivedClass doesn't run BaseClass tests + this.ValidatePassedTestsCount(9); + } + + [TestMethod] + public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_DataRowSomeOptional() + { + this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "FullyQualifiedName~DerivedClass&TestCategory~DataRowSomeOptional"); + + this.ValidatePassedTestsContain( + "DataRowTestMethodWithSomeOptionalParameters (123)", + "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString1)", + "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString2,DerivedOptionalString3)"); + + // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + this.ValidatePassedTestsCount(4); + } + + [TestMethod] + public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRows_DataRowAllOptional() + { + this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowAllOptional"); + + this.ValidatePassedTestsContain( "DataRowTestMethodWithAllOptionalParameters ()", "DataRowTestMethodWithAllOptionalParameters (42)", "DataRowTestMethodWithAllOptionalParameters (42,BaseOptionalString6)", @@ -38,35 +91,89 @@ public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRow "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString4)", "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString5,DerivedOptionalString6)"); - // 4 tests of BaseClass.DataRowTestMethod - 3 datarow result and 1 parent result - // 5 tests of BaseClass.DataRowTestMethodWithSomeOptionalParameters - 4 datarow result and 1 parent result - // 6 tests of BaseClass.DataRowTestMethodWithAllOptionalParameters - 5 datarow result and 1 parent result - // 3 tests of DerivedClass.DataRowTestMethod - 2 datarow result and 1 parent result - // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result - // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result - // Total 26 tests - Making sure that DerivedClass doesn't run BaseClass tests - this.ValidatePassedTestsCount(26); + // 6 tests of BaseClass.DataRowTestMethod - 4 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + // Total 10 tests - Making sure that DerivedClass doesn't run BaseClass tests + this.ValidatePassedTestsCount(10); } [TestMethod] - public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows() + public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_DataRowAllOptional() { - this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "FullyQualifiedName~DerivedClass"); + this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "FullyQualifiedName~DerivedClass&TestCategory~DataRowAllOptional"); this.ValidatePassedTestsContain( - "DataRowTestMethod (DerivedString1)", - "DataRowTestMethod (DerivedString2)", - "DataRowTestMethodWithSomeOptionalParameters (123)", - "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString1)", - "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString2,DerivedOptionalString3)", "DataRowTestMethodWithAllOptionalParameters (123)", "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString4)", "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString5,DerivedOptionalString6)"); + // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + this.ValidatePassedTestsCount(4); + } + + [TestMethod] + public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRows_DataRowParamsArgument() + { + this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowParamsArgument"); + + this.ValidatePassedTestsContain( + "DataRowTestMethodWithParamsParameters (1)", + "DataRowTestMethodWithParamsParameters (1,BaseSingleParamsArg)", + "DataRowTestMethodWithParamsParameters (1,BaseParamsArg1,BaseParamsArg2)", + "DataRowTestMethodWithParamsParameters (1,BaseParamsArg1,BaseParamsArg2,BaseParamsArg3)", + "DataRowTestMethodWithParamsParameters (2)", + "DataRowTestMethodWithParamsParameters (2,DerivedSingleParamsArg)", + "DataRowTestMethodWithParamsParameters (2,DerivedParamsArg1,DerivedParamsArg2)"); + + // 5 tests of BaseClass.DataRowTestMethod - 4 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + // Total 9 tests - Making sure that DerivedClass doesn't run BaseClass tests + this.ValidatePassedTestsCount(9); + } + + [TestMethod] + public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_DataRowParamsArgument() + { + this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "FullyQualifiedName~DerivedClass&TestCategory~DataRowParamsArgument"); + + this.ValidatePassedTestsContain( + "DataRowTestMethodWithParamsParameters (2)", + "DataRowTestMethodWithParamsParameters (2,DerivedSingleParamsArg)", + "DataRowTestMethodWithParamsParameters (2,DerivedParamsArg1,DerivedParamsArg2)"); + + // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + this.ValidatePassedTestsCount(4); + } + + [TestMethod] + public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRows_DataRowOptionalInvalidArguments() + { + this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowOptionalInvalidArguments"); + + this.ValidatePassedTestsContain( + "DataRowTestMethodFailsWithInvalidArguments ()", + "DataRowTestMethodFailsWithInvalidArguments (1)", + "DataRowTestMethodFailsWithInvalidArguments (1,BaseRequiredArgument,BaseOptionalArgument,BaseExtraArgument)", + "DataRowTestMethodFailsWithInvalidArguments (2)", + "DataRowTestMethodFailsWithInvalidArguments (2,DerivedRequiredArgument,DerivedOptionalArgument,DerivedExtraArgument)"); + + // 4 tests of BaseClass.DataRowTestMethod - 3 datarow result and 1 parent result + // 3 tests of DerivedClass.DataRowTestMethod - 2 datarow result and 1 parent result + // Total 7 tests - Making sure that DerivedClass doesn't run BaseClass tests + this.ValidatePassedTestsCount(7); + } + + [TestMethod] + public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_DataRowOptionalInvalidArguments() + { + this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "FullyQualifiedName~DerivedClass&TestCategory~DataRowOptionalInvalidArguments"); + + this.ValidatePassedTestsContain( + "DataRowTestMethodFailsWithInvalidArguments (2)", + "DataRowTestMethodFailsWithInvalidArguments (2,DerivedRequiredArgument,DerivedOptionalArgument,DerivedExtraArgument)"); + // 3 tests of DerivedClass.DataRowTestMethod - 2 datarow result and 1 parent result - // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result - // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result - this.ValidatePassedTestsCount(11); + this.ValidatePassedTestsCount(3); } } } diff --git a/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs b/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs index a5132f0c6c..cbc88f637d 100644 --- a/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs +++ b/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs @@ -8,6 +8,7 @@ namespace DataRowTestProject [TestClass] public class BaseClass { + [TestCategory("DataRowSimple")] [TestMethod] [DataRow("BaseString1")] [DataRow("BaseString2")] @@ -17,6 +18,7 @@ public virtual void DataRowTestMethod(string a) Assert.IsTrue(true); } + [TestCategory("DataRowSomeOptional")] [TestMethod] [DataRow(42)] [DataRow(42, "BaseOptionalString1")] @@ -27,6 +29,7 @@ public virtual void DataRowTestMethodWithSomeOptionalParameters(int i, string s1 Assert.IsTrue(true); } + [TestCategory("DataRowAllOptional")] [TestMethod] [DataRow()] [DataRow(42)] @@ -37,5 +40,27 @@ public virtual void DataRowTestMethodWithAllOptionalParameters(int i = 0, string { Assert.IsTrue(true); } + + [TestCategory("DataRowParamsArgument")] + [TestMethod] + [DataRow(1)] + [DataRow(1, "BaseSingleParamsArg")] + [DataRow(1, "BaseParamsArg1","BaseParamsArg2")] + [DataRow(1, "BaseParamsArg1", "BaseParamsArg2", "BaseParamsArg3")] + public virtual void DataRowTestMethodWithParamsParameters(int i, params string[] args) + { + Assert.IsTrue(true); + } + + [TestCategory("DataRowOptionalInvalidArguments")] + [TestMethod] + [ExpectedException(typeof(System.Reflection.TargetParameterCountException))] + [DataRow()] + [DataRow(1)] + [DataRow(1, "BaseRequiredArgument", "BaseOptionalArgument", "BaseExtraArgument")] + public virtual void DataRowTestMethodFailsWithInvalidArguments(int i1, string required, string s1 = null) + { + Assert.Fail(); + } } } diff --git a/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs b/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs index a01818c288..fee632e865 100644 --- a/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs +++ b/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs @@ -8,6 +8,7 @@ namespace DataRowTestProject [TestClass] public class DerivedClass : BaseClass { + [TestCategory("DataRowSimple")] [TestMethod] [DataRow("DerivedString1")] [DataRow("DerivedString2")] @@ -16,6 +17,7 @@ public override void DataRowTestMethod(string a) Assert.IsTrue(true); } + [TestCategory("DataRowSomeOptional")] [TestMethod] [DataRow(123)] [DataRow(123, "DerivedOptionalString1")] @@ -25,6 +27,7 @@ public override void DataRowTestMethodWithSomeOptionalParameters(int i, string s Assert.IsTrue(true); } + [TestCategory("DataRowAllOptional")] [TestMethod] [DataRow(123)] [DataRow(123, "DerivedOptionalString4")] @@ -33,5 +36,25 @@ public override void DataRowTestMethodWithAllOptionalParameters(int i = 0, strin { Assert.IsTrue(true); } + + [TestCategory("DataRowParamsArgument")] + [TestMethod] + [DataRow(2)] + [DataRow(2, "DerivedSingleParamsArg")] + [DataRow(2, "DerivedParamsArg1", "DerivedParamsArg2")] + public override void DataRowTestMethodWithParamsParameters(int i, params string[] args) + { + Assert.IsTrue(true); + } + + [TestCategory("DataRowOptionalInvalidArguments")] + [TestMethod] + [ExpectedException(typeof(System.Reflection.TargetParameterCountException))] + [DataRow(2)] + [DataRow(2, "DerivedRequiredArgument", "DerivedOptionalArgument", "DerivedExtraArgument")] + public override void DataRowTestMethodFailsWithInvalidArguments(int i1, string requiredString, string s1 = null) + { + Assert.Fail(); + } } } diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestMethodInfoTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestMethodInfoTests.cs index 123807f940..6ef9d380cf 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestMethodInfoTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestMethodInfoTests.cs @@ -1301,6 +1301,42 @@ public void TestMethodInfoInvokeShouldFailOnTokenSourceCancellation() #endregion + [TestMethodV1] + public void ResolveArgumentsShouldReturnProvidedArgumentsWhenTooFewParameters() + { + var simpleArgumentsMethod = typeof(DummyTestClass).GetMethod("DummySimpleArgumentsMethod"); + + var method = new TestMethodInfo( + simpleArgumentsMethod, + this.testClassInfo, + this.testMethodOptions); + + object[] arguments = new object[] { "RequiredStr1" }; + object[] expectedArguments = new object[] { "RequiredStr1" }; + var resolvedArguments = method.ResolveArguments(arguments); + + Assert.AreEqual(1, resolvedArguments.Length); + CollectionAssert.AreEqual(expectedArguments, resolvedArguments); + } + + [TestMethodV1] + public void ResolveArgumentsShouldReturnProvidedArgumentsWhenTooManyParameters() + { + var simpleArgumentsMethod = typeof(DummyTestClass).GetMethod("DummySimpleArgumentsMethod"); + + var method = new TestMethodInfo( + simpleArgumentsMethod, + this.testClassInfo, + this.testMethodOptions); + + object[] arguments = new object[] { "RequiredStr1", "RequiredStr2", "ExtraStr3" }; + object[] expectedArguments = new object[] { "RequiredStr1", "RequiredStr2", "ExtraStr3" }; + var resolvedArguments = method.ResolveArguments(arguments); + + Assert.AreEqual(3, resolvedArguments.Length); + CollectionAssert.AreEqual(expectedArguments, resolvedArguments); + } + [TestMethodV1] public void ResolveArgumentsShouldReturnAdditionalOptionalParametersWithNoneProvided() { @@ -1337,6 +1373,46 @@ public void ResolveArgumentsShouldReturnAdditionalOptionalParametersWithSomeProv CollectionAssert.AreEqual(expectedArguments, resolvedArguments); } + [TestMethodV1] + public void ResolveArgumentsShouldReturnEmptyParamsWithNoneProvided() + { + var paramsArgumentMethod = typeof(DummyTestClass).GetMethod("DummyParamsArgumentMethod"); + + var method = new TestMethodInfo( + paramsArgumentMethod, + this.testClassInfo, + this.testMethodOptions); + + object[] arguments = new object[] { 1 }; + object[] expectedArguments = new object[] { 1, new string[] { } }; + var resolvedArguments = method.ResolveArguments(arguments); + + Assert.AreEqual(2, resolvedArguments.Length); + Assert.AreEqual(expectedArguments[0], resolvedArguments[0]); + Assert.IsInstanceOfType(resolvedArguments[1], typeof(string[])); + CollectionAssert.AreEqual((string[])expectedArguments[1], (string[])resolvedArguments[1]); + } + + [TestMethodV1] + public void ResolveArgumentsShouldReturnPopulatedParamsWithAllProvided() + { + var paramsArgumentMethod = typeof(DummyTestClass).GetMethod("DummyParamsArgumentMethod"); + + var method = new TestMethodInfo( + paramsArgumentMethod, + this.testClassInfo, + this.testMethodOptions); + + object[] arguments = new object[] { 1, "str1", "str2", "str3" }; + object[] expectedArguments = new object[] { 1, new string[] { "str1", "str2", "str3" } }; + var resolvedArguments = method.ResolveArguments(arguments); + + Assert.AreEqual(2, resolvedArguments.Length); + Assert.AreEqual(expectedArguments[0], resolvedArguments[0]); + Assert.IsInstanceOfType(resolvedArguments[1], typeof(string[])); + CollectionAssert.AreEqual((string[])expectedArguments[1], (string[])resolvedArguments[1]); + } + #region helper methods private void RunWithTestablePlatformService(TestablePlatformServiceProvider testablePlatformServiceProvider, Action action) @@ -1436,10 +1512,20 @@ public Task DummyAsyncTestMethod() return DummyAsyncTestMethodBody(); } + public void DummySimpleArgumentsMethod(string str1, string str2) + { + TestMethodBody(this); + } + public void DummyOptionalArgumentsMethod(string str1, string str2 = null, string str3 = null) { TestMethodBody(this); } + + public void DummyParamsArgumentMethod(int i, params string[] args) + { + TestMethodBody(this); + } } public class DummyTestClassWithParameterizedCtor From b77f6243d29720ba570e4b70b38ab314d4d97e05 Mon Sep 17 00:00:00 2001 From: Steve Ashman Date: Sat, 27 Apr 2019 12:35:12 -0400 Subject: [PATCH 5/7] update datarowtests comments to reflect correct method names and counts being tested --- test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs b/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs index 5123c7ec53..88238f9c0e 100644 --- a/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs +++ b/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs @@ -56,8 +56,8 @@ public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRow "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString1)", "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString2,DerivedOptionalString3)"); - // 5 tests of BaseClass.DataRowTestMethod - 4 datarow result and 1 parent result - // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + // 5 tests of BaseClass.DataRowTestMethodWithSomeOptionalParameters - 4 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethodWithSomeOptionalParameters - 3 datarow result and 1 parent result // Total 9 tests - Making sure that DerivedClass doesn't run BaseClass tests this.ValidatePassedTestsCount(9); } @@ -72,7 +72,7 @@ public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_Data "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString1)", "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString2,DerivedOptionalString3)"); - // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethodWithSomeOptionalParameters - 3 datarow result and 1 parent result this.ValidatePassedTestsCount(4); } @@ -91,8 +91,8 @@ public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRow "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString4)", "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString5,DerivedOptionalString6)"); - // 6 tests of BaseClass.DataRowTestMethod - 4 datarow result and 1 parent result - // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + // 6 tests of BaseClass.DataRowTestMethodWithAllOptionalParameters - 5 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result // Total 10 tests - Making sure that DerivedClass doesn't run BaseClass tests this.ValidatePassedTestsCount(10); } @@ -107,7 +107,7 @@ public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_Data "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString4)", "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString5,DerivedOptionalString6)"); - // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result this.ValidatePassedTestsCount(4); } @@ -125,8 +125,8 @@ public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRow "DataRowTestMethodWithParamsParameters (2,DerivedSingleParamsArg)", "DataRowTestMethodWithParamsParameters (2,DerivedParamsArg1,DerivedParamsArg2)"); - // 5 tests of BaseClass.DataRowTestMethod - 4 datarow result and 1 parent result - // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + // 5 tests of BaseClass.DataRowTestMethodWithParamsParameters - 4 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethodWithParamsParameters - 3 datarow result and 1 parent result // Total 9 tests - Making sure that DerivedClass doesn't run BaseClass tests this.ValidatePassedTestsCount(9); } @@ -141,7 +141,7 @@ public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_Data "DataRowTestMethodWithParamsParameters (2,DerivedSingleParamsArg)", "DataRowTestMethodWithParamsParameters (2,DerivedParamsArg1,DerivedParamsArg2)"); - // 4 tests of DerivedClass.DataRowTestMethod - 3 datarow result and 1 parent result + // 4 tests of DerivedClass.DataRowTestMethodWithParamsParameters - 3 datarow result and 1 parent result this.ValidatePassedTestsCount(4); } @@ -157,8 +157,8 @@ public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRow "DataRowTestMethodFailsWithInvalidArguments (2)", "DataRowTestMethodFailsWithInvalidArguments (2,DerivedRequiredArgument,DerivedOptionalArgument,DerivedExtraArgument)"); - // 4 tests of BaseClass.DataRowTestMethod - 3 datarow result and 1 parent result - // 3 tests of DerivedClass.DataRowTestMethod - 2 datarow result and 1 parent result + // 4 tests of BaseClass.DataRowTestMethodFailsWithInvalidArguments - 3 datarow result and 1 parent result + // 3 tests of DerivedClass.DataRowTestMethodFailsWithInvalidArguments - 2 datarow result and 1 parent result // Total 7 tests - Making sure that DerivedClass doesn't run BaseClass tests this.ValidatePassedTestsCount(7); } @@ -172,7 +172,7 @@ public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_Data "DataRowTestMethodFailsWithInvalidArguments (2)", "DataRowTestMethodFailsWithInvalidArguments (2,DerivedRequiredArgument,DerivedOptionalArgument,DerivedExtraArgument)"); - // 3 tests of DerivedClass.DataRowTestMethod - 2 datarow result and 1 parent result + // 3 tests of DerivedClass.DataRowTestMethodFailsWithInvalidArguments - 2 datarow result and 1 parent result this.ValidatePassedTestsCount(3); } } From 30b76e08df4679a8fcda4ba7bf90ba247aacf5f9 Mon Sep 17 00:00:00 2001 From: Steve Ashman Date: Tue, 30 Apr 2019 21:36:01 -0400 Subject: [PATCH 6/7] rename iterator variables to be more descriptive; remove duplicate derived class tests and rename tests to be more descriptive --- .../Execution/TestMethodInfo.cs | 22 +++---- test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs | 63 ++----------------- 2 files changed, 15 insertions(+), 70 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs b/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs index 2cc5bdeb5b..937f0ecd55 100644 --- a/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs +++ b/src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs @@ -216,45 +216,45 @@ internal object[] ResolveArguments(object[] arguments) } object[] newParameters = new object[parameterInfos.Length]; - for (int i = 0; i < arguments.Length; i++) + for (int argumentIndex = 0; argumentIndex < arguments.Length; argumentIndex++) { // We have reached the end of the regular parameters and any additional // values will go in a params array - if (i >= parameterInfos.Length - 1 && hasParamsValue) + if (argumentIndex >= parameterInfos.Length - 1 && hasParamsValue) { // If this is the params parameter, instantiate a new object of that type - if (i == parameterInfos.Length - 1) + if (argumentIndex == parameterInfos.Length - 1) { - paramsValues = Activator.CreateInstance(parameterInfos[i].ParameterType, new object[] { arguments.Length - i }); - newParameters[i] = paramsValues; + paramsValues = Activator.CreateInstance(parameterInfos[argumentIndex].ParameterType, new object[] { arguments.Length - argumentIndex }); + newParameters[argumentIndex] = paramsValues; } // The params parameters is an array but the type is not known // set the values as a generic array if (paramsValues is Array paramsArray) { - paramsArray.SetValue(arguments[i], i - (parameterInfos.Length - 1)); + paramsArray.SetValue(arguments[argumentIndex], argumentIndex - (parameterInfos.Length - 1)); } } else { - newParameters[i] = arguments[i]; + newParameters[argumentIndex] = arguments[argumentIndex]; } } // If arguments supplied are less than total possible arguments set // the values supplied to the default values for those parameters - for (int i = arguments.Length; i < parameterInfos.Length; i++) + for (int parameterNotProvidedIndex = arguments.Length; parameterNotProvidedIndex < parameterInfos.Length; parameterNotProvidedIndex++) { // If this is the params parameters, set it to an empty // array of that type as DefaultValue is DBNull - if (hasParamsValue && i == parameterInfos.Length - 1) + if (hasParamsValue && parameterNotProvidedIndex == parameterInfos.Length - 1) { - newParameters[i] = Activator.CreateInstance(parameterInfos[i].ParameterType, 0); + newParameters[parameterNotProvidedIndex] = Activator.CreateInstance(parameterInfos[parameterNotProvidedIndex].ParameterType, 0); } else { - newParameters[i] = parameterInfos[i].DefaultValue; + newParameters[parameterNotProvidedIndex] = parameterInfos[parameterNotProvidedIndex].DefaultValue; } } diff --git a/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs b/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs index 88238f9c0e..35137e895e 100644 --- a/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs +++ b/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs @@ -43,7 +43,7 @@ public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_Simp } [TestMethod] - public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRows_DataRowSomeOptional() + public void DataRowsExecuteWithRequiredAndOptionalParameters() { this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowSomeOptional"); @@ -63,21 +63,7 @@ public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRow } [TestMethod] - public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_DataRowSomeOptional() - { - this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "FullyQualifiedName~DerivedClass&TestCategory~DataRowSomeOptional"); - - this.ValidatePassedTestsContain( - "DataRowTestMethodWithSomeOptionalParameters (123)", - "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString1)", - "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString2,DerivedOptionalString3)"); - - // 4 tests of DerivedClass.DataRowTestMethodWithSomeOptionalParameters - 3 datarow result and 1 parent result - this.ValidatePassedTestsCount(4); - } - - [TestMethod] - public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRows_DataRowAllOptional() + public void DataRowsExecuteWithAllOptionalParameters() { this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowAllOptional"); @@ -98,21 +84,7 @@ public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRow } [TestMethod] - public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_DataRowAllOptional() - { - this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "FullyQualifiedName~DerivedClass&TestCategory~DataRowAllOptional"); - - this.ValidatePassedTestsContain( - "DataRowTestMethodWithAllOptionalParameters (123)", - "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString4)", - "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString5,DerivedOptionalString6)"); - - // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result - this.ValidatePassedTestsCount(4); - } - - [TestMethod] - public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRows_DataRowParamsArgument() + public void DataRowsExecuteWithParamsArrayParameter() { this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowParamsArgument"); @@ -132,21 +104,7 @@ public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRow } [TestMethod] - public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_DataRowParamsArgument() - { - this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "FullyQualifiedName~DerivedClass&TestCategory~DataRowParamsArgument"); - - this.ValidatePassedTestsContain( - "DataRowTestMethodWithParamsParameters (2)", - "DataRowTestMethodWithParamsParameters (2,DerivedSingleParamsArg)", - "DataRowTestMethodWithParamsParameters (2,DerivedParamsArg1,DerivedParamsArg2)"); - - // 4 tests of DerivedClass.DataRowTestMethodWithParamsParameters - 3 datarow result and 1 parent result - this.ValidatePassedTestsCount(4); - } - - [TestMethod] - public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRows_DataRowOptionalInvalidArguments() + public void DataRowsFailWhenInvalidArgumentsProvided() { this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowOptionalInvalidArguments"); @@ -162,18 +120,5 @@ public void ExecuteOnlyDerivedClassDataRowsWhenBothBaseAndDerviedClassHasDataRow // Total 7 tests - Making sure that DerivedClass doesn't run BaseClass tests this.ValidatePassedTestsCount(7); } - - [TestMethod] - public void ExecuteOnlyDerivedClassDataRowsWhenItOverridesBaseClassDataRows_DataRowOptionalInvalidArguments() - { - this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "FullyQualifiedName~DerivedClass&TestCategory~DataRowOptionalInvalidArguments"); - - this.ValidatePassedTestsContain( - "DataRowTestMethodFailsWithInvalidArguments (2)", - "DataRowTestMethodFailsWithInvalidArguments (2,DerivedRequiredArgument,DerivedOptionalArgument,DerivedExtraArgument)"); - - // 3 tests of DerivedClass.DataRowTestMethodFailsWithInvalidArguments - 2 datarow result and 1 parent result - this.ValidatePassedTestsCount(3); - } } } From e992869143c7a8e93879b20e08e4aeaec2dc954e Mon Sep 17 00:00:00 2001 From: Steve Ashman Date: Fri, 3 May 2019 11:24:11 -0400 Subject: [PATCH 7/7] update datarow tests to only exist in DerivedClass so we aren't effectively running the same tests twice --- test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs | 39 ++++------------ .../DataRowTestProject/BaseClass.cs | 45 ------------------- .../DataRowTestProject/DerivedClass.cs | 11 +++-- 3 files changed, 16 insertions(+), 79 deletions(-) diff --git a/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs b/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs index 35137e895e..a1658cca3e 100644 --- a/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs +++ b/test/E2ETests/Smoke.E2E.Tests/DataRowTests.cs @@ -48,18 +48,12 @@ public void DataRowsExecuteWithRequiredAndOptionalParameters() this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowSomeOptional"); this.ValidatePassedTestsContain( - "DataRowTestMethodWithSomeOptionalParameters (42)", - "DataRowTestMethodWithSomeOptionalParameters (42,BaseOptionalString1)", - "DataRowTestMethodWithSomeOptionalParameters (42,BaseOptionalString2,BaseOptionalString3)", - "DataRowTestMethodWithSomeOptionalParameters (42,BaseOptionalString4,BaseOptionalString5)", "DataRowTestMethodWithSomeOptionalParameters (123)", "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString1)", "DataRowTestMethodWithSomeOptionalParameters (123,DerivedOptionalString2,DerivedOptionalString3)"); - // 5 tests of BaseClass.DataRowTestMethodWithSomeOptionalParameters - 4 datarow result and 1 parent result // 4 tests of DerivedClass.DataRowTestMethodWithSomeOptionalParameters - 3 datarow result and 1 parent result - // Total 9 tests - Making sure that DerivedClass doesn't run BaseClass tests - this.ValidatePassedTestsCount(9); + this.ValidatePassedTestsCount(4); } [TestMethod] @@ -69,18 +63,12 @@ public void DataRowsExecuteWithAllOptionalParameters() this.ValidatePassedTestsContain( "DataRowTestMethodWithAllOptionalParameters ()", - "DataRowTestMethodWithAllOptionalParameters (42)", - "DataRowTestMethodWithAllOptionalParameters (42,BaseOptionalString6)", - "DataRowTestMethodWithAllOptionalParameters (42,BaseOptionalString7,BaseOptionalString8)", - "DataRowTestMethodWithAllOptionalParameters (42,BaseOptionalString9,BaseOptionalString10)", "DataRowTestMethodWithAllOptionalParameters (123)", "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString4)", "DataRowTestMethodWithAllOptionalParameters (123,DerivedOptionalString5,DerivedOptionalString6)"); - // 6 tests of BaseClass.DataRowTestMethodWithAllOptionalParameters - 5 datarow result and 1 parent result - // 4 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 3 datarow result and 1 parent result - // Total 10 tests - Making sure that DerivedClass doesn't run BaseClass tests - this.ValidatePassedTestsCount(10); + // 5 tests of DerivedClass.DataRowTestMethodWithAllOptionalParameters - 4 datarow result and 1 parent result + this.ValidatePassedTestsCount(5); } [TestMethod] @@ -89,18 +77,13 @@ public void DataRowsExecuteWithParamsArrayParameter() this.InvokeVsTestForExecution(new string[] { TestAssembly }, testCaseFilter: "TestCategory~DataRowParamsArgument"); this.ValidatePassedTestsContain( - "DataRowTestMethodWithParamsParameters (1)", - "DataRowTestMethodWithParamsParameters (1,BaseSingleParamsArg)", - "DataRowTestMethodWithParamsParameters (1,BaseParamsArg1,BaseParamsArg2)", - "DataRowTestMethodWithParamsParameters (1,BaseParamsArg1,BaseParamsArg2,BaseParamsArg3)", "DataRowTestMethodWithParamsParameters (2)", "DataRowTestMethodWithParamsParameters (2,DerivedSingleParamsArg)", - "DataRowTestMethodWithParamsParameters (2,DerivedParamsArg1,DerivedParamsArg2)"); + "DataRowTestMethodWithParamsParameters (2,DerivedParamsArg1,DerivedParamsArg2)", + "DataRowTestMethodWithParamsParameters (2,DerivedParamsArg1,DerivedParamsArg2,DerivedParamsArg3)"); - // 5 tests of BaseClass.DataRowTestMethodWithParamsParameters - 4 datarow result and 1 parent result - // 4 tests of DerivedClass.DataRowTestMethodWithParamsParameters - 3 datarow result and 1 parent result - // Total 9 tests - Making sure that DerivedClass doesn't run BaseClass tests - this.ValidatePassedTestsCount(9); + // 5 tests of DerivedClass.DataRowTestMethodWithParamsParameters - 4 datarow result and 1 parent result + this.ValidatePassedTestsCount(5); } [TestMethod] @@ -110,15 +93,11 @@ public void DataRowsFailWhenInvalidArgumentsProvided() this.ValidatePassedTestsContain( "DataRowTestMethodFailsWithInvalidArguments ()", - "DataRowTestMethodFailsWithInvalidArguments (1)", - "DataRowTestMethodFailsWithInvalidArguments (1,BaseRequiredArgument,BaseOptionalArgument,BaseExtraArgument)", "DataRowTestMethodFailsWithInvalidArguments (2)", "DataRowTestMethodFailsWithInvalidArguments (2,DerivedRequiredArgument,DerivedOptionalArgument,DerivedExtraArgument)"); - // 4 tests of BaseClass.DataRowTestMethodFailsWithInvalidArguments - 3 datarow result and 1 parent result - // 3 tests of DerivedClass.DataRowTestMethodFailsWithInvalidArguments - 2 datarow result and 1 parent result - // Total 7 tests - Making sure that DerivedClass doesn't run BaseClass tests - this.ValidatePassedTestsCount(7); + // 4 tests of DerivedClass.DataRowTestMethodFailsWithInvalidArguments - 3 datarow result and 1 parent result + this.ValidatePassedTestsCount(4); } } } diff --git a/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs b/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs index cbc88f637d..688a365e0d 100644 --- a/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs +++ b/test/E2ETests/TestAssets/DataRowTestProject/BaseClass.cs @@ -17,50 +17,5 @@ public virtual void DataRowTestMethod(string a) { Assert.IsTrue(true); } - - [TestCategory("DataRowSomeOptional")] - [TestMethod] - [DataRow(42)] - [DataRow(42, "BaseOptionalString1")] - [DataRow(42, "BaseOptionalString2", "BaseOptionalString3")] - [DataRow(42, "BaseOptionalString4", "BaseOptionalString5")] - public virtual void DataRowTestMethodWithSomeOptionalParameters(int i, string s1 = null, string s2 = null) - { - Assert.IsTrue(true); - } - - [TestCategory("DataRowAllOptional")] - [TestMethod] - [DataRow()] - [DataRow(42)] - [DataRow(42, "BaseOptionalString6")] - [DataRow(42, "BaseOptionalString7", "BaseOptionalString8")] - [DataRow(42, "BaseOptionalString9", "BaseOptionalString10")] - public virtual void DataRowTestMethodWithAllOptionalParameters(int i = 0, string s1 = null, string s2 = null) - { - Assert.IsTrue(true); - } - - [TestCategory("DataRowParamsArgument")] - [TestMethod] - [DataRow(1)] - [DataRow(1, "BaseSingleParamsArg")] - [DataRow(1, "BaseParamsArg1","BaseParamsArg2")] - [DataRow(1, "BaseParamsArg1", "BaseParamsArg2", "BaseParamsArg3")] - public virtual void DataRowTestMethodWithParamsParameters(int i, params string[] args) - { - Assert.IsTrue(true); - } - - [TestCategory("DataRowOptionalInvalidArguments")] - [TestMethod] - [ExpectedException(typeof(System.Reflection.TargetParameterCountException))] - [DataRow()] - [DataRow(1)] - [DataRow(1, "BaseRequiredArgument", "BaseOptionalArgument", "BaseExtraArgument")] - public virtual void DataRowTestMethodFailsWithInvalidArguments(int i1, string required, string s1 = null) - { - Assert.Fail(); - } } } diff --git a/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs b/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs index fee632e865..f7a94f63e5 100644 --- a/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs +++ b/test/E2ETests/TestAssets/DataRowTestProject/DerivedClass.cs @@ -22,17 +22,18 @@ public override void DataRowTestMethod(string a) [DataRow(123)] [DataRow(123, "DerivedOptionalString1")] [DataRow(123, "DerivedOptionalString2", "DerivedOptionalString3")] - public override void DataRowTestMethodWithSomeOptionalParameters(int i, string s1 = null, string s2 = null) + public void DataRowTestMethodWithSomeOptionalParameters(int i, string s1 = null, string s2 = null) { Assert.IsTrue(true); } [TestCategory("DataRowAllOptional")] [TestMethod] + [DataRow()] [DataRow(123)] [DataRow(123, "DerivedOptionalString4")] [DataRow(123, "DerivedOptionalString5", "DerivedOptionalString6")] - public override void DataRowTestMethodWithAllOptionalParameters(int i = 0, string s1 = null, string s2 = null) + public void DataRowTestMethodWithAllOptionalParameters(int i = 0, string s1 = null, string s2 = null) { Assert.IsTrue(true); } @@ -42,7 +43,8 @@ public override void DataRowTestMethodWithAllOptionalParameters(int i = 0, strin [DataRow(2)] [DataRow(2, "DerivedSingleParamsArg")] [DataRow(2, "DerivedParamsArg1", "DerivedParamsArg2")] - public override void DataRowTestMethodWithParamsParameters(int i, params string[] args) + [DataRow(2, "DerivedParamsArg1", "DerivedParamsArg2","DerivedParamsArg3")] + public void DataRowTestMethodWithParamsParameters(int i, params string[] args) { Assert.IsTrue(true); } @@ -50,9 +52,10 @@ public override void DataRowTestMethodWithParamsParameters(int i, params string[ [TestCategory("DataRowOptionalInvalidArguments")] [TestMethod] [ExpectedException(typeof(System.Reflection.TargetParameterCountException))] + [DataRow()] [DataRow(2)] [DataRow(2, "DerivedRequiredArgument", "DerivedOptionalArgument", "DerivedExtraArgument")] - public override void DataRowTestMethodFailsWithInvalidArguments(int i1, string requiredString, string s1 = null) + public void DataRowTestMethodFailsWithInvalidArguments(int i1, string requiredString, string s1 = null) { Assert.Fail(); }