Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ internal List<UnitTestElement> GetTests(List<string> warnings)
{
// 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);
UnitTestElement testMethod = GetTestFromMethod(method, warnings);

tests.Add(testMethod);
}
Expand Down Expand Up @@ -117,10 +117,9 @@ internal List<UnitTestElement> GetTests(List<string> warnings)
/// Gets a UnitTestElement from a MethodInfo object filling it up with appropriate values.
/// </summary>
/// <param name="method">The reflected method.</param>
/// <param name="isDeclaredInTestTypeAssembly">True if the reflected method is declared in the same assembly as the current type.</param>
/// <param name="warnings">Contains warnings if any, that need to be passed back to the caller.</param>
/// <returns> Returns a UnitTestElement.</returns>
internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInTestTypeAssembly, ICollection<string> warnings)
internal UnitTestElement GetTestFromMethod(MethodInfo method, ICollection<string> warnings)
{
// null if the current instance represents a generic type parameter.
DebugEx.Assert(_type.AssemblyQualifiedName != null, "AssemblyQualifiedName for method is null.");
Expand All @@ -137,13 +136,6 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT
testMethod.DeclaringClassFullName = method.DeclaringType.FullName;
}

if (!isDeclaredInTestTypeAssembly)
{
testMethod.DeclaringAssemblyName =
PlatformServiceProvider.Instance.FileOperations.GetAssemblyPath(
method.DeclaringType.Assembly);
}

var testElement = new UnitTestElement(testMethod)
{
// Get compiler generated type name for async test method (either void returning or task returning).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ internal interface IFileOperations
/// </remarks>
Assembly LoadAssembly(string assemblyName, bool isReflectionOnly);

/// <summary>
/// Gets the path to the .DLL of the assembly.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <returns>Path to the .DLL of the assembly.</returns>
string? GetAssemblyPath(Assembly assembly);

/// <summary>
/// Verify if a file exists in the current context.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,6 @@ internal TestMethod(string? managedTypeName, string? managedMethodName, string?[

public string? ParameterTypes { get; }

/// <summary>
/// Gets or sets the declaring assembly full name. This will be used while getting navigation data.
/// This will be null if AssemblyName is same as DeclaringAssemblyName.
/// Reason to set to null in the above case is to minimize the transfer of data across appdomains and not have a performance hit.
/// </summary>
public string? DeclaringAssemblyName
{
get;

set
{
DebugEx.Assert(value != AssemblyName, "DeclaringAssemblyName should not be the same as AssemblyName.");
field = value;
}
}

/// <summary>
/// Gets or sets the declaring class full name.
/// This will be used to resolve overloads and while getting navigation data.
Expand Down Expand Up @@ -154,11 +138,6 @@ public string? DeclaringClassFullName
/// </remarks>
internal string? TestDataSourceIgnoreMessage { get; set; }

/// <summary>
/// Gets or sets the test group set during discovery.
/// </summary>
internal string? TestGroup { get; set; }

/// <summary>
/// Gets or sets the display name set during discovery.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,6 @@ public Assembly LoadAssembly(string assemblyName, bool isReflectionOnly)
#endif
}

/// <summary>
/// Gets the path to the .DLL of the assembly.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <returns>Path to the .DLL of the assembly.</returns>
public string? GetAssemblyPath(Assembly assembly)
#if NETSTANDARD || (NETCOREAPP && !WINDOWS_UWP) || NETFRAMEWORK
// This method will never be called in source generator mode, we are providing a different provider for file operations.
#pragma warning disable IL3000 // Avoid accessing Assembly file path when publishing as a single file
=> assembly.Location;
#pragma warning disable IL3000 // Avoid accessing Assembly file path when publishing as a single file
#elif WINDOWS_UWP
=> null; // TODO: what are the options here?
#endif

/// <summary>
/// Verifies if file exists in context.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void GetTestFromMethodShouldInitiateTestMethodWithCorrectParameters()
SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true);
TypeEnumerator typeEnumerator = GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName");

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType")!, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType")!, _warnings);

Verify(testElement is not null);
Verify(testElement.TestMethod.Name == "MethodWithVoidReturnType");
Expand All @@ -281,7 +281,7 @@ public void GetTestFromMethodShouldInitializeAsyncTypeNameCorrectly()
TypeEnumerator typeEnumerator = GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName");
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("AsyncMethodWithTaskReturnType")!;

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

string? expectedAsyncTaskName = methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>()!.StateMachineType.FullName;

