public TestOutcome Run()
            {
                TestContext context = TestContext.CurrentContext;

                using (skipProtect ? null : context.Sandbox.Protect())
                {
                    try
                    {
                        if (executor.progressMonitor.IsCanceled)
                            return TestOutcome.Canceled;

                        var outcome = TestOutcome.Passed;

                        if (!executor.options.SkipTestExecution)
                        {
                            context.LifecyclePhase = LifecyclePhases.Initialize;

                            UpdateInterimOutcome(context, ref outcome,
                                context.Sandbox.Run(TestLog.Writer, new InitializeTestInstanceAction(testInstanceState).Run, "Initialize"));
                        }

                        if (outcome.Status == TestStatus.Passed)
                        {
                            if (!executor.options.SkipTestExecution)
                            {
                                context.LifecyclePhase = LifecyclePhases.SetUp;

                                UpdateInterimOutcome(context, ref outcome,
                                    context.Sandbox.Run(TestLog.Writer, new SetUpTestInstanceAction(testInstanceState).Run, "Set Up"));
                            }

                            if (outcome.Status == TestStatus.Passed)
                            {
                                if (!executor.options.SkipTestExecution)
                                {
                                    context.LifecyclePhase = LifecyclePhases.Execute;

                                    UpdateInterimOutcome(context, ref outcome,
                                        context.Sandbox.Run(TestLog.Writer, new ExecuteTestInstanceAction(testInstanceState).Run, "Execute"));
                                }

                                if (outcome.Status == TestStatus.Passed)
                                {
                                    // Run all test children.
                                    PatternTestActionsDecorator testActionsDecorator =
                                        delegate(Sandbox childSandbox, ref PatternTestActions childTestActions)
                                        {
                                            childTestActions = childTestActions.Copy();
                                            return childSandbox.Run(TestLog.Writer, new DecorateChildTestAction(testInstanceState, childTestActions).Run, "Decorate Child Test");
                                        };

                                    foreach (TestBatch batch in GenerateTestBatches(testCommand.Children))
                                    {
                                        if (batch.Commands.Count == 1)
                                        {
                                            // Special case to reduce effective stack depth and cost since there is no
                                            // need to use the scheduler if nothing is running in parallel.
                                            RunTestAction action = new RunTestAction(executor, batch.Commands[0], context, testActionsDecorator);

                                            action.Run();

                                            UpdateInterimOutcome(context, ref outcome, action.Result.Outcome.Generalize());
                                        }
                                        else
                                        {
                                            RunTestAction[] actions = GenericCollectionUtils.ConvertAllToArray(batch.Commands,
                                                childTestCommand => new RunTestAction(executor, childTestCommand, context, testActionsDecorator));

                                            executor.scheduler.Run(Array.ConvertAll<RunTestAction, Action>(actions, action => action.Run));

                                            TestOutcome combinedChildOutcome = TestOutcome.Passed;
                                            foreach (var action in actions)
                                                combinedChildOutcome = combinedChildOutcome.CombineWith(action.Result.Outcome);

                                            UpdateInterimOutcome(context, ref outcome, combinedChildOutcome.Generalize());
                                        }
                                    }
                                }
                            }

                            if (!executor.options.SkipTestExecution)
                            {
                                context.LifecyclePhase = LifecyclePhases.TearDown;

                                UpdateInterimOutcome(context, ref outcome,
                                    context.Sandbox.Run(TestLog.Writer, new TearDownTestInstanceAction(testInstanceState).Run, "Tear Down"));
                            }
                        }

                        if (!executor.options.SkipTestExecution)
                        {
                            context.LifecyclePhase = LifecyclePhases.Dispose;

                            UpdateInterimOutcome(context, ref outcome,
                                context.Sandbox.Run(TestLog.Writer, new DisposeTestInstanceAction(testInstanceState).Run, "Dispose"));
                        }

                        return outcome;
                    }
                    catch (Exception ex)
                    {
                        TestLog.Failures.WriteException(ex,
                            String.Format("An exception occurred while running test instance '{0}'.", testInstanceState.TestStep.Name));
                        return TestOutcome.Error;
                    }
                }
            }
