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

            var task = result as Task;
            if (task != null)
                task.Wait();
        }
Exemplo n.º 2
0
        private PatternTestInstanceState GetCurrentTestInstanceState()
        {
            var state = PatternTestInstanceState.FromContext(TestContext.CurrentContext);

            if (state == null)
            {
                throw new NotSupportedException("Could not find the current pattern test instance state. The attribute probably cannot be used in this context.");
            }

            return(state);
        }
Exemplo n.º 3
0
 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;
     }
 }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
 public ExecuteTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
Exemplo n.º 6
0
 public DecorateChildTestAction(PatternTestInstanceState testInstanceState, PatternTestActions decoratedChildTestActions)
 {
     this.testInstanceState = testInstanceState;
     this.decoratedChildTestActions = decoratedChildTestActions;
 }
Exemplo n.º 7
0
 public AfterTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
Exemplo n.º 8
0
 public static bool CanOptimizeCallChainAndInvokeBodyActionDirectly(PatternTestInstanceState testInstanceState)
 {
     return testInstanceState.TestInstanceActions.IsDefaultRunTestInstanceBodyChain;
 }
Exemplo n.º 9
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);
        }
Exemplo n.º 11
0
 public DisposeTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
Exemplo n.º 12
0
 internal static TestOutcome RunBody(PatternTestInstanceState state)
 {
     return(state.body());
 }
 /// <summary>
 /// Disposes 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.DisposeTestInstanceChain"/>
 protected virtual void Dispose(PatternTestInstanceState testInstanceState)
 {
     InvokeDefaultAction(testInstanceState);
 }
 private void InvokeDefaultAction(PatternTestInstanceState testInstanceState)
 {
     Action<PatternTestInstanceState> action = testInstanceState.Data.GetValue(defaultActionKey);
     action(testInstanceState);
 }
 /// <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);
 }
 /// <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);
 }
Exemplo n.º 17
0
 protected virtual void Execute(PatternTestInstanceState state)
 {
     state.InvokeTestMethod();
 }
Exemplo n.º 18
0
            public void Run()
            {
                if (executor.progressMonitor.IsCanceled)
                {
                    outcome = TestOutcome.Canceled;
                    return;
                }

                TestOutcome instanceOutcome;
                try
                {
                    PatternTestInstanceActions decoratedTestInstanceActions = testState.TestActions.TestInstanceActions.Copy();

                    instanceOutcome = primaryContext.Sandbox.Run(TestLog.Writer,
                        new DecorateTestInstanceAction(testState, decoratedTestInstanceActions).Run, "Decorate Child Test");

                    if (instanceOutcome.Status == TestStatus.Passed)
                    {
                        bool invisibleTestInstance = true;

                        PatternTestStep testStep;
                        if (reusePrimaryTestStep)
                        {
                            testStep = testState.PrimaryTestStep;
                            invisibleTestInstance = false;

                            PropertyBag map = DataItemUtils.GetMetadata(bindingItem);
                            foreach (KeyValuePair<string, string> entry in map.Pairs)
                                primaryContext.AddMetadata(entry.Key, entry.Value);
                        }
                        else
                        {
                            testStep = new PatternTestStep(testState.Test, testState.PrimaryTestStep,
                                testState.Test.Name, testState.Test.CodeElement, false);
                            testStep.Kind = testState.Test.Kind;

                            testStep.IsDynamic = bindingItem.IsDynamic;
                            bindingItem.PopulateMetadata(testStep.Metadata);
                        }

                        var bodyAction = new RunTestInstanceAction(executor, testCommand);
                        var testInstanceState = new PatternTestInstanceState(testStep, decoratedTestInstanceActions, testState, bindingItem, bodyAction.Run);
                        bodyAction.TestInstanceState = testInstanceState;

                        instanceOutcome = instanceOutcome.CombineWith(primaryContext.Sandbox.Run(
                            TestLog.Writer, new BeforeTestInstanceAction(testInstanceState).Run, "Before Test Instance"));

                        if (instanceOutcome.Status == TestStatus.Passed)
                        {
                            executor.progressMonitor.SetStatus(testStep.Name);

                            TestContext context = reusePrimaryTestStep
                                ? primaryContext
                                : TestContext.PrepareContext(testCommand.StartStep(testStep), primaryContext.Sandbox.CreateChild());
                            testState.SetInContext(context);
                            testInstanceState.SetInContext(context);
                            invisibleTestInstance = false;

                            using (context.Enter())
                            {
                                if (RunTestInstanceBodyAction.CanOptimizeCallChainAndInvokeBodyActionDirectly(testInstanceState))
                                {
                                    bodyAction.SkipProtect = true;
                                    instanceOutcome = instanceOutcome.CombineWith(bodyAction.Run());
                                }
                                else
                                {
                                    var runTestInstanceBodyAction = new RunTestInstanceBodyAction(testInstanceState);
                                    TestOutcome sandboxOutcome = context.Sandbox.Run(TestLog.Writer, runTestInstanceBodyAction.Run, "Body");

                                    instanceOutcome = instanceOutcome.CombineWith(runTestInstanceBodyAction.Outcome.CombineWith(sandboxOutcome));
                                }
                            }

                            if (!reusePrimaryTestStep)
                                context.FinishStep(instanceOutcome);

                            executor.progressMonitor.SetStatus("");
                        }

                        instanceOutcome = instanceOutcome.CombineWith(primaryContext.Sandbox.Run(
                            TestLog.Writer, new AfterTestInstanceAction(testInstanceState).Run, "After Test Instance"));

                        if (invisibleTestInstance)
                            instanceOutcome = PublishOutcomeFromInvisibleTest(testCommand, testStep, instanceOutcome).Outcome;
                    }
                }
                catch (Exception ex)
                {
                    string message = String.Format("An exception occurred while preparing an instance of test '{0}'.", testState.Test.FullName);

                    if (reusePrimaryTestStep)
                    {
                        TestLog.Failures.WriteException(ex, message);
                        instanceOutcome = TestOutcome.Error;
                    }
                    else
                    {
                        instanceOutcome = ReportTestError(testCommand, testState.PrimaryTestStep, ex, message).Outcome;
                    }
                }

                outcome = instanceOutcome;
            }
        /// <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;
                }
            }
        }
