コード例 #1
0
        /// <summary>
        /// Runn the test.
        /// </summary>
        public void RunTest(ITest test)
        {
            TestTimer timer = new TestTimer();

            try
            {
                timer.Start(test);
                test.TestMethod.Invoke(test.Fixture.Instance, null);
                timer.Stop();
            }
            catch (TargetInvocationException tie)
            {
                timer.Stop();
                Type thrownExceptionType = tie.InnerException.GetType();
                test.Result.Message.AppendFormat("Expected Exception: {0}", ExceptionType.FullName);
                if (thrownExceptionType.Equals(ExceptionType) && (Message == null || tie.InnerException.Message == Message))
                {
                    test.Result.Status = TestStatus.Pass;
                    test.Result.Message.AppendLine(" was thrown.");
                }
                else
                {
                    test.Result.Status = TestStatus.Fail;
                    test.Result.Message.AppendLine(" was NOT thrown.");
                }
                test.Result.Message.Append("Message: ");
                test.Result.Message.AppendLine(tie.InnerException.Message);
                test.Result.Message.Append("Exception Type: ");
                test.Result.Message.AppendLine(thrownExceptionType.FullName);
                test.Result.SetFilteredStackTrace(tie.InnerException.StackTrace);
            }
            finally
            {
                timer.Stop();
                if (test.Result.Status == TestStatus.Untested)
                {
                    // No exception has been thrown.

                    test.Result.Status = TestStatus.Fail;
                    test.Result.Message.AppendFormat("Expected Exception: {0}", ExceptionType.FullName);
                    test.Result.Message.AppendLine(" was NOT thrown.");
                    SequenceManager sm = new SequenceManager(test.TestMethod);
                    if (sm.IsSourceAvailable())
                    {
                        test.Result.StackTrace = sm.GetStackTrace(0);
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Run the test.
        /// </summary>
        public void RunTest(ITest test)
        {
            Type       fixtureType = test.Fixture.Instance.GetType();
            MethodInfo method      = test.TestMethod;

            DynamicMethod testMethod = GetTestMethod(fixtureType, method);

            TestTimer timer = new TestTimer();
            Dictionary <int, Exception> exceptionsThrown;
            TestDelegate testDelegate = testMethod.CreateDelegate(typeof(TestDelegate), test.Fixture.Instance) as TestDelegate;

            try
            {
                timer.Start(test);
                exceptionsThrown = testDelegate();
                timer.Stop();
            }
            catch (InvalidProgramException exp)
            {
                timer.Stop();
                throw new InvalidOperationException("Unable to catch thrown exceptions in test method.", exp);
            }
            finally
            {
                timer.Stop();
            }

            SequenceManager sm = new SequenceManager(test.TestMethod);
            int             expectedExceptionCount  = 0;
            int             unexpectedExpectedCount = 0;
            int             i = 0;

            foreach (KeyValuePair <int, Exception> exception in exceptionsThrown)
            {
                if (exception.Value != null)
                {
                    Type thrownExceptionType = exception.Value.GetType();
                    test.Result.Output.AppendFormat("[{0}] Expected Exception: {1}", i + 1, ExceptionType.FullName);
                    if (thrownExceptionType.Equals(ExceptionType))
                    {
                        expectedExceptionCount++;
                        test.Result.Output.AppendLine(" was thrown.");
                    }
                    else
                    {
                        // Get line offset from il instruction offset.
                        unexpectedExpectedCount++;
                        test.Result.Output.AppendLine(" was NOT thrown.");
                        test.Result.Output.Append("\tThrown Exception Was: ");
                        if (sm.IsSourceAvailable())
                        {
                            int    sequenceOffset = Math.Abs(Array.BinarySearch <int>(sm.Offsets, exception.Key));
                            string dynamicLine    = String.Format("   at {0}_TestMethod({1} )",
                                                                  test.TestMethod.Name, test.Fixture.FixtureType.Name);
                            string sourceLine = sm.GetStackTrace(sequenceOffset);
                            test.Result.Output.AppendLine(
                                exception.Value.ToString().Replace(dynamicLine, sourceLine));
                            if (test.Result.StackTrace == null)
                            {
                                test.Result.SetFilteredStackTrace(
                                    exception.Value.StackTrace.Replace(dynamicLine, sourceLine));
                            }
                        }
                        else
                        {
                            test.Result.Output.AppendLine(exception.Value.ToString());
                            if (test.Result.StackTrace == null)
                            {
                                test.Result.SetFilteredStackTrace(exception.Value.StackTrace);
                            }
                        }
                    }
                }
                else
                {
                    test.Result.Output.AppendFormat("[{0}] No exception was thrown.\n", i + 1);
                }
                i++;
            }

            if (test.Result.StackTrace == null && sm.IsSourceAvailable())
            {
                test.Result.StackTrace = sm.GetStackTrace(0);
            }

            int expectedNumberOfExceptions = (ExceptionCount == UseTestCount ? exceptionsThrown.Count : ExceptionCount);

            test.Result.Message.AppendFormat(ExpectedExceptionCountMessage,
                                             ExceptionType.FullName, expectedExceptionCount, exceptionsThrown.Count, expectedNumberOfExceptions,
                                             unexpectedExpectedCount, (unexpectedExpectedCount != 1 ? " was" : "s were"));
            if (expectedExceptionCount != expectedNumberOfExceptions || (FailOnOtherExceptions && unexpectedExpectedCount > 0))
            {
                test.Result.Status = TestStatus.Fail;
            }
            else
            {
                test.Result.Status = TestStatus.Pass;
            }
        }