/// <summary> /// Runs the test and returns the result xml. /// </summary> /// <returns></returns> public XElement RunTestCase(TestCaseMetadata metadata) { ManagedTestCase testCase = null; object testObject = null; try { testCase = Controller.CreateManagedTestCase(metadata); testObject = testCase.CreateNewTestObject(); } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ((TargetInvocationException)ex).InnerException; } throw new Exception("Unit test threw exception while creating test class.", ex); } // Exec the unit test MethodInfo unitTestMethod = testCase.Method; object[] unitTestArgs = testCase.Arguments; try { Invoke(testCase, testObject, unitTestMethod, unitTestArgs, testCase.DisplayNameWithArgs); if (testCase.ExpectedExceptionType != null) { string errMsg = AssertMessagesUtil.FormatAssertionMessage_ExpectedExceptionNotThrown(testCase); return(TestResultUtil.CreateXTestResult(TestResultType.AssertFailure, errMsg, null)); } } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ((TargetInvocationException)ex).InnerException; } Type exType = ex.GetType(); // Detect expected exceptions and handle Assert exceptions if (testCase.ExpectedExceptionType == null || !testCase.ExpectedExceptionType.IsAssignableFrom(exType)) { // Handle special UnitTest exceptions if (ex is ConcurrencyUnitTestException) { // Pre-defined exceptions can just pass up their messages if (ex is AssertFailedException) { return(TestResultUtil.CreateXTestResult(TestResultType.AssertFailure, ex.Message, ex)); } else if (ex is AssertInconclusiveException) { return(TestResultUtil.CreateXTestResult(TestResultType.Inconclusive, ex.Message, ex)); } #if DEBUG // If there's another ex type that we've defined in the framework but haven't handled // by the prev conditions lets warn the developer. Type cutExType = typeof(ConcurrencyUnitTestException); if (exType != cutExType && exType.Assembly.FullName == cutExType.Assembly.FullName) { System.Diagnostics.Trace.TraceWarning("Unhandled ConcurrencyUnitTestException derived type in the CUT assembly: " + exType.Name + ". Custom handling should be added here."); } #endif // If not a built in exception, then use the regular handling below. } // If not an assert, then it's an unexpected exception if (testCase.ExpectedExceptionType == null) { string errMsg = AssertMessagesUtil.FormatAssertionMessage_UnExpectedExceptionThrown(testCase, ex); // Write the exception to the trace log so a debugger can see it. System.Diagnostics.Trace.TraceError(errMsg + Environment.NewLine + "Stack Trace: " + ex.StackTrace); return(TestResultUtil.CreateXTestResult(TestResultType.Exception, errMsg, ex)); } else // Then the exception isn't what's expected { string errMsg = AssertMessagesUtil.FormatAssertionMessage_ExceptionOfWrongTypeThrown(testCase, ex); // Write the exception to the trace log so a debugger can see it. System.Diagnostics.Trace.TraceError(errMsg + Environment.NewLine + "Stack Trace: " + ex.StackTrace); return(TestResultUtil.CreateXTestResult(TestResultType.AssertFailure, errMsg, ex)); } } // else - The exception was expected, so the test succedded, keep going } return(TestResultUtil.CreateXTestResult(TestResultType.Passed, null, null)); }
private void do_run() { if (runUnitTest) { object testObject = null; try { testObject = unitTestCase.CreateNewTestObject(); } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ((TargetInvocationException)ex).InnerException; } ReportErrorAndExit("Unit test threw exception while creating test class.", ChessExitCode.TestFailure, true, ex); } // Exec the unit test MethodInfo unitTestMethod = unitTestCase.Method; object[] unitTestArgs = unitTestCase.Arguments; try { unitTestMethod.Invoke(testObject, unitTestArgs); if (unitTestCase.ExpectedExceptionType != null) { string errMsg = AssertMessagesUtil.FormatAssertionMessage_ExpectedExceptionNotThrown(unitTestCase); ReportErrorAndExit(errMsg, ChessExitCode.UnitTestAssertFailure, true, null); } } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ((TargetInvocationException)ex).InnerException; } Type exType = ex.GetType(); // Detect expected exceptions and handle Assert exceptions if (unitTestCase.ExpectedExceptionType == null || !unitTestCase.ExpectedExceptionType.IsAssignableFrom(exType)) { // Handle special UnitTest exceptions if (ex is ConcurrencyUnitTestException) { // Pre-defined exceptions can just pass up their messages if (ex is AssertFailedException) { ReportErrorAndExit(ex.Message, ChessExitCode.UnitTestAssertFailure, true, ex); } else if (ex is AssertInconclusiveException) { ReportErrorAndExit(ex.Message, ChessExitCode.UnitTestAssertInconclusive, true, ex); } #if DEBUG // If there's another ex type that we've defined in the framework but haven't handled // by the prev conditions lets warn the developer. Type cutExType = typeof(ConcurrencyUnitTestException); if (exType != cutExType && exType.Assembly.FullName == cutExType.Assembly.FullName) { Trace.TraceWarning("Unhandled ConcurrencyUnitTestException derived type in the CUT assembly: " + exType.Name + ". Custom handling should be added here."); } #endif // If not a built in exception, then use the regular handling below. } // If not an assert, then it's an unexpected exception if (unitTestCase.ExpectedExceptionType == null) { string errMsg = AssertMessagesUtil.FormatAssertionMessage_UnExpectedExceptionThrown(unitTestCase, ex); // Write the exception to the trace log so a debugger can see it. Trace.TraceError(errMsg + Environment.NewLine + "Stack Trace: " + ex.StackTrace); ReportErrorAndExit(errMsg, ChessExitCode.UnitTestException, true, ex); } else // Then the exception isn't what's expected { string errMsg = AssertMessagesUtil.FormatAssertionMessage_ExceptionOfWrongTypeThrown(unitTestCase, ex); // Write the exception to the trace log so a debugger can see it. Trace.TraceError(errMsg + Environment.NewLine + "Stack Trace: " + ex.StackTrace); ReportErrorAndExit(errMsg, ChessExitCode.UnitTestAssertFailure, true, ex); } } // else - The exception was expected, so the test succeeded, keep going } } else { // Old code for when using the ChessTest class try { if (!(bool)run.Invoke(null, null)) { if (!manager.BreakDeadlockMode) { ReportErrorAndExit(testclass + ".Run returned false.", ChessExitCode.TestFailure, true, null); } } } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ((TargetInvocationException)ex).InnerException; } if (!manager.BreakDeadlockMode) { string message = testclass + ".Run raised exception"; ReportErrorAndExit(message, ChessExitCode.UnitTestException, true, ex); } } } // Always clean up the memory GC.Collect(); GC.WaitForPendingFinalizers(); manager.RunFinalizers(); }