/// <summary> /// Resolves the expected exception attribute. The function will try to /// get all the expected exception attributes defined for a testMethod. /// </summary> /// <param name="methodInfo">The MethodInfo instance.</param> /// <param name="testMethod">The test method.</param> /// <returns> /// The expected exception attribute found for this test. Null if not found. /// </returns> public virtual ExpectedExceptionBaseAttribute ResolveExpectedExceptionHelper(MethodInfo methodInfo, TestMethod testMethod) { Debug.Assert(methodInfo != null, "MethodInfo should be non-null"); // Get the expected exception attribute ExpectedExceptionBaseAttribute[] expectedExceptions; try { expectedExceptions = GetCustomAttributes(methodInfo, typeof(ExpectedExceptionBaseAttribute), true).OfType <ExpectedExceptionBaseAttribute>().ToArray(); } catch (Exception ex) { // If construction of the attribute throws an exception, indicate that there was an // error when trying to run the test string errorMessage = string.Format( CultureInfo.CurrentCulture, Resource.UTA_ExpectedExceptionAttributeConstructionException, testMethod.FullClassName, testMethod.Name, StackTraceHelper.GetExceptionMessage(ex)); throw new TypeInspectionException(errorMessage); } if (expectedExceptions == null || expectedExceptions.Length == 0) { return(null); } // Verify that there is only one attribute (multiple attributes derived from // ExpectedExceptionBaseAttribute are not allowed on a test method) if (expectedExceptions.Length > 1) { string errorMessage = string.Format( CultureInfo.CurrentCulture, Resource.UTA_MultipleExpectedExceptionsOnTestMethod, testMethod.FullClassName, testMethod.Name); throw new TypeInspectionException(errorMessage); } // Set the expected exception attribute to use for this test ExpectedExceptionBaseAttribute expectedException = expectedExceptions[0]; return(expectedException); }
internal TestMethodInfo( MethodInfo testMethod, int timeout, TestMethodAttribute executor, ExpectedExceptionBaseAttribute expectedException, TestClassInfo parent, ITestContext testContext) { Debug.Assert(testMethod != null, "TestMethod should not be null"); Debug.Assert(parent != null, "Parent should not be null"); this.TestMethod = testMethod; this.Timeout = timeout; this.Parent = parent; this.ExpectedException = expectedException; this.Executor = executor; this.testContext = testContext; }
public static void Verify(this ExpectedExceptionBaseAttribute attr, Exception realException) { var method = typeof(ExpectedExceptionBaseAttribute).GetMethod("Verify", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); method.Invoke(attr, new object[] { realException }); }