Esempio n. 1
0
        /// <inheritdoc />
        protected override void Execute(PatternTestInstanceState state)
        {
            var result = state.InvokeTestMethod();

            var task = result as Task;

            if (task != null)
            {
                task.Wait();
            }
        }
Esempio n. 2
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);
        }
 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;
     }
 }
Esempio n. 4
0
        private static TransactionScope CreateAndEnterTransactionScope(PatternTestInstanceState state)
        {
            TimeSpan timeout = TransactionManager.MaximumTimeout;
            TimeSpan?value   = state.Test.TimeoutFunc();

            if (value.HasValue && value.Value < timeout)
            {
                timeout = value.Value;
            }

            var options = new TransactionOptions()
            {
                Timeout = timeout
            };

            return(new TransactionScope(TransactionScopeOption.RequiresNew, options, EnterpriseServicesInteropOption.Full));
        }
        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);
            }
        }
Esempio n. 6
0
 private void WithMultiple(PatternTestInstanceState state, Action <PatternTestInstanceState> action)
 {
     Assert.Multiple(() => action(state));
 }
        /// <summary>
        /// This method does the actual work of catching and handling (or not) <see cref="Exception"/>s.
        /// </summary>
        private void ExecuteTestInstanceChainCatchExceptionDecorator(Action <PatternTestInstanceState> inner, PatternTestInstanceState state)
        {
            try
            {
                inner(state);
                OnInnerActionCompleted(null);
            }
            catch (TestException ex)
            {
                //If the exception is a TestException it must be rethrown
                //because it represents the (non-Passed) TestOutcome of the inner action

                //TODO: Review: Should we call ExceptionUtils.RethrowWithNoStackTraceLoss(ex) here?
                ExceptionUtils.RethrowWithNoStackTraceLoss(ex);
                throw;
            }
            catch (Exception ex)
            {
                //NOTE: Ideally, the whole catch block would be implemented inside of <see cref="OnInnerActionCompleted"/>.
                //However, that is not possible for technical reasons.
                //See the Implementation Note in the remarks section of the <see cref="OnInnerActionCompleted"/>
                //documentation for details.

                if (OnInnerActionCompleted(ex))
                {
                    return;
                }
                else
                {
                    //TODO: Review: Should we call ExceptionUtils.RethrowWithNoStackTraceLoss(ex) here?
                    ExceptionUtils.RethrowWithNoStackTraceLoss(ex);
                    throw;
                }
            }
        }
 /// <summary>
 /// Executes the test.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Override this method to add your own behavior around the default actions.
 /// </para>
 /// </remarks>
 /// <param name="testInstanceState">The test instance state, not null.</param>
 /// <seealso cref="PatternTestInstanceActions.ExecuteTestInstanceChain"/>
 protected virtual void Execute(PatternTestInstanceState testInstanceState)
 {
     InvokeDefaultAction(testInstanceState);
 }
 /// <summary>
 /// Initializes the test.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Override this method to add your own behavior around the default actions.
 /// </para>
 /// </remarks>
 /// <param name="testInstanceState">The test instance state, not null.</param>
 /// <seealso cref="PatternTestInstanceActions.InitializeTestInstanceChain"/>
 protected virtual void Initialize(PatternTestInstanceState testInstanceState)
 {
     InvokeDefaultAction(testInstanceState);
 }
Esempio n. 10
0
        private void InvokeDefaultAction(PatternTestInstanceState testInstanceState)
        {
            Action <PatternTestInstanceState> action = testInstanceState.Data.GetValue(defaultActionKey);

            action(testInstanceState);
        }
Esempio n. 11
0
 /// <summary>
 /// Tears down the test.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Override this method to add your own behavior around the default actions.
 /// </para>
 /// </remarks>
 /// <param name="testInstanceState">The test instance state, not null.</param>
 /// <seealso cref="PatternTestInstanceActions.TearDownTestInstanceChain"/>
 protected virtual void TearDown(PatternTestInstanceState testInstanceState)
 {
     InvokeDefaultAction(testInstanceState);
 }