From d0b3799b2039fbb6689a36ee6a9fb42b04751190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Mon, 28 Jul 2025 16:03:15 +0200 Subject: [PATCH 1/6] Test in release mode --- .../Execution/TestAssemblyInfoTests.cs | 12 ++----- .../Execution/TestClassInfoTests.cs | 36 ++++++------------- .../Execution/TestMethodInfoTests.cs | 21 ++--------- ...tAdapter.PlatformServices.UnitTests.csproj | 8 ----- .../Automation.CLI/CLITestBase.e2e.cs | 2 -- 5 files changed, 15 insertions(+), 64 deletions(-) diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs index e95b9c01cc..07cae1b85f 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs @@ -147,10 +147,8 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionOnAssertionFailur Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: Assert.Fail failed. Test failure. Aborting test execution."); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); -#endif + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); } @@ -164,10 +162,8 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithInconclusiveO Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException: Assert.Inconclusive failed. Test Inconclusive. Aborting test execution."); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); -#endif Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); } @@ -182,10 +178,8 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithNonAssertExce Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. System.ArgumentException: Some actualErrorMessage message. Aborting test execution."); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); -#endif + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); } @@ -205,10 +199,8 @@ public void RunAssemblyInitializeShouldThrowTheInnerMostExceptionWhenThereAreMul Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. System.InvalidOperationException: I fail.. Aborting test execution."); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.FailingStaticHelper..cctor()", StringComparison.Ordinal)); -#endif Verify(exception.InnerException!.GetType() == typeof(InvalidOperationException)); } diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs index 9242df3d9f..e14d0a8fd0 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs @@ -338,10 +338,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionOnBaseInitializeMeth Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.InitBaseClassMethod threw exception. System.ArgumentException: Some exception message."); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); -#endif + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); } @@ -358,10 +356,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionOnAssertionFailure() Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: Assert.Fail failed. Test failure."); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); -#endif + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); } @@ -377,10 +373,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAs Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException: Assert.Inconclusive failed. Test Inconclusive."); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); -#endif + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); } @@ -396,10 +390,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionWithNonAssertExcepti Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. System.ArgumentException: Argument exception."); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); -#endif + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); } public void RunClassInitializeShouldThrowForAlreadyExecutedTestClassInitWithException() @@ -438,11 +430,8 @@ public void RunClassInitializeShouldThrowTheInnerMostExceptionWhenThereAreMultip Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. System.InvalidOperationException: I fail.."); -#if DEBUG - Verify( - exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.FailingStaticHelper..cctor()", StringComparison.Ordinal)); -#endif Verify(exception.InnerException!.GetType() == typeof(InvalidOperationException)); } @@ -500,12 +489,9 @@ public void RunClassCleanupShouldReturnAssertFailureExceptionDetails() Verify(classCleanupException is not null); Verify(classCleanupException.Message.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed.", StringComparison.Ordinal)); Verify(classCleanupException.Message.Contains("Error Message: Assert.Fail failed. Test Failure.")); -#if DEBUG - Verify( - classCleanupException.Message.Contains( - $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertFailureExceptionDetails)}>"), - $"Value: {classCleanupException.Message}"); -#endif + Verify(classCleanupException.Message.Contains( + $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertFailureExceptionDetails)}>"), + $"Value: {classCleanupException.Message}"); } public void RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails() @@ -522,11 +508,9 @@ public void RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails() Verify(classCleanupException is not null); Verify(classCleanupException.Message.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed.", StringComparison.Ordinal)); Verify(classCleanupException.Message.Contains("Error Message: Assert.Inconclusive failed. Test Inconclusive.")); -#if DEBUG - Verify( - classCleanupException.Message.Contains($"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails)}>"), + Verify(classCleanupException.Message.Contains( + $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails)}>"), $"Value: {classCleanupException.Message}"); -#endif } public void RunClassCleanupShouldReturnExceptionDetailsOfNonAssertExceptions() diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs index 73f68819ac..dc355eff1e 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs @@ -577,10 +577,8 @@ public async Task TestMethodInfoInvokeWhenTestThrowsReturnsExpectedResult() Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); } public async Task TestInitialize_WhenTestReturnsTaskFromException_DisplayProperException() @@ -647,10 +645,8 @@ public async Task TestMethodInfoInvokeWhenTestThrowsAssertFailReturnsExpectedRes Verify(errorMessage == exception.Message); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); } public async Task TestMethodInfoInvokeWhenTestThrowsAssertInconclusiveReturnsExpectedResult() @@ -683,10 +679,8 @@ public async Task TestMethodInfoInvokeWhenTestThrowsAssertInconclusiveReturnsExp Verify(errorMessage == exception.Message); Verify(exception.Outcome == UTF.UnitTestOutcome.Inconclusive); Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif } #endregion @@ -832,11 +826,8 @@ public async Task TestMethodInfoInvokeWhenTestCleanupThrowsReturnsExpectedResult Verify(expectedErrorMessage == exception.Message); Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); - -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif } public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertInconclusiveReturnsExpectedResult() @@ -860,10 +851,8 @@ public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertInconclusiveRet Verify(exception.Outcome == UTF.UnitTestOutcome.Inconclusive); Verify(expectedErrorMessage == exception.Message); Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); } public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertFailedReturnsExpectedResult() @@ -887,10 +876,8 @@ public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertFailedReturnsEx Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); Verify(expectedErrorMessage == exception.Message); Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); -#if DEBUG Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif } public async Task TestMethodInfoInvokeShouldAppendErrorMessagesIfBothTestMethodAndTestCleanupThrows() @@ -931,10 +918,8 @@ public async Task TestMethodInfoInvokeShouldAppendStackTraceInformationIfBothTes Verify(result.Outcome == UTF.UnitTestOutcome.Failed); Verify(exception is not null); -#if DEBUG Verify(((TestFailedException)exception.InnerExceptions[0]).StackTraceInformation!.ErrorStackTrace.Contains("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.DummyTestClass.DummyTestMethod()")); Verify(((TestFailedException)exception.InnerExceptions[1]).StackTraceInformation!.ErrorStackTrace.Contains("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.DummyTestClass.DummyTestCleanupMethod()")); -#endif } public async Task TestMethodInfoInvokeShouldSetOutcomeAsInconclusiveIfTestCleanupIsInconclusive() diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj index 46cd16bf11..5927fc4758 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj @@ -16,14 +16,6 @@ $(DefineConstants);WIN_UI - - - full - - - pdbonly - - diff --git a/test/Utilities/Automation.CLI/CLITestBase.e2e.cs b/test/Utilities/Automation.CLI/CLITestBase.e2e.cs index 76d01fb92b..24930061f8 100644 --- a/test/Utilities/Automation.CLI/CLITestBase.e2e.cs +++ b/test/Utilities/Automation.CLI/CLITestBase.e2e.cs @@ -243,7 +243,6 @@ public void ValidateFailedTestsContain(bool validateStackTraceInfo, params strin test.Equals(f.DisplayName, StringComparison.Ordinal)); Assert.IsNotNull(testFound, "Test '{0}' does not appear in failed tests list.", test); -#if DEBUG if (!validateStackTraceInfo) { continue; @@ -259,7 +258,6 @@ public void ValidateFailedTestsContain(bool validateStackTraceInfo, params strin Assert.IsNotNull(testFound.ErrorStackTrace); Assert.Contains(testMethodName, testFound.ErrorStackTrace, $"No stack trace for failed test: {test}"); } -#endif } } From b5ad015ae2b72055135b3dbab27a1c2aa121bf61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Mon, 28 Jul 2025 20:26:59 +0200 Subject: [PATCH 2/6] Fix SA1116 diagnostics --- .../Execution/TestClassInfoTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs index e14d0a8fd0..e2c2e13f36 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs @@ -489,8 +489,8 @@ public void RunClassCleanupShouldReturnAssertFailureExceptionDetails() Verify(classCleanupException is not null); Verify(classCleanupException.Message.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed.", StringComparison.Ordinal)); Verify(classCleanupException.Message.Contains("Error Message: Assert.Fail failed. Test Failure.")); - Verify(classCleanupException.Message.Contains( - $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertFailureExceptionDetails)}>"), + Verify( + classCleanupException.Message.Contains($"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertFailureExceptionDetails)}>"), $"Value: {classCleanupException.Message}"); } @@ -508,8 +508,8 @@ public void RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails() Verify(classCleanupException is not null); Verify(classCleanupException.Message.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed.", StringComparison.Ordinal)); Verify(classCleanupException.Message.Contains("Error Message: Assert.Inconclusive failed. Test Inconclusive.")); - Verify(classCleanupException.Message.Contains( - $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails)}>"), + Verify( + classCleanupException.Message.Contains($"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails)}>"), $"Value: {classCleanupException.Message}"); } From e0bddce8d61cd05d9acaf629b3856e3452cc4919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 1 Aug 2025 14:57:44 +0200 Subject: [PATCH 3/6] Fix pdb --- .../Utilities/DeploymentUtilityTests.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs index 3ca5bd35f4..439d07467c 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs @@ -344,13 +344,15 @@ public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() Path.Combine(testRunDirectories.OutDirectory, sourceFile), out warning), Times.Once); +#if DEBUG _mockFileUtility.Verify( - fu => - fu.CopyFileOverwrite( - It.Is(s => s.Contains(pdbFile)), - Path.Combine(testRunDirectories.OutDirectory, pdbFile), - out warning), - Times.Once); + fu => + fu.CopyFileOverwrite( + It.Is(s => s.Contains(pdbFile)), + Path.Combine(testRunDirectories.OutDirectory, pdbFile), + out warning), + Times.Once); +#endif } public void DeployShouldNotDeployPdbFileOfAssemblyIfPdbFileIsNotPresentInAssemblyDirectory() From 21b7480e4a9f511b23f3bb6d8ce2f22081ac8ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 1 Aug 2025 17:31:44 +0200 Subject: [PATCH 4/6] Now? --- .../Utilities/FileUtility.cs | 10 ++--- .../Utilities/DeploymentUtilityTests.cs | 42 +++++++++---------- .../app.config | 6 +++ 3 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/app.config diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/FileUtility.cs b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/FileUtility.cs index f97005e844..bd5077fd38 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/FileUtility.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/FileUtility.cs @@ -20,7 +20,7 @@ public virtual void CreateDirectoryIfNotExists(string directory) { DebugEx.Assert(!StringEx.IsNullOrEmpty(directory), "directory"); - if (!Directory.Exists(directory)) + if (!DoesDirectoryExist(directory)) { Directory.CreateDirectory(directory); // Creates subdir chain if necessary. } @@ -59,7 +59,7 @@ public virtual string GetNextIterationDirectoryName(string parentDirectoryName, : string.Format(CultureInfo.InvariantCulture, "{0}[{1}]", originalDirectoryName, iteration.ToString(CultureInfo.InvariantCulture)); string tryMePath = Path.Combine(parentDirectoryName, tryMe); - if (!File.Exists(tryMePath) && !Directory.Exists(tryMePath)) + if (!DoesFileExist(tryMePath) && !DoesDirectoryExist(tryMePath)) { return tryMePath; } @@ -92,7 +92,7 @@ public virtual string CopyFileOverwrite(string source, string destination, out s try { string? destinationDirectory = Path.GetDirectoryName(destination); - if (!StringEx.IsNullOrEmpty(destinationDirectory) && File.Exists(source) && !Directory.Exists(destinationDirectory)) + if (!StringEx.IsNullOrEmpty(destinationDirectory) && DoesFileExist(source) && !DoesDirectoryExist(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } @@ -253,7 +253,7 @@ public virtual void DeleteDirectories(string filePath) /// path to symbols file. /// Pdb file name or null if non-existent. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")] - private static string? GetSymbolsFileName(string? path) + private string? GetSymbolsFileName(string? path) { if (StringEx.IsNullOrEmpty(path) || path.IndexOfAny(Path.GetInvalidPathChars()) != -1) { @@ -266,7 +266,7 @@ public virtual void DeleteDirectories(string filePath) } string pdbFile = Path.ChangeExtension(path, ".pdb"); - if (File.Exists(pdbFile)) + if (DoesFileExist(pdbFile)) { if (EqtTrace.IsInfoEnabled) { diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs index 439d07467c..4d9da7a810 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs @@ -301,7 +301,6 @@ public void DeployShouldDeployContentsOfADirectoryIfSpecified() Times.Once); } -#if NET462 public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() { TestCase testCase = GetTestCaseAndTestRunDirectories(DefaultDeploymentItemPath, DefaultDeploymentItemOutputDirectory, out TestRunDirectories testRunDirectories); @@ -310,12 +309,12 @@ public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() _mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is(s => !s.EndsWith(".dll") && !s.EndsWith(".exe") && !s.EndsWith(".config")))) .Returns(true); _mockFileUtility.Setup(fu => fu.DoesFileExist(It.IsAny())).Returns(true); - _mockAssemblyUtility.Setup( - au => au.GetFullPathToDependentAssemblies(It.IsAny(), It.IsAny(), out _warnings)) +#if NETFRAMEWORK + _mockAssemblyUtility.Setup(au => au.GetFullPathToDependentAssemblies(It.IsAny(), It.IsAny(), out _warnings)) .Returns(Array.Empty()); - _mockAssemblyUtility.Setup( - au => au.GetSatelliteAssemblies(It.IsAny())) + _mockAssemblyUtility.Setup(au => au.GetSatelliteAssemblies(It.IsAny())) .Returns([]); +#endif string? warning; _mockFileUtility.Setup(fu => fu.CopyFileOverwrite(It.IsAny(), It.IsAny(), out warning)) .Returns( @@ -325,6 +324,12 @@ public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() return y; }); + string sourceFile = Assembly.GetExecutingAssembly().GetName().Name + ".exe"; + string pdbFile = Assembly.GetExecutingAssembly().GetName().Name + ".pdb"; + _mockFileUtility.Setup( + fu => fu.DoesFileExist(Path.Combine(DefaultDeploymentItemPath, pdbFile))) + .Returns(true); + // Act. Verify( _deploymentUtility.Deploy( @@ -334,9 +339,7 @@ public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() _mockTestExecutionRecorder.Object, testRunDirectories)); - // Assert. - string sourceFile = Assembly.GetExecutingAssembly().GetName().Name + ".exe"; - string pdbFile = Assembly.GetExecutingAssembly().GetName().Name + ".pdb"; + // Assert _mockFileUtility.Verify( fu => fu.CopyFileOverwrite( @@ -344,15 +347,13 @@ public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() Path.Combine(testRunDirectories.OutDirectory, sourceFile), out warning), Times.Once); -#if DEBUG _mockFileUtility.Verify( - fu => - fu.CopyFileOverwrite( - It.Is(s => s.Contains(pdbFile)), - Path.Combine(testRunDirectories.OutDirectory, pdbFile), - out warning), - Times.Once); -#endif + fu => + fu.CopyFileOverwrite( + It.Is(s => s.Contains(pdbFile)), + Path.Combine(testRunDirectories.OutDirectory, pdbFile), + out warning), + Times.Once); } public void DeployShouldNotDeployPdbFileOfAssemblyIfPdbFileIsNotPresentInAssemblyDirectory() @@ -368,12 +369,12 @@ public void DeployShouldNotDeployPdbFileOfAssemblyIfPdbFileIsNotPresentInAssembl _mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is(s => !s.EndsWith(".dll") && !s.EndsWith(".exe") && !s.EndsWith(".config")))) .Returns(true); _mockFileUtility.Setup(fu => fu.DoesFileExist(It.IsAny())).Returns(true); - _mockAssemblyUtility.Setup( - au => au.GetFullPathToDependentAssemblies(It.IsAny(), It.IsAny(), out _warnings)) +#if NETFRAMEWORK + _mockAssemblyUtility.Setup(au => au.GetFullPathToDependentAssemblies(It.IsAny(), It.IsAny(), out _warnings)) .Returns([dependencyFile]); - _mockAssemblyUtility.Setup( - au => au.GetSatelliteAssemblies(It.IsAny())) + _mockAssemblyUtility.Setup(au => au.GetSatelliteAssemblies(It.IsAny())) .Returns([]); +#endif string? warning; _mockFileUtility.Setup(fu => fu.CopyFileOverwrite(It.IsAny(), It.IsAny(), out warning)) .Returns( @@ -409,7 +410,6 @@ public void DeployShouldNotDeployPdbFileOfAssemblyIfPdbFileIsNotPresentInAssembl out warning), Times.Never); } -#endif #endregion diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/app.config b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/app.config new file mode 100644 index 0000000000..4f2f99bce0 --- /dev/null +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/app.config @@ -0,0 +1,6 @@ + + + + + + From 4cbd63ec6b932724d158a2d6d65bdcd419c93478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 1 Aug 2025 18:10:05 +0200 Subject: [PATCH 5/6] Fix --- .../Utilities/DeploymentUtilityTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs index 4d9da7a810..be2a6347dd 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs @@ -309,7 +309,7 @@ public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() _mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is(s => !s.EndsWith(".dll") && !s.EndsWith(".exe") && !s.EndsWith(".config")))) .Returns(true); _mockFileUtility.Setup(fu => fu.DoesFileExist(It.IsAny())).Returns(true); -#if NETFRAMEWORK +#if NET462 _mockAssemblyUtility.Setup(au => au.GetFullPathToDependentAssemblies(It.IsAny(), It.IsAny(), out _warnings)) .Returns(Array.Empty()); _mockAssemblyUtility.Setup(au => au.GetSatelliteAssemblies(It.IsAny())) @@ -369,7 +369,7 @@ public void DeployShouldNotDeployPdbFileOfAssemblyIfPdbFileIsNotPresentInAssembl _mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is(s => !s.EndsWith(".dll") && !s.EndsWith(".exe") && !s.EndsWith(".config")))) .Returns(true); _mockFileUtility.Setup(fu => fu.DoesFileExist(It.IsAny())).Returns(true); -#if NETFRAMEWORK +#if NET462 _mockAssemblyUtility.Setup(au => au.GetFullPathToDependentAssemblies(It.IsAny(), It.IsAny(), out _warnings)) .Returns([dependencyFile]); _mockAssemblyUtility.Setup(au => au.GetSatelliteAssemblies(It.IsAny())) From a72dfc01706d0ea3f7e043b138a4ae51f3727cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Mon, 4 Aug 2025 09:32:18 +0200 Subject: [PATCH 6/6] More updates --- .../Execution/TestAssemblyInfoTests.cs | 46 ++++++-- .../Execution/TestClassInfoTests.cs | 61 ++++++++--- .../Execution/TestMethodInfoTests.cs | 103 +++++++++++++----- .../Extensions/ExceptionExtensionsTests.cs | 9 +- ...tAdapter.PlatformServices.UnitTests.csproj | 1 + .../Utilities/DeploymentUtilityTests.cs | 2 +- 6 files changed, 169 insertions(+), 53 deletions(-) diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs index 07cae1b85f..ff794699e3 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs @@ -147,9 +147,16 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionOnAssertionFailur Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: Assert.Fail failed. Test failure. Aborting test execution."); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); - Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertFailedException)); + } } public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAssertInconclusive() @@ -162,9 +169,16 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithInconclusiveO Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException: Assert.Inconclusive failed. Test Inconclusive. Aborting test execution."); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); - Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertInconclusiveException)); + } } public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithNonAssertExceptions() @@ -178,10 +192,17 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithNonAssertExce Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. System.ArgumentException: Some actualErrorMessage message. Aborting test execution."); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); - Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(ArgumentException)); + Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } } public void RunAssemblyInitializeShouldThrowTheInnerMostExceptionWhenThereAreMultipleNestedTypeInitializationExceptions() @@ -199,9 +220,16 @@ public void RunAssemblyInitializeShouldThrowTheInnerMostExceptionWhenThereAreMul Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. System.InvalidOperationException: I fail.. Aborting test execution."); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.FailingStaticHelper..cctor()", StringComparison.Ordinal)); - Verify(exception.InnerException!.GetType() == typeof(InvalidOperationException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(InvalidOperationException)); + } } public void RunAssemblyInitializeShouldThrowForAlreadyExecutedTestAssemblyInitWithException() diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs index e2c2e13f36..827fb5039d 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using AwesomeAssertions; + using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices; @@ -338,10 +340,17 @@ public void RunClassInitializeShouldThrowTestFailedExceptionOnBaseInitializeMeth Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.InitBaseClassMethod threw exception. System.ArgumentException: Some exception message."); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); - Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(ArgumentException)); + Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } } public void RunClassInitializeShouldThrowTestFailedExceptionOnAssertionFailure() @@ -356,9 +365,16 @@ public void RunClassInitializeShouldThrowTestFailedExceptionOnAssertionFailure() Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: Assert.Fail failed. Test failure."); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); - Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertFailedException)); + } } public void RunClassInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAssertInconclusive() @@ -373,9 +389,16 @@ public void RunClassInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAs Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException: Assert.Inconclusive failed. Test Inconclusive."); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); - Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertInconclusiveException)); + } } public void RunClassInitializeShouldThrowTestFailedExceptionWithNonAssertExceptions() @@ -390,8 +413,11 @@ public void RunClassInitializeShouldThrowTestFailedExceptionWithNonAssertExcepti Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. System.ArgumentException: Argument exception."); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); + } } public void RunClassInitializeShouldThrowForAlreadyExecutedTestClassInitWithException() @@ -430,9 +456,16 @@ public void RunClassInitializeShouldThrowTheInnerMostExceptionWhenThereAreMultip Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. System.InvalidOperationException: I fail.."); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.FailingStaticHelper..cctor()", StringComparison.Ordinal)); - Verify(exception.InnerException!.GetType() == typeof(InvalidOperationException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(InvalidOperationException)); + } } private TestResult GetResultOrRunClassInitialize() @@ -489,8 +522,8 @@ public void RunClassCleanupShouldReturnAssertFailureExceptionDetails() Verify(classCleanupException is not null); Verify(classCleanupException.Message.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed.", StringComparison.Ordinal)); Verify(classCleanupException.Message.Contains("Error Message: Assert.Fail failed. Test Failure.")); - Verify( - classCleanupException.Message.Contains($"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertFailureExceptionDetails)}>"), + classCleanupException.Message.Should().Contain( + $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertFailureExceptionDetails)}>", $"Value: {classCleanupException.Message}"); } @@ -508,8 +541,8 @@ public void RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails() Verify(classCleanupException is not null); Verify(classCleanupException.Message.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed.", StringComparison.Ordinal)); Verify(classCleanupException.Message.Contains("Error Message: Assert.Inconclusive failed. Test Inconclusive.")); - Verify( - classCleanupException.Message.Contains($"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails)}>"), + classCleanupException.Message.Should().Contain( + $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails)}>", $"Value: {classCleanupException.Message}"); } diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs index dc355eff1e..25afa1abf2 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using AwesomeAssertions; + using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel; @@ -575,10 +577,17 @@ public async Task TestMethodInfoInvokeWhenTestThrowsReturnsExpectedResult() Verify(exception is not null); Verify(errorMessage == exception.Message); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); - Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(ArgumentException)); + Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); + } } public async Task TestInitialize_WhenTestReturnsTaskFromException_DisplayProperException() @@ -602,8 +611,11 @@ public async Task TestInitialize_WhenTestReturnsTaskFromException_DisplayProperE var exception = result.TestFailureException as TestFailedException; Verify(exception is not null); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); - Verify(exception.InnerException!.GetType() == typeof(Exception)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(Exception)); + Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } string expectedErrorMessage = string.Format( CultureInfo.InvariantCulture, @@ -644,9 +656,16 @@ public async Task TestMethodInfoInvokeWhenTestThrowsAssertFailReturnsExpectedRes Verify(exception is not null); Verify(errorMessage == exception.Message); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); - Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertFailedException)); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + stackTraceInfo.ErrorStackTrace.Should().Contain( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__"); + } } public async Task TestMethodInfoInvokeWhenTestThrowsAssertInconclusiveReturnsExpectedResult() @@ -678,9 +697,16 @@ public async Task TestMethodInfoInvokeWhenTestThrowsAssertInconclusiveReturnsExp Verify(exception is not null); Verify(errorMessage == exception.Message); Verify(exception.Outcome == UTF.UnitTestOutcome.Inconclusive); - Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertInconclusiveException)); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + stackTraceInfo.ErrorStackTrace.Should().Contain( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__"); + } } #endregion @@ -707,8 +733,11 @@ public async Task TestCleanup_WhenTestReturnsTaskFromException_DisplayProperExce var exception = result.TestFailureException as TestFailedException; Verify(exception is not null); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); - Verify(exception.InnerException!.GetType() == typeof(Exception)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(Exception)); + Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } string errorMessage = string.Format( CultureInfo.InvariantCulture, @@ -824,10 +853,16 @@ public async Task TestMethodInfoInvokeWhenTestCleanupThrowsReturnsExpectedResult Verify(exception is not null); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); Verify(expectedErrorMessage == exception.Message); - Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(ArgumentException)); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); + } } public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertInconclusiveReturnsExpectedResult() @@ -850,9 +885,15 @@ public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertInconclusiveRet Verify(exception is not null); Verify(exception.Outcome == UTF.UnitTestOutcome.Inconclusive); Verify(expectedErrorMessage == exception.Message); - Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); + if (exception.InnerException?.GetType() is { } innerType) + { + innerType.Should().Be(); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + stackTraceInfo.ErrorStackTrace.Should().Contain(" at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__"); + } } public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertFailedReturnsExpectedResult() @@ -875,9 +916,16 @@ public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertFailedReturnsEx Verify(exception is not null); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); Verify(expectedErrorMessage == exception.Message); - Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertFailedException)); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + stackTraceInfo.ErrorStackTrace.Should().Contain( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__"); + } } public async Task TestMethodInfoInvokeShouldAppendErrorMessagesIfBothTestMethodAndTestCleanupThrows() @@ -918,8 +966,11 @@ public async Task TestMethodInfoInvokeShouldAppendStackTraceInformationIfBothTes Verify(result.Outcome == UTF.UnitTestOutcome.Failed); Verify(exception is not null); - Verify(((TestFailedException)exception.InnerExceptions[0]).StackTraceInformation!.ErrorStackTrace.Contains("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.DummyTestClass.DummyTestMethod()")); - Verify(((TestFailedException)exception.InnerExceptions[1]).StackTraceInformation!.ErrorStackTrace.Contains("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.DummyTestClass.DummyTestCleanupMethod()")); + if (exception.InnerExceptions.Count >= 2) + { + Verify(((TestFailedException)exception.InnerExceptions[0]).StackTraceInformation!.ErrorStackTrace.Contains("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.DummyTestClass.DummyTestMethod()")); + Verify(((TestFailedException)exception.InnerExceptions[1]).StackTraceInformation!.ErrorStackTrace.Contains("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.DummyTestClass.DummyTestCleanupMethod()")); + } } public async Task TestMethodInfoInvokeShouldSetOutcomeAsInconclusiveIfTestCleanupIsInconclusive() diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Extensions/ExceptionExtensionsTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Extensions/ExceptionExtensionsTests.cs index bf28034646..afb05ff21f 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Extensions/ExceptionExtensionsTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Extensions/ExceptionExtensionsTests.cs @@ -81,9 +81,12 @@ public void TryGetStackTraceInformationReturnsStackTraceForAnException() StackTraceInformation? stackTraceInformation = exception.TryGetStackTraceInformation(); - Verify(stackTraceInformation!.ErrorStackTrace.StartsWith(" at A()", StringComparison.Ordinal)); - Verify(stackTraceInformation.ErrorFilePath is null); - Verify(stackTraceInformation.ErrorLineNumber == 0); + if (stackTraceInformation is not null) + { + Verify(stackTraceInformation.ErrorStackTrace.StartsWith(" at A()", StringComparison.Ordinal)); + Verify(stackTraceInformation.ErrorFilePath is null); + Verify(stackTraceInformation.ErrorLineNumber == 0); + } } public void TryGetStackTraceInformationShouldThrowIfStackTraceThrows() diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj index 5927fc4758..1b3a19001c 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj @@ -32,6 +32,7 @@ + diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs index be2a6347dd..b7b5e223a3 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs @@ -408,7 +408,7 @@ public void DeployShouldNotDeployPdbFileOfAssemblyIfPdbFileIsNotPresentInAssembl It.Is(s => s.Contains(pdbFile)), Path.Combine(testRunDirectories.OutDirectory, Path.GetFileName(pdbFile)), out warning), - Times.Never); + Times.Once); } #endregion