Expand All @@ -297,7 +297,7 @@ public void GetTestFromMethodShouldSetTestCategory()
methodInfo = new MockedMethodInfoWithExtraAttributes(methodInfo, new TestCategoryAttribute("foo"), new TestCategoryAttribute("bar"));
string[] testCategories = ["foo", "bar"];

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testCategories.SequenceEqual(testElement.TestCategory));
Expand All @@ -310,7 +310,7 @@ public void GetTestFromMethodShouldSetDoNotParallelize()
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType")!;
methodInfo = new MockedMethodInfoWithExtraAttributes(methodInfo, new DoNotParallelizeAttribute());

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testElement.DoNotParallelize);
Expand All @@ -327,7 +327,7 @@ public void GetTestFromMethodShouldFillTraitsWithTestProperties()
new TestPropertyAttribute("foo", "bar"),
new TestPropertyAttribute("fooprime", "barprime"));

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testElement.Traits!.Length == 2);
Expand All @@ -349,7 +349,7 @@ public void GetTestFromMethodShouldFillTraitsWithTestOwnerPropertyIfPresent()
new TestPropertyAttribute("fooprime", "barprime"),
new OwnerAttribute("mike"));

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testElement.Traits!.Length == 3);
Expand All @@ -368,7 +368,7 @@ public void GetTestFromMethodShouldFillTraitsWithTestPriorityPropertyIfPresent()
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType")!;
methodInfo = new MockedMethodInfoWithExtraAttributes(methodInfo, new TestPropertyAttribute("foo", "bar"), new TestPropertyAttribute("fooprime", "barprime"), new PriorityAttribute(1));

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testElement.Traits!.Length == 3);
Expand All @@ -387,7 +387,7 @@ public void GetTestFromMethodShouldSetPriority()
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType")!;
methodInfo = new MockedMethodInfoWithExtraAttributes(methodInfo, new PriorityAttribute(1));

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testElement.Priority == 1);
Expand All @@ -400,7 +400,7 @@ public void GetTestFromMethodShouldSetDescription()
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType")!;
methodInfo = new MockedMethodInfoWithExtraAttributes(methodInfo, new DescriptionAttribute("Dummy description"));

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement.Traits is not null);
Verify(testElement.Traits.Any(t => t.Name == "Description" && t.Value == "Dummy description"));
Expand All @@ -413,7 +413,7 @@ public void GetTestFromMethodShouldSetWorkItemIds()
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType")!;
methodInfo = new MockedMethodInfoWithExtraAttributes(methodInfo, new WorkItemAttribute(123), new WorkItemAttribute(345));

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(new string[] { "123", "345" }.SequenceEqual(testElement.WorkItemIds));
}
Expand All @@ -424,7 +424,7 @@ public void GetTestFromMethodShouldSetWorkItemIdsToNullIfNotAny()
TypeEnumerator typeEnumerator = GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName");
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType")!;

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement.WorkItemIds is null);
}
Expand All @@ -440,7 +440,7 @@ public void GetTestFromMethodShouldSetDeploymentItemsToNullIfNotPresent()
td => td.GetDeploymentItems(It.IsAny<MethodInfo>(), It.IsAny<Type>(), _warnings))
.Returns((KeyValuePair<string, string>[])null!);

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testElement.DeploymentItems is null);
Expand All @@ -457,39 +457,21 @@ public void GetTestFromMethodShouldSetDeploymentItems()
_testablePlatformServiceProvider.MockTestDeployment.Setup(
td => td.GetDeploymentItems(methodInfo, typeof(DummyTestClass), _warnings)).Returns(deploymentItems);

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testElement.DeploymentItems is not null);
Verify(deploymentItems.SequenceEqual(testElement.DeploymentItems));
}

public void GetTestFromMethodShouldSetDeclaringAssemblyName()
{
const bool isMethodFromSameAssembly = false;

TypeEnumerator typeEnumerator = GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName");
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("MethodWithVoidReturnType")!;

// Setup mocks
string otherAssemblyName = "ADifferentAssembly";
_testablePlatformServiceProvider.MockFileOperations.Setup(fo => fo.GetAssemblyPath(It.IsAny<Assembly>()))
.Returns(otherAssemblyName);

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, isMethodFromSameAssembly, _warnings);

Verify(testElement is not null);
Verify(otherAssemblyName == testElement.TestMethod.DeclaringAssemblyName);
}

public void GetTestFromMethodShouldSetDisplayNameToTestMethodNameIfDisplayNameIsNotPresent()
{
SetupTestClassAndTestMethods(isValidTestClass: true, isValidTestMethod: true, isMethodFromSameAssembly: true);
TypeEnumerator typeEnumerator = GetTypeEnumeratorInstance(typeof(DummyTestClass), "DummyAssemblyName");
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.MethodWithVoidReturnType))!;
methodInfo = new MockedMethodInfoWithExtraAttributes(methodInfo, new TestMethodAttribute());

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testElement.DisplayName == "MethodWithVoidReturnType");
Expand All @@ -502,7 +484,7 @@ public void GetTestFromMethodShouldSetDisplayNameFromTestMethodAttribute()
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.MethodWithVoidReturnType))!;
methodInfo = new MockedMethodInfoWithExtraAttributes(methodInfo, new TestMethodAttribute() { DisplayName = "Test method display name." });

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testElement.DisplayName == "Test method display name.");
Expand All @@ -515,7 +497,7 @@ public void GetTestFromMethodShouldSetDisplayNameFromDataTestMethodAttribute()
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.MethodWithVoidReturnType))!;
methodInfo = new MockedMethodInfoWithExtraAttributes(methodInfo, new DataTestMethodAttribute() { DisplayName = "Test method display name." });

MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, true, _warnings);
MSTest.TestAdapter.ObjectModel.UnitTestElement testElement = typeEnumerator.GetTestFromMethod(methodInfo, _warnings);

Verify(testElement is not null);
Verify(testElement.DisplayName == "Test method display name.");
Expand Down