Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add support for overloaded parameterized tests #3298

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Adapter/MSTest.TestAdapter/Discovery/TypeEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ internal Collection<UnitTestElement> GetTests(ICollection<string> warnings)

if (_testMethodValidator.IsValidTestMethod(method, _type, warnings))
{
foundDuplicateTests = foundDuplicateTests || !foundTests.Add(method.Name);
// ToString() outputs method name and its signature. This is necessary for overloaded methods to be recognized as distinct tests.
foundDuplicateTests = foundDuplicateTests || !foundTests.Add(method.ToString() ?? method.Name);
UnitTestElement testMethod = GetTestFromMethod(method, isMethodDeclaredInTestTypeAssembly, warnings);

tests.Add(testMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ public void ExecuteDataRowTests_Regular()
"FourteenObjectArrays ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14])",
"FifteenObjectArrays ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15])",
"SixteenObjectArrays ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16])",
"MultipleIntegersWrappedWithParams (1,2,3,4,5)");
"MultipleIntegersWrappedWithParams (1,2,3,4,5)",
"MethodWithOverload (1)",
"MethodWithOverload (2)",
"MethodWithOverload (\"a\")",
"MethodWithOverload (\"b\")");

VerifyE2E.TestsFailed(
testResults,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void ExecuteDynamicDataTests()
System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult> testResults = RunTests(testCases);

// Assert
VerifyE2E.ContainsTestsPassed(
VerifyE2E.TestsPassed(
testResults,
"DynamicDataTest_SourceMethod (John;Doe,LibProjectReferencedByDataSourceTest.User)",
"DynamicDataTest_SourceMethod (Jane;Doe,LibProjectReferencedByDataSourceTest.User)",
Expand Down Expand Up @@ -47,7 +47,11 @@ public void ExecuteDynamicDataTests()
"UserDynamicDataTestMethod DynamicDataTest_SourcePropertyOtherType_CustomDisplayNameOtherType with 2 parameters",
"DynamicDataTestWithTestCategory (John;Doe,LibProjectReferencedByDataSourceTest.User)",
"DynamicDataTestWithTestCategory (Jane;Doe,LibProjectReferencedByDataSourceTest.User)",
"StackOverflowException_Example (DataSourceTestProject.DynamicDataTests+ExampleTestCase)");
"StackOverflowException_Example (DataSourceTestProject.DynamicDataTests+ExampleTestCase)",
"MethodWithOverload (1,1)",
"MethodWithOverload (2,1)",
"MethodWithOverload (1,0)",
"MethodWithOverload (2,2)");

VerifyE2E.FailedTestCount(testResults, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ public void ExecuteRegular_DataRowTests()
"FourteenObjectArrays ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14])",
"FifteenObjectArrays ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15])",
"SixteenObjectArrays ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16])",
"MultipleIntegersWrappedWithParams (1,2,3,4,5)");
"MultipleIntegersWrappedWithParams (1,2,3,4,5)",
"MethodWithOverload (1)",
"MethodWithOverload (2)",
"MethodWithOverload (\"a\")",
"MethodWithOverload (\"b\")");

ValidateFailedTests(
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public void ExecuteDynamicDataTests()
"UserDynamicDataTestMethod DynamicDataTest_SourcePropertyOtherType_CustomDisplayNameOtherType with 2 parameters",
"DynamicDataTestWithTestCategory (John;Doe,LibProjectReferencedByDataSourceTest.User)",
"DynamicDataTestWithTestCategory (Jane;Doe,LibProjectReferencedByDataSourceTest.User)",
"StackOverflowException_Example (DataSourceTestProject.DynamicDataTests+ExampleTestCase)");
"StackOverflowException_Example (DataSourceTestProject.DynamicDataTests+ExampleTestCase)",
"MethodWithOverload (1,1)",
"MethodWithOverload (2,1)",
"MethodWithOverload (1,0)",
"MethodWithOverload (2,2)");

ValidateFailedTestsCount(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,18 @@ public void SixteenObjectArrays(object[] o1, object[] o2, object[] o3, object[]
[TestMethod]
[DataRow(1, 2, 3, 4, 5)]
public void MultipleIntegersWrappedWithParams(params int[] integers) => Assert.AreEqual(5, integers.Length);

[TestMethod]
[DataRow("a")]
[DataRow("b")]
public void MethodWithOverload(string s)
{
}

[TestMethod]
[DataRow(1)]
[DataRow(2)]
public void MethodWithOverload(int i)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public class DynamicDataTests
[DynamicData(nameof(GetExampleTestCases), DynamicDataSourceType.Method)]
public void StackOverflowException_Example(ExampleTestCase exampleTestCase) => Assert.IsNotNull(exampleTestCase.Example);

[DataTestMethod]
[DynamicData(nameof(StringAndInt32), DynamicDataSourceType.Method)]
public void MethodWithOverload(string x, int y)
{
}

[DataTestMethod]
[DynamicData(nameof(Int32AndString), DynamicDataSourceType.Method)]
public void MethodWithOverload(int x, string y)
{
}

private static void ParseAndAssert(string userData, User expectedUser)
{
// Prepare
Expand Down Expand Up @@ -182,4 +194,16 @@ public class ExampleTestCase

public ExampleClass Example { get; set; }
}

private static IEnumerable<object[]> StringAndInt32()
{
yield return new object[] { "1", 1 };
yield return new object[] { "2", 1 };
}

private static IEnumerable<object[]> Int32AndString()
{
yield return new object[] { 1, "0" };
yield return new object[] { 2, "2" };
}
}