diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/FileOperationsTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/FileOperationsTests.cs index e532caaa28..b03838b697 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/FileOperationsTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/FileOperationsTests.cs @@ -19,24 +19,15 @@ public void LoadAssemblyShouldThrowExceptionIfTheFileNameHasInvalidCharacters() string filePath = "temp<>txt"; void A() => _fileOperations.LoadAssembly(filePath, false); - Type expectedException; #if NETCOREAPP - expectedException = typeof(FileNotFoundException); + VerifyThrows(A); #else - expectedException = typeof(ArgumentException); + VerifyThrows(A); #endif - - Exception ex = VerifyThrows(A); - Verify(ex.GetType() == expectedException); } - public void LoadAssemblyShouldThrowExceptionIfFileIsNotFound() - { - string filePath = "temptxt"; - void A() => _fileOperations.LoadAssembly(filePath, false); - Exception ex = VerifyThrows(A); - Verify(ex is FileNotFoundException); - } + public void LoadAssemblyShouldThrowExceptionIfFileIsNotFound() => + VerifyThrows(() => _fileOperations.LoadAssembly("temptxt", false)); public void LoadAssemblyShouldLoadAssemblyInCurrentContext() { diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/MSTestAdapterSettingsTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/MSTestAdapterSettingsTests.cs index f6f2bf6b9c..e840af3d2c 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/MSTestAdapterSettingsTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/MSTestAdapterSettingsTests.cs @@ -224,8 +224,7 @@ public void ToSettingsShouldThrowExceptionWhenRunSettingsXmlIsWrong() void ShouldThrowException() => MSTestAdapterSettings.ToSettings(reader); - Exception ex = VerifyThrows(ShouldThrowException); - Verify(ex is SettingsException); + VerifyThrows(ShouldThrowException); } #endregion diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/MSTestSettingsProviderTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/MSTestSettingsProviderTests.cs index 038f2b6143..5eb74aa7cf 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/MSTestSettingsProviderTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/MSTestSettingsProviderTests.cs @@ -55,11 +55,8 @@ public void SettingsShouldReturnInitializedSettings() Verify(!MSTestSettingsProvider.Settings.DeploymentEnabled); } - public void LoadShouldThrowIfReaderIsNull() - { - Exception exception = VerifyThrows(() => _settingsProvider.Load(null)); - Verify(exception is ArgumentNullException); - } + public void LoadShouldThrowIfReaderIsNull() => + VerifyThrows(() => _settingsProvider.Load(null)); public void LoadShouldReadAndFillInSettings() { diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TestContextImplementationTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TestContextImplementationTests.cs index 896d1e8dc7..b2a1ec1e32 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TestContextImplementationTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TestContextImplementationTests.cs @@ -140,10 +140,7 @@ public void AddResultFileShouldThrowIfFileNameIsNull() { _testContextImplementation = new TestContextImplementation(_testMethod.Object, new ThreadSafeStringWriter(null, "test"), _properties); - Exception exception = - VerifyThrows(() => _testContextImplementation.AddResultFile(null)); - - Verify(typeof(ArgumentException) == exception.GetType()); + ArgumentException exception = VerifyThrows(() => _testContextImplementation.AddResultFile(null)); Verify(exception.Message.Contains(Resource.Common_CannotBeNullOrEmpty)); } @@ -151,10 +148,7 @@ public void AddResultFileShouldThrowIfFileNameIsEmpty() { _testContextImplementation = new TestContextImplementation(_testMethod.Object, new ThreadSafeStringWriter(null, "test"), _properties); - Exception exception = - VerifyThrows(() => _testContextImplementation.AddResultFile(string.Empty)); - - Verify(typeof(ArgumentException) == exception.GetType()); + ArgumentException exception = VerifyThrows(() => _testContextImplementation.AddResultFile(string.Empty)); Verify(exception.Message.Contains(Resource.Common_CannotBeNullOrEmpty)); } diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TraceListenerManagerTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TraceListenerManagerTests.cs index 2a45634576..d95cadf0fa 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TraceListenerManagerTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TraceListenerManagerTests.cs @@ -57,9 +57,7 @@ public void DisposeShouldCallDisposeOnCorrespondingTraceListener() traceListenerManager.Dispose(traceListener); // Trying to write after closing textWriter should throw exception - void ShouldThrowException() => writer.WriteLine("Try to write something"); - Exception ex = VerifyThrows(ShouldThrowException); - Verify(ex is ObjectDisposedException); + VerifyThrows(() => writer.WriteLine("Try to write something")); } } diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TraceListenerTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TraceListenerTests.cs index fbca5ffb78..f97695a13a 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TraceListenerTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TraceListenerTests.cs @@ -30,8 +30,7 @@ public void DisposeShouldDisposeCorrespondingTextWriter() // Trying to write after disposing textWriter should throw exception void ShouldThrowException() => writer.WriteLine("Try to write something"); - Exception ex = VerifyThrows(ShouldThrowException); - Verify(ex is ObjectDisposedException); + VerifyThrows(ShouldThrowException); } } diff --git a/test/UnitTests/MSTestAdapter.UnitTests/Execution/TestAssemblyInfoTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/Execution/TestAssemblyInfoTests.cs index ab836b22de..194bb26609 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/Execution/TestAssemblyInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/Execution/TestAssemblyInfoTests.cs @@ -42,8 +42,7 @@ void Action() _testAssemblyInfo.AssemblyInitializeMethod = _dummyMethodInfo; } - Exception ex = VerifyThrows(Action); - Verify(ex.GetType() == typeof(TypeInspectionException)); + VerifyThrows(Action); } public void TestAssemblyInfoAssemblyCleanupMethodThrowsForMultipleAssemblyCleanupMethods() @@ -54,8 +53,7 @@ void Action() _testAssemblyInfo.AssemblyCleanupMethod = _dummyMethodInfo; } - Exception ex = VerifyThrows(Action); - Verify(ex.GetType() == typeof(TypeInspectionException)); + VerifyThrows(Action); } public void TestAssemblyHasExecutableCleanupMethodShouldReturnFalseIfAssemblyHasNoCleanupMethod() => Verify(!_testAssemblyInfo.HasExecutableCleanupMethod); @@ -97,8 +95,7 @@ public void RunAssemblyInitializeShouldThrowIfTestContextIsNull() void Action() => _testAssemblyInfo.RunAssemblyInitialize(null); - Exception ex = VerifyThrows(Action); - Verify(ex.GetType() == typeof(NullReferenceException)); + VerifyThrows(Action); } public void RunAssemblyInitializeShouldNotExecuteAssemblyInitializeIfItHasAlreadyExecuted() @@ -151,9 +148,7 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionOnAssertionFailur DummyTestClass.AssemblyInitializeMethodBody = tc => UTF.Assert.Fail("Test failure"); _testAssemblyInfo.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("AssemblyInitializeMethod"); - var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)) as TestFailedException; - - Verify(exception is not null); + TestFailedException exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)); Verify(exception.Outcome == UnitTestOutcome.Failed); Verify( exception.Message @@ -169,9 +164,7 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithInconclusiveO DummyTestClass.AssemblyInitializeMethodBody = tc => UTF.Assert.Inconclusive("Test Inconclusive"); _testAssemblyInfo.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("AssemblyInitializeMethod"); - var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)) as TestFailedException; - - Verify(exception is not null); + TestFailedException exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)); Verify(exception.Outcome == UnitTestOutcome.Inconclusive); Verify( exception.Message @@ -187,9 +180,8 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithNonAssertExce DummyTestClass.AssemblyInitializeMethodBody = tc => throw new ArgumentException("Some actualErrorMessage message", new InvalidOperationException("Inner actualErrorMessage message")); _testAssemblyInfo.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("AssemblyInitializeMethod"); - var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)) as TestFailedException; + var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)); - Verify(exception is not null); Verify(exception.Outcome == UnitTestOutcome.Failed); Verify( exception.Message @@ -210,9 +202,8 @@ public void RunAssemblyInitializeShouldThrowTheInnerMostExceptionWhenThereAreMul FailingStaticHelper.DoWork(); _testAssemblyInfo.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("AssemblyInitializeMethod"); - var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)) as TestFailedException; + TestFailedException exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)); - Verify(exception is not null); Verify(exception.Outcome == UnitTestOutcome.Failed); Verify( exception.Message @@ -230,9 +221,7 @@ public void RunAssemblyInitializeShouldThrowForAlreadyExecutedTestAssemblyInitWi _testAssemblyInfo.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("AssemblyInitializeMethod"); _testAssemblyInfo.AssemblyInitializationException = new TestFailedException(UnitTestOutcome.Failed, "Cached Test failure"); - var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)) as TestFailedException; - - Verify(exception is not null); + TestFailedException exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)); Verify(exception.Outcome == UnitTestOutcome.Failed); Verify(exception.Message == "Cached Test failure"); } diff --git a/test/UnitTests/MSTestAdapter.UnitTests/Execution/TestClassInfoTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/Execution/TestClassInfoTests.cs index 74aa531cae..77fa520e93 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/Execution/TestClassInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/Execution/TestClassInfoTests.cs @@ -81,8 +81,7 @@ void Action() _testClassInfo.ClassInitializeMethod = _testClassType.GetMethods().First(); } - Exception ex = VerifyThrows(Action); - Verify(ex.GetType() == typeof(TypeInspectionException)); + VerifyThrows(Action); } public void TestClassInfoClassCleanupMethodSetShouldThrowForMultipleClassCleanupMethods() @@ -93,8 +92,7 @@ void Action() _testClassInfo.ClassCleanupMethod = _testClassType.GetMethods().First(); } - Exception ex = VerifyThrows(Action); - Verify(ex.GetType() == typeof(TypeInspectionException)); + VerifyThrows(Action); } public void TestClassInfoClassCleanupMethodShouldNotInvokeWhenNoTestClassInitializedIsCalled() @@ -174,10 +172,7 @@ public void RunClassInitializeShouldThrowIfTestContextIsNull() _testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod"); - void Action() => _testClassInfo.RunClassInitialize(null); - - Exception ex = VerifyThrows(Action); - Verify(ex.GetType() == typeof(NullReferenceException)); + VerifyThrows(() => _testClassInfo.RunClassInitialize(null)); } public void RunClassInitializeShouldNotExecuteClassInitializeIfItHasAlreadyExecuted() @@ -309,9 +304,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionOnBaseInitializeMeth _testClassInfo.BaseClassInitMethods.Add(typeof(DummyBaseTestClass).GetMethod("InitBaseClassMethod")); - var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException; + TestFailedException exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)); - Verify(exception is not null); Verify(exception.Outcome == UnitTestOutcome.Failed); Verify( exception.Message @@ -328,9 +322,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionOnAssertionFailure() DummyTestClass.ClassInitializeMethodBody = tc => UTF.Assert.Fail("Test failure"); _testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod"); - var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException; + TestFailedException exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)); - Verify(exception is not null); Verify(exception.Outcome == UnitTestOutcome.Failed); Verify( exception.Message @@ -346,9 +339,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAs DummyTestClass.ClassInitializeMethodBody = tc => UTF.Assert.Inconclusive("Test Inconclusive"); _testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod"); - var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException; + TestFailedException exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)); - Verify(exception is not null); Verify(exception.Outcome == UnitTestOutcome.Inconclusive); Verify( exception.Message @@ -364,9 +356,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionWithNonAssertExcepti DummyTestClass.ClassInitializeMethodBody = tc => throw new ArgumentException("Argument exception"); _testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod"); - var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException; + TestFailedException exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)); - Verify(exception is not null); Verify(exception.Outcome == UnitTestOutcome.Failed); Verify( exception.Message @@ -382,9 +373,7 @@ public void RunClassInitializeShouldThrowForAlreadyExecutedTestClassInitWithExce _testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod"); _testClassInfo.ClassInitializationException = new TestFailedException(UnitTestOutcome.Failed, "Cached Test failure"); - var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException; - - Verify(exception is not null); + TestFailedException exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)); Verify(exception.Outcome == UnitTestOutcome.Failed); Verify(exception.Message == "Cached Test failure"); } @@ -406,9 +395,8 @@ public void RunClassInitializeShouldThrowTheInnerMostExceptionWhenThereAreMultip FailingStaticHelper.DoWork(); _testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod"); - var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException; + var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)); - Verify(exception is not null); Verify(exception.Outcome == UnitTestOutcome.Failed); Verify( exception.Message diff --git a/test/UnitTests/MSTestAdapter.UnitTests/Execution/TypeCacheTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/Execution/TypeCacheTests.cs index e00fd6337b..ffbc102efc 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/Execution/TypeCacheTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/Execution/TypeCacheTests.cs @@ -59,22 +59,15 @@ protected override void Dispose(bool disposing) public void GetTestMethodInfoShouldThrowIfTestMethodIsNull() { var testMethod = new TestMethod("M", "C", "A", isAsync: false); - void A() => _typeCache.GetTestMethodInfo( - null, - new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), - false); - Exception ex = VerifyThrows(A); - Verify(ex.GetType() == typeof(ArgumentNullException)); + var context = new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()); + VerifyThrows(() => _typeCache.GetTestMethodInfo(null, context, false)); } public void GetTestMethodInfoShouldThrowIfTestContextIsNull() { var testMethod = new TestMethod("M", "C", "A", isAsync: false); - void A() => _typeCache.GetTestMethodInfo(testMethod, null, false); - - Exception ex = VerifyThrows(A); - Verify(ex.GetType() == typeof(ArgumentNullException)); + VerifyThrows(() => _typeCache.GetTestMethodInfo(testMethod, null, false)); } public void GetTestMethodInfoShouldReturnNullIfClassInfoForTheMethodIsNull() @@ -112,10 +105,8 @@ void Action() => new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(Action); + TypeInspectionException exception = VerifyThrows(Action); - Verify(exception is not null); - Verify(exception is TypeInspectionException); Verify(exception.Message.StartsWith("Unable to get type C. Error: System.Exception: Load failure", StringComparison.Ordinal)); } @@ -130,10 +121,7 @@ void Action() => new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(Action); - - Verify(exception is not null); - Verify(exception is TypeInspectionException); + TypeInspectionException exception = VerifyThrows(Action); Verify(exception.Message.StartsWith("Cannot find a valid constructor for test class 'Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TypeCacheTests+DummyTestClassWithNoDefaultConstructor'. Valid constructors are 'public' and either parameterless or with one parameter of type 'TestContext'.", StringComparison.Ordinal)); } @@ -148,10 +136,7 @@ void Action() => new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(Action); - - Verify(exception is not null); - Verify(exception is TypeInspectionException); + TypeInspectionException exception = VerifyThrows(Action); Verify(exception.Message.StartsWith($"The {className}.TestContext has incorrect type.", StringComparison.Ordinal)); } @@ -166,10 +151,7 @@ void Action() => new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(Action); - - Verify(exception is not null); - Verify(exception is TypeInspectionException); + TypeInspectionException exception = VerifyThrows(Action); Verify(exception.Message.StartsWith(string.Format(CultureInfo.InvariantCulture, "Unable to find property {0}.TestContext. Error:{1}.", className, "Ambiguous match found."), StringComparison.Ordinal)); } @@ -328,9 +310,7 @@ void A() => new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(A); - Verify(exception is not null); - Verify(exception is TypeInspectionException); + Exception exception = VerifyThrows(A); MethodInfo methodInfo = type.GetMethod("AssemblyInit"); string expectedMessage = @@ -360,9 +340,7 @@ void A() => new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(A); - Verify(exception is not null); - Verify(exception is TypeInspectionException); + Exception exception = VerifyThrows(A); MethodInfo methodInfo = type.GetMethod("AssemblyCleanup"); string expectedMessage = @@ -659,9 +637,7 @@ void A() => new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(A); - Verify(exception is not null); - Verify(exception is TypeInspectionException); + Exception exception = VerifyThrows(A); MethodInfo methodInfo = type.GetMethod("AssemblyInit"); string expectedMessage = @@ -691,9 +667,7 @@ void A() => new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(A); - Verify(exception is not null); - Verify(exception is TypeInspectionException); + Exception exception = VerifyThrows(A); MethodInfo methodInfo = type.GetMethod("AssemblyCleanup"); string expectedMessage = @@ -763,10 +737,7 @@ void A() => new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(A); - - Verify(exception is not null); - Verify(exception is TypeInspectionException); + TypeInspectionException exception = VerifyThrows(A); MethodInfo methodInfo = type.GetMethod("TestInit"); string expectedMessage = @@ -860,10 +831,7 @@ void A() => new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(A); - - Verify(exception is not null); - Verify(exception is TypeInspectionException); + Exception exception = VerifyThrows(A); string expectedMessage = string.Format( CultureInfo.InvariantCulture, @@ -926,10 +894,7 @@ void A() => _typeCache.GetTestMethodInfo( new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(A); - - Verify(exception is not null); - Verify(exception is TypeInspectionException); + Exception exception = VerifyThrows(A); string expectedMessage = string.Format( @@ -955,10 +920,7 @@ void A() => _typeCache.GetTestMethodInfo( new TestContextImplementation(testMethod, new ThreadSafeStringWriter(null, "test"), new Dictionary()), false); - Exception exception = VerifyThrows(A); - - Verify(exception is not null); - Verify(exception is TypeInspectionException); + TypeInspectionException exception = VerifyThrows(A); string expectedMessage = string.Format( diff --git a/test/UnitTests/MSTestAdapter.UnitTests/Execution/UnitTestRunnerTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/Execution/UnitTestRunnerTests.cs index 52df6e4f64..186ecb58de 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/Execution/UnitTestRunnerTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/Execution/UnitTestRunnerTests.cs @@ -84,19 +84,13 @@ public void ConstructorShouldPopulateSettings() #region RunSingleTest tests - public void RunSingleTestShouldThrowIfTestMethodIsNull() - { - void A() => _unitTestRunner.RunSingleTest(null, null); - Exception ex = VerifyThrows(A); - Verify(ex.GetType() == typeof(ArgumentNullException)); - } + public void RunSingleTestShouldThrowIfTestMethodIsNull() => + VerifyThrows(() => _unitTestRunner.RunSingleTest(null, null)); public void RunSingleTestShouldThrowIfTestRunParametersIsNull() { var testMethod = new TestMethod("M", "C", "A", isAsync: false); - void A() => _unitTestRunner.RunSingleTest(testMethod, null); - Exception ex = VerifyThrows(A); - Verify(ex.GetType() == typeof(ArgumentNullException)); + VerifyThrows(() => _unitTestRunner.RunSingleTest(testMethod, null)); } public void RunSingleTestShouldReturnTestResultIndicateATestNotFoundIfTestMethodCannotBeFound() diff --git a/test/UnitTests/MSTestAdapter.UnitTests/Extensions/ExceptionExtensionsTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/Extensions/ExceptionExtensionsTests.cs index 45d2b9a215..79391029ec 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/Extensions/ExceptionExtensionsTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/Extensions/ExceptionExtensionsTests.cs @@ -47,8 +47,7 @@ public void ExceptionTryGetMessageShouldThrowIfExceptionMessageThrows() { var exception = new DummyException(() => throw new NotImplementedException()); - Exception ex = VerifyThrows(() => exception.TryGetMessage()); - Verify(ex is NotImplementedException); + VerifyThrows(() => exception.TryGetMessage()); } #endregion @@ -77,8 +76,7 @@ public void TryGetStackTraceInformationShouldThrowIfStackTraceThrows() { var exception = new DummyExceptionForStackTrace(() => throw new NotImplementedException()); - Exception ex = VerifyThrows(() => exception.TryGetStackTraceInformation()); - Verify(ex is NotImplementedException); + VerifyThrows(() => exception.TryGetStackTraceInformation()); } #pragma warning disable CA1710 // Identifiers should have correct suffix diff --git a/test/UnitTests/MSTestAdapter.UnitTests/Helpers/RunSettingsUtilitiesTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/Helpers/RunSettingsUtilitiesTests.cs index 7b5acec0df..f01999451e 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/Helpers/RunSettingsUtilitiesTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/Helpers/RunSettingsUtilitiesTests.cs @@ -130,8 +130,7 @@ public void GetTestRunParametersThrowsWhenTRPNodeHasAttributes() """; - Exception ex = VerifyThrows(() => RunSettingsUtilities.GetTestRunParameters(settingsXml)); - Verify(ex.GetType() == typeof(SettingsException)); + VerifyThrows(() => RunSettingsUtilities.GetTestRunParameters(settingsXml)); } public void GetTestRunParametersThrowsWhenTRPNodeHasNonParameterTypeChildNodes() @@ -152,8 +151,7 @@ public void GetTestRunParametersThrowsWhenTRPNodeHasNonParameterTypeChildNodes() """; - Exception ex = VerifyThrows(() => RunSettingsUtilities.GetTestRunParameters(settingsXml)); - Verify(ex.GetType() == typeof(SettingsException)); + VerifyThrows(() => RunSettingsUtilities.GetTestRunParameters(settingsXml)); } public void GetTestRunParametersIgnoresMalformedKeyValues() diff --git a/test/UnitTests/MSTestAdapter.UnitTests/MSTestDiscovererTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/MSTestDiscovererTests.cs index 4364e32cb7..de5c7be8bc 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/MSTestDiscovererTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/MSTestDiscovererTests.cs @@ -87,22 +87,19 @@ public void MSTestDiscovererHasExeAsFileExtension() public void DiscoverTestsShouldThrowIfSourcesIsNull() { void A() => _discoverer.DiscoverTests(null, _mockDiscoveryContext.Object, _mockMessageLogger.Object, _mockTestCaseDiscoverySink.Object); - Exception ex = VerifyThrows(A); - Verify(ex.GetType() == typeof(ArgumentNullException)); + VerifyThrows(A); } public void DiscoverTestsShouldThrowIfDiscoverySinkIsNull() { void A() => _discoverer.DiscoverTests(new List(), _mockDiscoveryContext.Object, _mockMessageLogger.Object, null); - Exception ex = VerifyThrows(A); - Verify(ex.GetType() == typeof(ArgumentNullException)); + VerifyThrows(A); } public void DiscoverTestsShouldThrowIfLoggerIsNull() { void A() => _discoverer.DiscoverTests(new List(), _mockDiscoveryContext.Object, null, _mockTestCaseDiscoverySink.Object); - Exception ex = VerifyThrows(A); - Verify(ex.GetType() == typeof(ArgumentNullException)); + VerifyThrows(A); } public void DiscoverTestsShouldThrowIfSourcesAreNotValid() @@ -112,8 +109,7 @@ public void DiscoverTestsShouldThrowIfSourcesAreNotValid() .Returns(new List { }); void A() => _discoverer.DiscoverTests(new List(), _mockDiscoveryContext.Object, _mockMessageLogger.Object, _mockTestCaseDiscoverySink.Object); - Exception ex = VerifyThrows(A); - Verify(ex.GetType() == typeof(NotSupportedException)); + VerifyThrows(A); } public void DiscoverTestsShouldNotThrowIfDiscoveryContextIsNull() @@ -208,10 +204,8 @@ public void DiscoveryShouldReportAndBailOutOnSettingsException() public void AreValidSourcesShouldThrowIfPlatformsValidSourceExtensionsIsNull() { _testablePlatformServiceProvider.MockTestSourceValidator.SetupGet(ts => ts.ValidSourceExtensions).Returns((List)null); - Exception ex = VerifyThrows(A); - Verify(ex.GetType() == typeof(ArgumentNullException)); - - static void A() => MSTestDiscovererHelpers.AreValidSources(new List { "dummy" }); + var sources = new List { "dummy" }; + VerifyThrows(() => MSTestDiscovererHelpers.AreValidSources(sources)); } public void AreValidSourcesShouldReturnFalseIfValidSourceExtensionsIsEmpty() diff --git a/test/UnitTests/MSTestAdapter.UnitTests/MSTestSettingsTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/MSTestSettingsTests.cs index 3ff9ec1c52..1916738314 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/MSTestSettingsTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/MSTestSettingsTests.cs @@ -403,10 +403,8 @@ public void GetSettingsShouldThrowIfParallelizationWorkersIsNotInt() """; - Exception exception = VerifyThrows(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias, _mockMessageLogger.Object)); + AdapterSettingsException exception = VerifyThrows(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias, _mockMessageLogger.Object)); - Verify(exception is not null); - Verify(typeof(AdapterSettingsException).FullName == exception.GetType().FullName); Verify(exception.Message.Contains("Invalid value 'GoneFishing' specified for 'Workers'. The value should be a non-negative integer.")); } @@ -423,10 +421,7 @@ public void GetSettingsShouldThrowIfParallelizationWorkersIsNegative() """; - Exception exception = VerifyThrows(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias, _mockMessageLogger.Object)); - - Verify(exception is not null); - Verify(typeof(AdapterSettingsException).FullName == exception.GetType().FullName); + AdapterSettingsException exception = VerifyThrows(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias, _mockMessageLogger.Object)); Verify(exception.Message.Contains("Invalid value '-1' specified for 'Workers'. The value should be a non-negative integer.")); } @@ -534,10 +529,7 @@ public void GetSettingsShouldThrowIfParallelizationScopeIsNotValid() """; - Exception exception = VerifyThrows(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias, _mockMessageLogger.Object)); - - Verify(exception is not null); - Verify(typeof(AdapterSettingsException).FullName == exception.GetType().FullName); + AdapterSettingsException exception = VerifyThrows(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias, _mockMessageLogger.Object)); Verify(exception.Message.Contains("Invalid value 'JustParallelizeWillYou' specified for 'Scope'. Supported scopes are ClassLevel, MethodLevel.")); } @@ -572,10 +564,7 @@ public void GetSettingsShouldThrowWhenParallelizeHasInvalidElements() """; - Exception exception = VerifyThrows(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias, _mockMessageLogger.Object)); - - Verify(exception is not null); - Verify(typeof(AdapterSettingsException).FullName == exception.GetType().FullName); + AdapterSettingsException exception = VerifyThrows(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias, _mockMessageLogger.Object)); Verify(exception.Message.Contains("Invalid settings 'Parallelize'. Unexpected XmlElement: 'Hola'.")); } diff --git a/test/UnitTests/MSTestAdapter.UnitTests/ObjectModel/UnitTestElementTests.cs b/test/UnitTests/MSTestAdapter.UnitTests/ObjectModel/UnitTestElementTests.cs index d799d6d46c..2d99951a67 100644 --- a/test/UnitTests/MSTestAdapter.UnitTests/ObjectModel/UnitTestElementTests.cs +++ b/test/UnitTests/MSTestAdapter.UnitTests/ObjectModel/UnitTestElementTests.cs @@ -24,11 +24,8 @@ public UnitTestElementTests() #region Ctor tests - public void UnitTestElementConstructorShouldThrowIfTestMethodIsNull() - { - Exception ex = VerifyThrows(() => _ = new UnitTestElement(null)); - Verify(ex.GetType() == typeof(ArgumentNullException)); - } + public void UnitTestElementConstructorShouldThrowIfTestMethodIsNull() => + VerifyThrows(() => _ = new UnitTestElement(null)); #endregion diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.AreEqualTests.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.AreEqualTests.cs index ec2a9fe83f..1f2be37f6d 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.AreEqualTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.AreEqualTests.cs @@ -14,12 +14,8 @@ namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests; public partial class AssertTests : TestContainer { - public void AreNotEqualShouldFailWhenNotEqualType() - { - static void Action() => Assert.AreNotEqual(1, 1); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualType() => + VerifyThrows(() => Assert.AreNotEqual(1, 1)); public void AreNotEqualShouldFailWhenNotEqualTypeWithMessage() { @@ -27,12 +23,8 @@ public void AreNotEqualShouldFailWhenNotEqualTypeWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreNotEqualShouldFailWhenNotEqualString() - { - static void Action() => Assert.AreNotEqual("A", "A"); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualString() => + VerifyThrows(() => Assert.AreNotEqual("A", "A")); public void AreNotEqualShouldFailWhenNotEqualStringWithMessage() { @@ -41,19 +33,11 @@ public void AreNotEqualShouldFailWhenNotEqualStringWithMessage() } [SuppressMessage("Globalization", "CA1304:Specify CultureInfo", Justification = "Testing the API without the culture")] - public void AreNotEqualShouldFailWhenNotEqualStringAndCaseIgnored() - { - static void Action() => Assert.AreNotEqual("A", "a", true); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualStringAndCaseIgnored() => + VerifyThrows(() => Assert.AreNotEqual("A", "a", true)); - public void AreNotEqualShouldFailWhenNotEqualInt() - { - static void Action() => Assert.AreNotEqual(1, 1); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualInt() => + VerifyThrows(() => Assert.AreNotEqual(1, 1)); public void AreNotEqualShouldFailWhenNotEqualIntWithMessage() { @@ -61,12 +45,8 @@ public void AreNotEqualShouldFailWhenNotEqualIntWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreNotEqualShouldFailWhenNotEqualLong() - { - static void Action() => Assert.AreNotEqual(1L, 1L); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualLong() => + VerifyThrows(() => Assert.AreNotEqual(1L, 1L)); public void AreNotEqualShouldFailWhenNotEqualLongWithMessage() { @@ -74,19 +54,11 @@ public void AreNotEqualShouldFailWhenNotEqualLongWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreNotEqualShouldFailWhenNotEqualLongWithDelta() - { - static void Action() => Assert.AreNotEqual(1L, 2L, 1L); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualLongWithDelta() => + VerifyThrows(() => Assert.AreNotEqual(1L, 2L, 1L)); - public void AreNotEqualShouldFailWhenNotEqualDecimal() - { - static void Action() => Assert.AreNotEqual(0.1M, 0.1M); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualDecimal() => + VerifyThrows(() => Assert.AreNotEqual(0.1M, 0.1M)); public void AreNotEqualShouldFailWhenNotEqualDecimalWithMessage() { @@ -94,19 +66,11 @@ public void AreNotEqualShouldFailWhenNotEqualDecimalWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreNotEqualShouldFailWhenNotEqualDecimalWithDelta() - { - static void Action() => Assert.AreNotEqual(0.1M, 0.2M, 0.1M); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualDecimalWithDelta() => + VerifyThrows(() => Assert.AreNotEqual(0.1M, 0.2M, 0.1M)); - public void AreNotEqualShouldFailWhenNotEqualDouble() - { - static void Action() => Assert.AreNotEqual(0.1, 0.1); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualDouble() => + VerifyThrows(() => Assert.AreNotEqual(0.1, 0.1)); public void AreNotEqualShouldFailWhenNotEqualDoubleWithMessage() { @@ -114,19 +78,11 @@ public void AreNotEqualShouldFailWhenNotEqualDoubleWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreNotEqualShouldFailWhenNotEqualDoubleWithDelta() - { - static void Action() => Assert.AreNotEqual(0.1, 0.2, 0.1); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualDoubleWithDelta() => + VerifyThrows(() => Assert.AreNotEqual(0.1, 0.2, 0.1)); - public void AreNotEqualShouldFailWhenFloatDouble() - { - static void Action() => Assert.AreNotEqual(100E-2, 100E-2); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenFloatDouble() => + VerifyThrows(() => Assert.AreNotEqual(100E-2, 100E-2)); public void AreNotEqualShouldFailWhenFloatDoubleWithMessage() { @@ -134,19 +90,11 @@ public void AreNotEqualShouldFailWhenFloatDoubleWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreNotEqualShouldFailWhenNotEqualFloatWithDelta() - { - static void Action() => Assert.AreNotEqual(100E-2, 200E-2, 100E-2); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreNotEqualShouldFailWhenNotEqualFloatWithDelta() => + VerifyThrows(() => Assert.AreNotEqual(100E-2, 200E-2, 100E-2)); - public void AreEqualShouldFailWhenNotEqualType() - { - static void Action() => Assert.AreEqual(null, "string"); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualShouldFailWhenNotEqualType() => + VerifyThrows(() => Assert.AreEqual(null, "string")); public void AreEqualShouldFailWhenNotEqualTypeWithMessage() { @@ -201,19 +149,11 @@ public void AreEqualShouldFailWhenNotEqualStringWithMessage() } [SuppressMessage("Globalization", "CA1304:Specify CultureInfo", Justification = "Testing the API without the culture")] - public void AreEqualShouldFailWhenNotEqualStringAndCaseIgnored() - { - static void Action() => Assert.AreEqual("A", "a", false); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualShouldFailWhenNotEqualStringAndCaseIgnored() => + VerifyThrows(() => Assert.AreEqual("A", "a", false)); - public void AreEqualShouldFailWhenNotEqualInt() - { - static void Action() => Assert.AreEqual(1, 2); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualShouldFailWhenNotEqualInt() => + VerifyThrows(() => Assert.AreEqual(1, 2)); public void AreEqualShouldFailWhenNotEqualIntWithMessage() { @@ -221,12 +161,8 @@ public void AreEqualShouldFailWhenNotEqualIntWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreEqualShouldFailWhenNotEqualLong() - { - static void Action() => Assert.AreEqual(1L, 2L); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualShouldFailWhenNotEqualLong() => + VerifyThrows(() => Assert.AreEqual(1L, 2L)); public void AreEqualShouldFailWhenNotEqualLongWithMessage() { @@ -234,19 +170,11 @@ public void AreEqualShouldFailWhenNotEqualLongWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreEqualShouldFailWhenNotEqualLongWithDelta() - { - static void Action() => Assert.AreEqual(10L, 20L, 5L); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualShouldFailWhenNotEqualLongWithDelta() => + VerifyThrows(() => Assert.AreEqual(10L, 20L, 5L)); - public void AreEqualShouldFailWhenNotEqualDouble() - { - static void Action() => Assert.AreEqual(0.1, 0.2); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualShouldFailWhenNotEqualDouble() => + VerifyThrows(() => Assert.AreEqual(0.1, 0.2)); public void AreEqualShouldFailWhenNotEqualDoubleWithMessage() { @@ -254,18 +182,13 @@ public void AreEqualShouldFailWhenNotEqualDoubleWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreEqualShouldFailWhenNotEqualDoubleWithDelta() - { - static void Action() => Assert.AreEqual(0.1, 0.2, 0.05); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualShouldFailWhenNotEqualDoubleWithDelta() => + VerifyThrows(() => Assert.AreEqual(0.1, 0.2, 0.05)); public void AreEqualShouldFailWhenNotEqualDecimal() { static void Action() => Assert.AreEqual(0.1M, 0.2M); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); + VerifyThrows(Action); } public void AreEqualShouldFailWhenNotEqualDecimalWithMessage() @@ -274,19 +197,11 @@ public void AreEqualShouldFailWhenNotEqualDecimalWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreEqualShouldFailWhenNotEqualDecimalWithDelta() - { - static void Action() => Assert.AreEqual(0.1M, 0.2M, 0.05M); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualShouldFailWhenNotEqualDecimalWithDelta() => + VerifyThrows(() => Assert.AreEqual(0.1M, 0.2M, 0.05M)); - public void AreEqualShouldFailWhenFloatDouble() - { - static void Action() => Assert.AreEqual(100E-2, 200E-2); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualShouldFailWhenFloatDouble() => + VerifyThrows(() => Assert.AreEqual(100E-2, 200E-2)); public void AreEqualShouldFailWhenFloatDoubleWithMessage() { @@ -294,25 +209,15 @@ public void AreEqualShouldFailWhenFloatDoubleWithMessage() Verify(ex.Message.Contains("A Message")); } - public void AreEqualShouldFailWhenNotEqualFloatWithDelta() - { - static void Action() => Assert.AreEqual(100E-2, 200E-2, 50E-2); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualShouldFailWhenNotEqualFloatWithDelta() => + VerifyThrows(() => Assert.AreEqual(100E-2, 200E-2, 50E-2)); - public void AreEqualTwoObjectsShouldFail() - { - static void Action() => Assert.AreEqual(new object(), new object()); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void AreEqualTwoObjectsShouldFail() => + VerifyThrows(() => Assert.AreEqual(new object(), new object())); public void AreEqualTwoObjectsDifferentTypeShouldFail() { - static void Action() => Assert.AreEqual(new object(), 1); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); + AssertFailedException ex = VerifyThrows(() => Assert.AreEqual(new object(), 1)); Verify(ex.Message.Contains("Assert.AreEqual failed. Expected:. Actual:<1 (System.Int32)>.")); } @@ -339,8 +244,7 @@ static void Action() Assert.AreEqual(a, b, new TypeOverridesEqualsEqualityComparer()); } - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); + VerifyThrows(Action); } public void AreEqualStringIgnoreCaseCultureInfoNullabilityPostConditions() diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.InconclusiveTests.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.InconclusiveTests.cs index bff1a73913..cde2af40ff 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.InconclusiveTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.InconclusiveTests.cs @@ -12,16 +12,12 @@ public partial class AssertTests // See https://github.com/dotnet/sdk/issues/25373 public void InconclusiveDoesNotThrowWhenMessageContainsInvalidStringFormatCompositeAndNoArgumentsPassed() { - Exception ex = VerifyThrows(() => Assert.Inconclusive("{")); - Verify(typeof(AssertInconclusiveException) == ex.GetType()); + Exception ex = VerifyThrows(() => Assert.Inconclusive("{")); Verify(ex.Message.Contains("Assert.Inconclusive failed. {")); } // See https://github.com/dotnet/sdk/issues/25373 [SuppressMessage("Usage", "CA2241:Provide correct arguments to formatting methods", Justification = "We want to test invalid format")] - public void InconclusiveThrowsWhenMessageContainsInvalidStringFormatComposite() - { - Exception ex = VerifyThrows(() => Assert.Inconclusive("{", "arg")); - Verify(ex is FormatException); - } + public void InconclusiveThrowsWhenMessageContainsInvalidStringFormatComposite() => + VerifyThrows(() => Assert.Inconclusive("{", "arg")); } diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.IsInstanceOfTypeTests.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.IsInstanceOfTypeTests.cs index e40fdc3f62..b532333130 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.IsInstanceOfTypeTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.IsInstanceOfTypeTests.cs @@ -12,19 +12,11 @@ namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests; [SuppressMessage("Usage", "CA2263:Prefer generic overload when type is known", Justification = "We want to test also the non-generic API")] public partial class AssertTests { - public void InstanceOfTypeShouldFailWhenValueIsNull() - { - static void Action() => Assert.IsInstanceOfType(null, typeof(AssertTests)); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void InstanceOfTypeShouldFailWhenValueIsNull() => + VerifyThrows(() => Assert.IsInstanceOfType(null, typeof(AssertTests))); - public void InstanceOfTypeShouldFailWhenTypeIsNull() - { - static void Action() => Assert.IsInstanceOfType(5, null); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void InstanceOfTypeShouldFailWhenTypeIsNull() => + VerifyThrows(() => Assert.IsInstanceOfType(5, null)); public void InstanceOfTypeShouldPassOnSameInstance() => Assert.IsInstanceOfType(5, typeof(int)); @@ -32,32 +24,22 @@ public void InstanceOfTypeShouldFailWhenTypeIsNull() public void InstanceNotOfTypeShouldFailWhenValueIsNull() => Assert.IsNotInstanceOfType(null, typeof(object)); - public void InstanceNotOfTypeShouldFailWhenTypeIsNull() - { - static void Action() => Assert.IsNotInstanceOfType(5, null); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void InstanceNotOfTypeShouldFailWhenTypeIsNull() => + VerifyThrows(() => Assert.IsNotInstanceOfType(5, null)); public void InstanceNotOfTypeShouldPassOnWrongInstance() => Assert.IsNotInstanceOfType(5L, typeof(int)); public void InstanceNotOfTypeShouldPassOnSubInstance() => Assert.IsNotInstanceOfType(new object(), typeof(int)); [TestMethod] - public void IsInstanceOfTypeUsingGenericType_WhenValueIsNull_Fails() - { - static void Action() => Assert.IsInstanceOfType(null); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); - } + public void IsInstanceOfTypeUsingGenericType_WhenValueIsNull_Fails() => + VerifyThrows(() => Assert.IsInstanceOfType(null)); [TestMethod] public void IsInstanceOfTypeUsingGenericTypeWithOutParameter_WhenValueIsNull_Fails() { AssertTests? assertTests = null; - void Action() => Assert.IsInstanceOfType(null, out assertTests); - Exception ex = VerifyThrows(Action); - Verify(ex is AssertFailedException); + VerifyThrows(() => Assert.IsInstanceOfType(null, out assertTests)); Verify(assertTests is null); } diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.ThrowsExceptionTests.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.ThrowsExceptionTests.cs index b81b31f95e..459c84c165 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.ThrowsExceptionTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.ThrowsExceptionTests.cs @@ -11,8 +11,7 @@ public partial class AssertTests // See https://github.com/dotnet/sdk/issues/25373 public void ThrowAssertFailedDoesNotThrowIfMessageContainsInvalidStringFormatComposite() { - Exception ex = VerifyThrows(() => Assert.ThrowAssertFailed("name", "{")); - Verify(typeof(AssertFailedException) == ex.GetType()); + Exception ex = VerifyThrows(() => Assert.ThrowAssertFailed("name", "{")); Verify(ex.Message.Contains("name failed. {")); } #endregion @@ -20,36 +19,31 @@ public void ThrowAssertFailedDoesNotThrowIfMessageContainsInvalidStringFormatCom #region ThrowsException tests public void ThrowsExceptionWithLambdaExpressionsShouldThrowAssertionOnNoException() { - Exception ex = VerifyThrows(() => Assert.ThrowsException(() => { })); - Verify(typeof(AssertFailedException) == ex.GetType()); + Exception ex = VerifyThrows(() => Assert.ThrowsException(() => { })); Verify(ex.Message.Equals("Assert.ThrowsException failed. Expected exception type: but no exception was thrown. ", StringComparison.Ordinal)); } public void ThrowsExceptionWithLambdaExpressionsShouldThrowAssertionOnWrongException() { - Exception ex = VerifyThrows(() => Assert.ThrowsException(() => throw new FormatException())); - Verify(typeof(AssertFailedException) == ex.GetType()); + Exception ex = VerifyThrows(() => Assert.ThrowsException(() => throw new FormatException())); Verify(ex.Message.Equals("Assert.ThrowsException failed. Expected exception type:. Actual exception type:. ", StringComparison.Ordinal)); } public void ThrowsException_FuncArgument_AllowsToReturnNull() { - Exception ex = VerifyThrows(() => Assert.ThrowsException(() => null)); - Verify(typeof(AssertFailedException) == ex.GetType()); + Exception ex = VerifyThrows(() => Assert.ThrowsException(() => null)); Verify(ex.Message.Equals("Assert.ThrowsException failed. Expected exception type: but no exception was thrown. ", StringComparison.Ordinal)); } public void ThrowsException_FuncArgumentOverloadWithMessage_AllowsToReturnNull() { - Exception ex = VerifyThrows(() => Assert.ThrowsException(() => null, "message")); - Verify(typeof(AssertFailedException) == ex.GetType()); + Exception ex = VerifyThrows(() => Assert.ThrowsException(() => null, "message")); Verify(ex.Message.Equals("Assert.ThrowsException failed. Expected exception type: but no exception was thrown. message", StringComparison.Ordinal)); } public void ThrowsException_FuncArgumentOverloadWithMessagesAndParameters_AllowsToReturnNull() { - Exception ex = VerifyThrows(() => Assert.ThrowsException(() => null, "message {0}", 1)); - Verify(typeof(AssertFailedException) == ex.GetType()); + Exception ex = VerifyThrows(() => Assert.ThrowsException(() => null, "message {0}", 1)); Verify(ex.Message.Equals("Assert.ThrowsException failed. Expected exception type: but no exception was thrown. message 1", StringComparison.Ordinal)); } #endregion diff --git a/test/UnitTests/TestFramework.UnitTests/Attributes/DynamicDataAttributeTests.cs b/test/UnitTests/TestFramework.UnitTests/Attributes/DynamicDataAttributeTests.cs index 1b76469f20..47944c7b1f 100644 --- a/test/UnitTests/TestFramework.UnitTests/Attributes/DynamicDataAttributeTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Attributes/DynamicDataAttributeTests.cs @@ -25,17 +25,12 @@ public DynamicDataAttributeTests() DynamicDataAttribute.TestIdGenerationStrategy = TestIdGenerationStrategy.FullyQualified; } - public void GetDataShouldThrowExceptionIfInvalidPropertyNameIsSpecifiedOrPropertyDoesNotExist() - { - void Action() + public void GetDataShouldThrowExceptionIfInvalidPropertyNameIsSpecifiedOrPropertyDoesNotExist() => + VerifyThrows(() => { _dynamicDataAttribute = new DynamicDataAttribute("ABC"); _dynamicDataAttribute.GetData(_testMethodInfo); - } - - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentNullException); - } + }); public void GetDataShouldReadDataFromProperty() { @@ -73,44 +68,29 @@ public void GetDataShouldReadDataFromMethodInDifferentClass() Verify(data.ToList().Count == 2); } - public void GetDataShouldThrowExceptionIfPropertyReturnsNull() - { - void Action() + public void GetDataShouldThrowExceptionIfPropertyReturnsNull() => + VerifyThrows(() => { MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod4"); _dynamicDataAttribute = new DynamicDataAttribute("NullProperty", typeof(DummyTestClass)); _dynamicDataAttribute.GetData(methodInfo); - } + }); - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentNullException); - } - - public void GetDataShouldThrowExceptionIfPropertyReturnsEmpty() - { - void Action() + public void GetDataShouldThrowExceptionIfPropertyReturnsEmpty() => + VerifyThrows(() => { MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod5"); _dynamicDataAttribute = new DynamicDataAttribute("EmptyProperty", typeof(DummyTestClass)); _dynamicDataAttribute.GetData(methodInfo); - } - - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentException); - } + }); - public void GetDataShouldThrowExceptionIfPropertyDoesNotReturnCorrectType() - { - void Action() + public void GetDataShouldThrowExceptionIfPropertyDoesNotReturnCorrectType() => + VerifyThrows(() => { MethodInfo methodInfo = _dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod3"); _dynamicDataAttribute = new DynamicDataAttribute("WrongDataTypeProperty", typeof(DummyTestClass)); _dynamicDataAttribute.GetData(methodInfo); - } - - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentNullException); - } + }); public void GetDisplayNameShouldReturnDisplayName() { @@ -139,103 +119,68 @@ public void GetDisplayNameShouldReturnDisplayNameWithDynamicDataDisplayNameInDif Verify(displayName == "DynamicDataTestWithDisplayName TestMethod1 with 3 parameters"); } - public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodMissingParameters() - { - void Action() + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodMissingParameters() => + VerifyThrows(() => { object[] data = [1, 2, 3]; _dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithMissingParameters"; _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data); - } - - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentNullException); - } + }); - public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidReturnType() - { - void Action() + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidReturnType() => + VerifyThrows(() => { object[] data = [1, 2, 3]; _dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithInvalidReturnType"; _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data); - } + }); - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentNullException); - } - - public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidFirstParameterType() - { - void Action() + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidFirstParameterType() => + VerifyThrows(() => { object[] data = [1, 2, 3]; _dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithInvalidFirstParameterType"; _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data); - } - - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentNullException); - } + }); - public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidSecondParameterType() - { - void Action() + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidSecondParameterType() => + VerifyThrows(() => { object[] data = [1, 2, 3]; _dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithInvalidSecondParameterType"; _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data); - } + }); - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentNullException); - } - - public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodNonStatic() - { - void Action() + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodNonStatic() => + VerifyThrows(() => { object[] data = [1, 2, 3]; _dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameNonStatic"; _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data); - } - - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentNullException); - } + }); - public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodPrivate() - { - void Action() + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodPrivate() => + VerifyThrows(() => { object[] data = [1, 2, 3]; _dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNamePrivate"; _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data); - } + }); - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentNullException); - } - - public void GetDisplayNameShouldThrowExceptionWithMissingDynamicDataDisplayNameMethod() - { - void Action() + public void GetDisplayNameShouldThrowExceptionWithMissingDynamicDataDisplayNameMethod() => + VerifyThrows(() => { object[] data = [1, 2, 3]; _dynamicDataAttribute.DynamicDataDisplayName = "MissingCustomDynamicDataDisplayName"; _dynamicDataAttribute.GetDisplayName(_testMethodInfo, data); - } - - Exception ex = VerifyThrows(Action); - Verify(ex is ArgumentNullException); - } + }); public void GetDisplayNameShouldReturnEmptyStringIfDataIsNull() { @@ -325,36 +270,30 @@ public void DynamicDataSource_WithTuple_Throws() MethodInfo testMethodInfo = new TestClassTupleData().GetType().GetTypeInfo().GetDeclaredMethod(nameof(TestClassTupleData.DynamicDataTestWithTuple)); var dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.DataWithTuple), typeof(TestClassTupleData), DynamicDataSourceType.Property); - Exception ex = VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); - Verify(ex is ArgumentNullException); + VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.GetDataWithTuple), typeof(TestClassTupleData), DynamicDataSourceType.Method); - ex = VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); - Verify(ex is ArgumentNullException); + VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); } public void DynamicDataSource_WithValueTuple_Throws() { MethodInfo testMethodInfo = new TestClassTupleData().GetType().GetTypeInfo().GetDeclaredMethod(nameof(TestClassTupleData.DynamicDataTestWithTuple)); var dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.DataWithValueTuple), typeof(TestClassTupleData), DynamicDataSourceType.Property); - Exception ex = VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); - Verify(ex is ArgumentNullException); + VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.GetDataWithValueTuple), typeof(TestClassTupleData), DynamicDataSourceType.Method); - ex = VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); - Verify(ex is ArgumentNullException); + VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); } public void DynamicDataSource_WithValueTupleWithTupleSyntax_Throws() { MethodInfo testMethodInfo = new TestClassTupleData().GetType().GetTypeInfo().GetDeclaredMethod(nameof(TestClassTupleData.DynamicDataTestWithTuple)); var dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.DataWithValueTupleWithTupleSyntax), typeof(TestClassTupleData), DynamicDataSourceType.Property); - Exception ex = VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); - Verify(ex is ArgumentNullException); + VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); dynamicDataAttribute = new DynamicDataAttribute(nameof(TestClassTupleData.GetDataWithValueTupleWithTupleSyntax), typeof(TestClassTupleData), DynamicDataSourceType.Method); - ex = VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); - Verify(ex is ArgumentNullException); + VerifyThrows(() => dynamicDataAttribute.GetData(testMethodInfo)); } #endif } diff --git a/test/UnitTests/TestFramework.UnitTests/Attributes/ExpectedExceptionAttributeTests.cs b/test/UnitTests/TestFramework.UnitTests/Attributes/ExpectedExceptionAttributeTests.cs index b8ba779278..5e48e056b3 100644 --- a/test/UnitTests/TestFramework.UnitTests/Attributes/ExpectedExceptionAttributeTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Attributes/ExpectedExceptionAttributeTests.cs @@ -26,13 +26,8 @@ public void ExpectedExceptionAttributeConstructerShouldThrowArgumentNullExceptio /// /// ExpectedExceptionAttribute constructor should throw ArgumentNullException when parameter exceptionType = typeof(AnyClassNotDerivedFromExceptionClass). /// - public void ExpectedExceptionAttributeConstructerShouldThrowArgumentException() - { - static void A() => _ = new ExpectedExceptionAttribute(typeof(ExpectedExceptionAttributeTests), "Dummy"); - - Exception ex = VerifyThrows(A); - Verify(ex is ArgumentException); - } + public void ExpectedExceptionAttributeConstructerShouldThrowArgumentException() => + VerifyThrows(() => _ = new ExpectedExceptionAttribute(typeof(ExpectedExceptionAttributeTests), "Dummy")); /// /// ExpectedExceptionAttribute constructor should not throw exception when parameter exceptionType = typeof(AnyClassDerivedFromExceptionClass). diff --git a/test/UnitTests/TestFramework.UnitTests/Attributes/ExpectedExceptionBaseAttributeTests.cs b/test/UnitTests/TestFramework.UnitTests/Attributes/ExpectedExceptionBaseAttributeTests.cs index fdf45b12b5..1811f80edd 100644 --- a/test/UnitTests/TestFramework.UnitTests/Attributes/ExpectedExceptionBaseAttributeTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Attributes/ExpectedExceptionBaseAttributeTests.cs @@ -30,13 +30,8 @@ public void RethrowIfAssertExceptionThrowsExceptionOnAssertFailure() /// /// RethrowIfAssertException function will throw AssertFailedException if we pass AssertInconclusiveException as parameter in it. /// - public void RethrowIfAssertExceptionThrowsExceptionOnAssertInconclusive() - { - void A() => _sut.RethrowIfAssertException(new AssertInconclusiveException()); - - Exception ex = VerifyThrows(A); - Verify(ex is AssertInconclusiveException); - } + public void RethrowIfAssertExceptionThrowsExceptionOnAssertInconclusive() => + VerifyThrows(() => _sut.RethrowIfAssertException(new AssertInconclusiveException())); public void VerifyCorrectMessageIsGettingSetInVariablenoExceptionMessage() { diff --git a/test/UnitTests/TestFramework.UnitTests/LoggerTests.cs b/test/UnitTests/TestFramework.UnitTests/LoggerTests.cs index 8e911c5e6d..683d1ca53d 100644 --- a/test/UnitTests/TestFramework.UnitTests/LoggerTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/LoggerTests.cs @@ -13,15 +13,15 @@ public sealed class LoggerTests : TestContainer public void LogMessageWhenFormatIsNullShouldThrow() { Logger.OnLogMessage += message => { }; - Exception ex = VerifyThrows(() => Logger.LogMessage(null!, "arg1")); - Verify(ex is ArgumentNullException && ex.Message.Contains("format")); + ArgumentNullException ex = VerifyThrows(() => Logger.LogMessage(null!, "arg1")); + Verify(ex.Message.Contains("format")); } public void LogMessageWhenArgsIsNullShouldThrow() { Logger.OnLogMessage += message => { }; - Exception ex = VerifyThrows(() => Logger.LogMessage("foo", null!)); - Verify(ex is ArgumentNullException && ex.Message.Contains("args")); + ArgumentNullException ex = VerifyThrows(() => Logger.LogMessage("foo", null!)); + Verify(ex.Message.Contains("args")); } public void LogMessageWhenFormatIsSimpleMessageAndNoArgsShouldCallEvent() diff --git a/test/Utilities/TestFramework.ForTestingMSTest/TestContainer.cs b/test/Utilities/TestFramework.ForTestingMSTest/TestContainer.cs index 7cf3f24cff..c242c4a119 100644 --- a/test/Utilities/TestFramework.ForTestingMSTest/TestContainer.cs +++ b/test/Utilities/TestFramework.ForTestingMSTest/TestContainer.cs @@ -83,6 +83,28 @@ public static Exception VerifyThrows( return null; } + public static T VerifyThrows( + Action action, + [CallerArgumentExpression(nameof(action))] + string? expression = default, + [CallerMemberName] string? caller = default, + [CallerFilePath] string? filePath = default, + [CallerLineNumber] int lineNumber = default) + where T : Exception + { + try + { + action(); + } + catch (T ex) + { + return ex; + } + + Throw(expression, caller, filePath, lineNumber); + return null; + } + public static void Fail( [CallerMemberName] string? caller = default, [CallerFilePath] string? filePath = default,