private void Initialize(MethodInfo method) { NUnit.Framework.ExpectedExceptionAttribute attribute = Reflect.GetExpectedExceptionAttribute(method); if (attribute == null) { throw new InvalidTestFixtureException("ExpectedExceptionAttribute not found"); } this.expectedException = attribute.ExceptionType; this.expectedMessage = attribute.ExpectedMessage; }
private void TheTestFailedByNotExpectedException(MethodInfo testMethod, NUnit.Framework.ExpectedExceptionAttribute[] expExAttrs) { m_countFailedByAssert++; // String message = String.Empty; //message += "{" + expExAttrs.Length.ToString() + "} "; NUnit.Framework.ExpectedExceptionAttribute attribute = expExAttrs[0]; if (attribute.ExceptionName != null) { message += attribute.ExceptionName; } else { message += attribute.ExceptionType.Name; } message += " was expected."; // AddProgressDot("F"); TheTestFailed__(testMethod, message); }
//---------------------------------------------------------------------- void InternalDoTestsOnClass(Type testFixtureType, StringBuilder timings) { Object instance = Activator.CreateInstance(testFixtureType); // foreach method with attribute [Test], invoke, // handling exception by checking if they're in a [ExcpectedException] attribute // MethodInfo[] methods = testFixtureType.GetMethods(); //---------- // [TestFixtureSetUpAttribute] foreach (MethodInfo curMethod in methods) { bool defined = curMethod.IsDefined(typeof(NUnit.Framework.TestFixtureSetUpAttribute), false); object[] attributes = curMethod.GetCustomAttributes(typeof(NUnit.Framework.TestFixtureSetUpAttribute), false); if (attributes == null || attributes.Length == 0) { System.Diagnostics.Debug.Assert(!defined, "testing IsDefined equivalent [not]"); // Not [TestFixtureSetUp] method. continue; } System.Diagnostics.Debug.Assert(defined, "testing IsDefined equivalent [not]"); try { curMethod.Invoke(instance, null); } catch (Exception ex) { TheTestFailedByException(curMethod, ex); goto exit; } } //---------- foreach (MethodInfo curMethod in methods) { if (m_userStopRequested) { break; } bool defined = curMethod.IsDefined(typeof(NUnit.Framework.TestAttribute), false); object[] attributes = curMethod.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), false); if (attributes == null || attributes.Length == 0) { System.Diagnostics.Debug.Assert(!defined, "testing IsDefined equivalent [not]"); // Not [Test] method. continue; } System.Diagnostics.Debug.Assert(defined, "testing IsDefined equivalent [not]"); NUnit.Framework.ExpectedExceptionAttribute[] expctdExAttributes = (NUnit.Framework.ExpectedExceptionAttribute[]) curMethod.GetCustomAttributes( typeof(NUnit.Framework.ExpectedExceptionAttribute), false); System.Diagnostics.Debug.Assert(expctdExAttributes.Length == 0 || expctdExAttributes.Length == 1); try { object[] ignoreAttributes = curMethod.GetCustomAttributes(typeof(NUnit.Framework.IgnoreAttribute), false); if (ignoreAttributes != null && ignoreAttributes.Length > 0) { // [Ignore] test method. System.Diagnostics.Debug.Assert(ignoreAttributes.Length == 1, "FAIL: attributes.Length == 1"); NUnit.Framework.IgnoreAttribute attr = (NUnit.Framework.IgnoreAttribute)ignoreAttributes[0]; throw new NUnit.Framework.IgnoreException(attr.Reason); } object[] explicitAttributes = curMethod.GetCustomAttributes(typeof(NUnit.Framework.ExplicitAttribute), false); if (explicitAttributes != null && explicitAttributes.Length > 0) { // [Explicit] test method. System.Diagnostics.Debug.Assert(explicitAttributes.Length == 1, "FAIL: attributes.Length == 1"); continue; } //-------------------------------------------------- // Do it!!!!!!! int startTickCount = Environment.TickCount; curMethod.Invoke(instance, null); int elapsed = Environment.TickCount - startTickCount; const String Sep = ","; timings.Append(testFixtureType.Name).Append("."); timings.Append(curMethod.Name).Append(Sep); timings.Append(elapsed).Append("\r\n"); if (expctdExAttributes != null && expctdExAttributes.Length > 0) { /*Failed due to missing expected exception.*/ TheTestFailedByNotExpectedException(curMethod, expctdExAttributes); continue; } TheTestPassed(curMethod); // Done or thrown... //-------------------------------------------------- } catch (NUnit.Framework.IgnoreException iex) { TheTestNotRun(curMethod, iex.Message); } catch (Exception testResultEx) { if (expctdExAttributes != null && expctdExAttributes.Length > 0) { NUnit.Framework.ExpectedExceptionAttribute expExAttr = expctdExAttributes[0]; Type expExType = expExAttr.ExceptionType; String expExName = expExAttr.ExceptionName; if (testResultEx.GetType().Equals(expExType)) { /* NOP Expected */ TheTestPassed(curMethod); continue; } else if (expExName != null && testResultEx.GetType().FullName.Equals(expExName)) { // separate just now for initial testing; fold into the above... /* NOP Expected */ TheTestPassed(curMethod); continue; } } // Otherwise unexpected! TheTestFailedByException(curMethod, testResultEx); } }//for //---------- // [TestFixtureTearDownAttribute] foreach (MethodInfo curMethod in methods) { bool defined = curMethod.IsDefined(typeof(NUnit.Framework.TestFixtureTearDownAttribute), false); object[] attributes = curMethod.GetCustomAttributes(typeof(NUnit.Framework.TestFixtureTearDownAttribute), false); if (attributes == null || attributes.Length == 0) { System.Diagnostics.Debug.Assert(!defined, "testing IsDefined equivalent [not]"); // Not [TestFixtureSetUp] method. continue; } System.Diagnostics.Debug.Assert(defined, "testing IsDefined equivalent [not]"); try{ curMethod.Invoke(instance, null); } catch (Exception ex) { TheTestFailedByException(curMethod, ex); goto exit; } } //---------- exit: m_progressCountOfTestClasses++; }