From 3a773d055a767f9872bc0a7f1b8d4501dc2659fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Tue, 27 Sep 2022 11:41:26 +0200 Subject: [PATCH] Fix issue causing null ref when test class has no namespace (#1283) --- TestFx.sln | 8 +++- .../NoNamespaceTests.cs | 37 +++++++++++++++++++ .../HierarchyProject/ClassWithNamespace.cs | 12 ++++++ .../HierarchyProject/ClassWithNoNamespace.cs | 11 ++++++ .../HierarchyProject/HierarchyProject.csproj | 15 ++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 test/E2ETests/DiscoveryAndExecutionTests/NoNamespaceTests.cs create mode 100644 test/E2ETests/TestAssets/HierarchyProject/ClassWithNamespace.cs create mode 100644 test/E2ETests/TestAssets/HierarchyProject/ClassWithNoNamespace.cs create mode 100644 test/E2ETests/TestAssets/HierarchyProject/HierarchyProject.csproj diff --git a/TestFx.sln b/TestFx.sln index bb7a9761d..4120b1105 100644 --- a/TestFx.sln +++ b/TestFx.sln @@ -205,6 +205,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OutputTestProject", "test\E EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestFramework.ForTestingMSTest", "test\TestFramework.ForTestingMSTest\TestFramework.ForTestingMSTest.csproj", "{0685FBC3-C3A9-43A9-B15C-15BAA39705FE}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HierarchyProject", "test\E2ETests\TestAssets\HierarchyProject\HierarchyProject.csproj", "{94A4DAA8-9645-4161-91F6-11EB1AD70EFC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -213,7 +215,6 @@ Global GlobalSection(ProjectConfigurationPlatforms) = postSolution {98BA6D2C-1F3D-4636-8E1D-D4932B7A253D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {98BA6D2C-1F3D-4636-8E1D-D4932B7A253D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {98BA6D2C-1F3D-4636-8E1D-D4932B7A253D}.Debug|ARM.ActiveCfg = Debug|Any CPU {98BA6D2C-1F3D-4636-8E1D-D4932B7A253D}.Release|Any CPU.ActiveCfg = Release|Any CPU {98BA6D2C-1F3D-4636-8E1D-D4932B7A253D}.Release|Any CPU.Build.0 = Release|Any CPU {BBC99A6B-4490-49DD-9C12-AF2C1E95576E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU @@ -360,6 +361,10 @@ Global {0685FBC3-C3A9-43A9-B15C-15BAA39705FE}.Debug|Any CPU.Build.0 = Debug|Any CPU {0685FBC3-C3A9-43A9-B15C-15BAA39705FE}.Release|Any CPU.ActiveCfg = Release|Any CPU {0685FBC3-C3A9-43A9-B15C-15BAA39705FE}.Release|Any CPU.Build.0 = Release|Any CPU + {94A4DAA8-9645-4161-91F6-11EB1AD70EFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {94A4DAA8-9645-4161-91F6-11EB1AD70EFC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {94A4DAA8-9645-4161-91F6-11EB1AD70EFC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {94A4DAA8-9645-4161-91F6-11EB1AD70EFC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -421,6 +426,7 @@ Global {44A504D9-A0D6-427D-BFB2-DB144A74F0D5} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8} {66608D86-416A-49AF-A937-C47F7E4586AE} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8} {0685FBC3-C3A9-43A9-B15C-15BAA39705FE} = {33D3029D-E653-4929-BB31-C714178C4BEE} + {94A4DAA8-9645-4161-91F6-11EB1AD70EFC} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {31E0F4D5-975A-41CC-933E-545B2201FAF9} diff --git a/test/E2ETests/DiscoveryAndExecutionTests/NoNamespaceTests.cs b/test/E2ETests/DiscoveryAndExecutionTests/NoNamespaceTests.cs new file mode 100644 index 000000000..ae385b13d --- /dev/null +++ b/test/E2ETests/DiscoveryAndExecutionTests/NoNamespaceTests.cs @@ -0,0 +1,37 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.MSTestV2.Smoke.DiscoveryAndExecutionTests; + +using Microsoft.MSTestV2.CLIAutomation; + +using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Extensions; +using Microsoft.VisualStudio.TestPlatform.ObjectModel; + +public class NoNamespaceTests : CLITestBase +{ + private const string TestAssembly = "HierarchyProject.dll"; + + public void TestsAreDiscoveredWithExpectedHierarchy() + { + // Arrange & Act + var testCases = DiscoverTests(GetAssetFullPath(TestAssembly)); + + // Assert + Verify(testCases.Count == 2); + + VerifyHierarchy(testCases[0], null, "ClassWithNoNamespace", "MyMethodUnderTest"); + VerifyHierarchy(testCases[1], "SomeNamespace.WithMultipleLevels", "ClassWithNamespace", "MyMethodUnderTest"); + } + + private static void VerifyHierarchy(TestCase testCase, string expectedNamespace, string expectedClassName, string expectedMethodName) + { + var hierarchy = testCase.GetPropertyValue(TestCaseExtensions.HierarchyProperty) as string[]; + Verify(hierarchy?.Length == 4); + // This level is always null. + Verify(hierarchy[0] == null); + Verify(hierarchy[1] == expectedNamespace); + Verify(hierarchy[2] == expectedClassName); + Verify(hierarchy[3] == expectedMethodName); + } +} diff --git a/test/E2ETests/TestAssets/HierarchyProject/ClassWithNamespace.cs b/test/E2ETests/TestAssets/HierarchyProject/ClassWithNamespace.cs new file mode 100644 index 000000000..78860121d --- /dev/null +++ b/test/E2ETests/TestAssets/HierarchyProject/ClassWithNamespace.cs @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace SomeNamespace.WithMultipleLevels; +[TestClass] +public class ClassWithNamespace +{ + [TestMethod] + public void MyMethodUnderTest() { } +} diff --git a/test/E2ETests/TestAssets/HierarchyProject/ClassWithNoNamespace.cs b/test/E2ETests/TestAssets/HierarchyProject/ClassWithNoNamespace.cs new file mode 100644 index 000000000..cb17d0a07 --- /dev/null +++ b/test/E2ETests/TestAssets/HierarchyProject/ClassWithNoNamespace.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +[TestClass] +public class ClassWithNoNamespace +{ + [TestMethod] + public void MyMethodUnderTest() { } +} diff --git a/test/E2ETests/TestAssets/HierarchyProject/HierarchyProject.csproj b/test/E2ETests/TestAssets/HierarchyProject/HierarchyProject.csproj new file mode 100644 index 000000000..8f3b22a56 --- /dev/null +++ b/test/E2ETests/TestAssets/HierarchyProject/HierarchyProject.csproj @@ -0,0 +1,15 @@ + + + + net462 + false + false + $(RepoRoot)artifacts\TestAssets\ + + + + + + + +