Esempio n. 2
0
            public TestOutcome Run()
            {
                TestContext context = TestContext.CurrentContext;

                using (skipProtect ? null : context.Sandbox.Protect())
                {
                    try
                    {
                        if (executor.progressMonitor.IsCanceled)
                        {
                            return(TestOutcome.Canceled);
                        }

                        var outcome = TestOutcome.Passed;

                        if (!executor.options.SkipTestExecution)
                        {
                            context.LifecyclePhase = LifecyclePhases.Initialize;

                            UpdateInterimOutcome(context, ref outcome,
                                                 context.Sandbox.Run(TestLog.Writer, new InitializeTestInstanceAction(testInstanceState).Run, "Initialize"));
                        }

                        if (outcome.Status == TestStatus.Passed)
                        {
                            if (!executor.options.SkipTestExecution)
                            {
                                context.LifecyclePhase = LifecyclePhases.SetUp;

                                UpdateInterimOutcome(context, ref outcome,
                                                     context.Sandbox.Run(TestLog.Writer, new SetUpTestInstanceAction(testInstanceState).Run, "Set Up"));
                            }

                            if (outcome.Status == TestStatus.Passed)
                            {
                                if (!executor.options.SkipTestExecution)
                                {
                                    context.LifecyclePhase = LifecyclePhases.Execute;

                                    UpdateInterimOutcome(context, ref outcome,
                                                         context.Sandbox.Run(TestLog.Writer, new ExecuteTestInstanceAction(testInstanceState).Run, "Execute"));
                                }

                                if (outcome.Status == TestStatus.Passed)
                                {
                                    // Run all test children.
                                    PatternTestActionsDecorator testActionsDecorator =
                                        delegate(Sandbox childSandbox, ref PatternTestActions childTestActions)
                                    {
                                        childTestActions = childTestActions.Copy();
                                        return(childSandbox.Run(TestLog.Writer, new DecorateChildTestAction(testInstanceState, childTestActions).Run, "Decorate Child Test"));
                                    };

                                    foreach (TestBatch batch in GenerateTestBatches(testCommand.Children))
                                    {
                                        if (batch.Commands.Count == 1)
                                        {
                                            // Special case to reduce effective stack depth and cost since there is no
                                            // need to use the scheduler if nothing is running in parallel.
                                            RunTestAction action = new RunTestAction(executor, batch.Commands[0], context, testActionsDecorator);

                                            action.Run();

                                            UpdateInterimOutcome(context, ref outcome, action.Result.Outcome.Generalize());
                                        }
                                        else
                                        {
                                            RunTestAction[] actions = GenericCollectionUtils.ConvertAllToArray(batch.Commands,
                                                                                                               childTestCommand => new RunTestAction(executor, childTestCommand, context, testActionsDecorator));

                                            executor.scheduler.Run(Array.ConvertAll <RunTestAction, GallioAction>(actions, action => action.Run));

                                            TestOutcome combinedChildOutcome = TestOutcome.Passed;
                                            foreach (var action in actions)
                                            {
                                                combinedChildOutcome = combinedChildOutcome.CombineWith(action.Result.Outcome);
                                            }

                                            UpdateInterimOutcome(context, ref outcome, combinedChildOutcome.Generalize());
                                        }
                                    }
                                }
                            }

                            if (!executor.options.SkipTestExecution)
                            {
                                context.LifecyclePhase = LifecyclePhases.TearDown;

                                UpdateInterimOutcome(context, ref outcome,
                                                     context.Sandbox.Run(TestLog.Writer, new TearDownTestInstanceAction(testInstanceState).Run, "Tear Down"));
                            }
                        }

                        if (!executor.options.SkipTestExecution)
                        {
                            context.LifecyclePhase = LifecyclePhases.Dispose;

                            UpdateInterimOutcome(context, ref outcome,
                                                 context.Sandbox.Run(TestLog.Writer, new DisposeTestInstanceAction(testInstanceState).Run, "Dispose"));
                        }

                        return(outcome);
                    }
                    catch (Exception ex)
                    {
                        TestLog.Failures.WriteException(ex,
                                                        String.Format("An exception occurred while running test instance '{0}'.", testInstanceState.TestStep.Name));
                        return(TestOutcome.Error);
                    }
                }
            }