Exemplo n.º 20
0
 public DecorateChildTestAction(PatternTestInstanceState testInstanceState, PatternTestActions decoratedChildTestActions)
 {
     this.testInstanceState         = testInstanceState;
     this.decoratedChildTestActions = decoratedChildTestActions;
 }
Exemplo n.º 21
0
 public AfterTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
 private void WithMultiple(PatternTestInstanceState state, Action<PatternTestInstanceState> action)
 {
     Assert.Multiple(() => action(state));
 }
 internal static TestOutcome RunBody(PatternTestInstanceState state)
 {
     return state.body();
 }
 protected virtual void Execute(PatternTestInstanceState state)
 {
     state.InvokeTestMethod();
 }
 public void UnbindTestParameter(PatternTestInstanceState testInstanceState, object value)
 {
     unbindTestParameterChain.Action(testInstanceState, value);
 }
Exemplo n.º 26
0
            public void Run()
            {
                if (executor.progressMonitor.IsCanceled)
                {
                    outcome = TestOutcome.Canceled;
                    return;
                }

                TestOutcome instanceOutcome;

                try
                {
                    PatternTestInstanceActions decoratedTestInstanceActions = testState.TestActions.TestInstanceActions.Copy();

                    instanceOutcome = primaryContext.Sandbox.Run(TestLog.Writer,
                                                                 new DecorateTestInstanceAction(testState, decoratedTestInstanceActions).Run, "Decorate Child Test");

                    if (instanceOutcome.Status == TestStatus.Passed)
                    {
                        bool invisibleTestInstance = true;

                        PatternTestStep testStep;
                        if (reusePrimaryTestStep)
                        {
                            testStep = testState.PrimaryTestStep;
                            invisibleTestInstance = false;

                            PropertyBag map = DataItemUtils.GetMetadata(bindingItem);
                            foreach (KeyValuePair <string, string> entry in map.Pairs)
                            {
                                primaryContext.AddMetadata(entry.Key, entry.Value);
                            }
                        }
                        else
                        {
                            testStep = new PatternTestStep(testState.Test, testState.PrimaryTestStep,
                                                           testState.Test.Name, testState.Test.CodeElement, false);
                            testStep.Kind = testState.Test.Kind;

                            testStep.IsDynamic = bindingItem.IsDynamic;
                            bindingItem.PopulateMetadata(testStep.Metadata);
                        }

                        var bodyAction        = new RunTestInstanceAction(executor, testCommand);
                        var testInstanceState = new PatternTestInstanceState(testStep, decoratedTestInstanceActions, testState, bindingItem, bodyAction.Run);
                        bodyAction.TestInstanceState = testInstanceState;

                        instanceOutcome = instanceOutcome.CombineWith(primaryContext.Sandbox.Run(
                                                                          TestLog.Writer, new BeforeTestInstanceAction(testInstanceState).Run, "Before Test Instance"));

                        if (instanceOutcome.Status == TestStatus.Passed)
                        {
                            executor.progressMonitor.SetStatus(testStep.Name);

                            TestContext context = reusePrimaryTestStep
                                ? primaryContext
                                : TestContext.PrepareContext(testCommand.StartStep(testStep), primaryContext.Sandbox.CreateChild());
                            testState.SetInContext(context);
                            testInstanceState.SetInContext(context);
                            invisibleTestInstance = false;

                            using (context.Enter())
                            {
                                if (RunTestInstanceBodyAction.CanOptimizeCallChainAndInvokeBodyActionDirectly(testInstanceState))
                                {
                                    bodyAction.SkipProtect = true;
                                    instanceOutcome        = instanceOutcome.CombineWith(bodyAction.Run());
                                }
                                else
                                {
                                    var         runTestInstanceBodyAction = new RunTestInstanceBodyAction(testInstanceState);
                                    TestOutcome sandboxOutcome            = context.Sandbox.Run(TestLog.Writer, runTestInstanceBodyAction.Run, "Body");

                                    instanceOutcome = instanceOutcome.CombineWith(runTestInstanceBodyAction.Outcome.CombineWith(sandboxOutcome));
                                }
                            }

                            if (!reusePrimaryTestStep)
                            {
                                context.FinishStep(instanceOutcome);
                            }

                            executor.progressMonitor.SetStatus("");
                        }

                        instanceOutcome = instanceOutcome.CombineWith(primaryContext.Sandbox.Run(
                                                                          TestLog.Writer, new AfterTestInstanceAction(testInstanceState).Run, "After Test Instance"));

                        if (invisibleTestInstance)
                        {
                            instanceOutcome = PublishOutcomeFromInvisibleTest(testCommand, testStep, instanceOutcome).Outcome;
                        }
                    }
                }
                catch (Exception ex)
                {
                    string message = String.Format("An exception occurred while preparing an instance of test '{0}'.", testState.Test.FullName);

                    if (reusePrimaryTestStep)
                    {
                        TestLog.Failures.WriteException(ex, message);
                        instanceOutcome = TestOutcome.Error;
                    }
                    else
                    {
                        instanceOutcome = ReportTestError(testCommand, testState.PrimaryTestStep, ex, message).Outcome;
                    }
                }

                outcome = instanceOutcome;
            }
Exemplo n.º 27
0
 public RunTestInstanceBodyAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
     outcome = TestOutcome.Error;
 }
Exemplo n.º 28
0
 public BeforeTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
Exemplo n.º 29
0
 public TearDownTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
Exemplo n.º 30
0
 public InitializeTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
Exemplo n.º 31
0
 public BeforeTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
Exemplo n.º 32
0
 public ExecuteTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
Exemplo n.º 33
0
 public DisposeTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
Exemplo n.º 34
0
 public RunTestInstanceBodyAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
     outcome = TestOutcome.Error;
 }
Exemplo n.º 35
0
 public InitializeTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }
Exemplo n.º 36
0
 public static bool CanOptimizeCallChainAndInvokeBodyActionDirectly(PatternTestInstanceState testInstanceState)
 {
     return(testInstanceState.TestInstanceActions.IsDefaultRunTestInstanceBodyChain);
 }
 public void UnbindTestParameter(PatternTestInstanceState testInstanceState, object value)
 {
     unbindTestParameterChain.Action(testInstanceState, value);
 }
Exemplo n.º 38
0
 public TearDownTestInstanceAction(PatternTestInstanceState testInstanceState)
 {
     this.testInstanceState = testInstanceState;
 }