Esempio n. 1
0
        protected override void Execute(PatternTestInstanceState state)
        {
            string expectedExceptionType = state.TestStep.Metadata.GetValue(MetadataKeys.ExpectedException)
                                           ?? state.Test.Metadata.GetValue(MetadataKeys.ExpectedException);

            if (expectedExceptionType == null)
            {
                state.InvokeTestMethod();
                return;
            }

            try
            {
                state.InvokeTestMethod();

                using (TestLog.Failures.BeginSection("Expected Exception"))
                    TestLog.Failures.WriteLine("Expected an exception of type '{0}' but none was thrown.", expectedExceptionType);
            }
            catch (Exception ex)
            {
                string expectedExceptionMessage = state.TestStep.Metadata.GetValue(MetadataKeys.ExpectedExceptionMessage)
                                                  ?? state.Test.Metadata.GetValue(MetadataKeys.ExpectedExceptionMessage);

                Type exceptionType = ex.GetType();

                if (!ReflectionUtils.IsAssignableFrom(expectedExceptionType, exceptionType) ||
                    (expectedExceptionMessage != null && !ex.Message.Contains(expectedExceptionMessage)))
                {
                    if (ex is TestException)
                    {
                        throw;
                    }

                    LogExpectedExceptionFailure(ex, expectedExceptionType, expectedExceptionMessage);
                }
                else
                {
                    string expectedInnerExceptionType = state.TestStep.Metadata.GetValue(MetadataKeys.ExpectedInnerException)
                                                        ?? state.Test.Metadata.GetValue(MetadataKeys.ExpectedInnerException);

                    if (expectedInnerExceptionType == null ||
                        (ex.InnerException != null && ReflectionUtils.IsAssignableFrom(expectedInnerExceptionType, ex.InnerException.GetType())))
                    {
                        return;
                    }

                    LogExpectedInnerExceptionFailure(ex, expectedInnerExceptionType);
                }
            }

            throw new SilentTestException(TestOutcome.Failed);
        }
Esempio n. 2
0
        /// <inheritdoc />
        protected override void Execute(PatternTestInstanceState state)
        {
            var result = state.InvokeTestMethod();

            var task = result as Task;

            if (task != null)
            {
                task.Wait();
            }
        }
 protected override void Execute(PatternTestInstanceState state)
 {
     System.Threading.SynchronizationContext oldContext = null;
     try
     {
         oldContext          = SyncContext.Current;
         SyncContext.Current = new TestSynchronizationContext();
         state.InvokeTestMethod();
     }
     finally
     {
         SyncContext.Current = oldContext;
     }
 }
        protected override void Execute(PatternTestInstanceState state)
        {
            var tests = state.InvokeTestMethod() as IEnumerable <Test>;

            if (tests == null)
            {
                throw new TestFailedException("Expected the dynamic test factory method to "
                                              + "return a value that is assignable to type IEnumerable<Test>.");
            }

            TestOutcome outcome = Test.RunDynamicTests(tests, state.Test.CodeElement, null, null);

            if (outcome != TestOutcome.Passed)
            {
                throw new SilentTestException(outcome);
            }
        }