Ejemplo n.º 1
0
        public UnitTestOutcome Execute()
        {
            this.RaiseStartEvent();

            Logger.WriteLine("Executing unit test: " + this.ID);

            int             stepCount = 0;
            UnitTestOutcome outcome   = new UnitTestOutcome();

            outcome.Test = this;
            this.ExecutedConstructors = new ObservableCollection <string>();
            AttributeConstructor.AttributeConstructorCompletedEvent += AttributeConstructor_AttributeConstructorCompletedEvent;
            List <UnitTestStep> executedSteps = new List <UnitTestStep>();

            try
            {
                Logger.IncreaseIndent();
                TransactionOptions op = new TransactionOptions();
                op.IsolationLevel = IsolationLevel.ReadCommitted;
                op.Timeout        = TransactionManager.MaximumTimeout;
                using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, op))
                {
                    for (int i = 0; i < this.Steps.Count; i++)
                    {
                        UnitTestStep step = this.Steps[i];

                        stepCount++;

                        try
                        {
                            Logger.IncreaseIndent();
                            Logger.WriteLine("Executing step {0}/{1}: {2} ", stepCount, this.steps.Count, step.Name);

                            executedSteps.Add(step);
                            step.Execute();
                        }
                        catch (EvaluationFailedException)
                        {
                            outcome.FailureStepNumber            = stepCount;
                            outcome.Result                       = UnitTestResult.Failed;
                            outcome.FailureStepName              = step.Name;
                            outcome.AdditionalFailureInformation = ((UnitTestStepObjectEvaluation)step).FailureReason;
                            this.RaiseCompleteEvent(outcome);

                            Logger.WriteLine(outcome.Result.ToString());
                            Logger.WriteLine(outcome.Description);
                            Logger.WriteLine(outcome.AdditionalFailureInformation);

                            if (UnitTestFile.BreakOnTestFailure)
                            {
                                throw new OperationCanceledException();
                            }

                            return(outcome);
                        }
                        catch (Exception ex)
                        {
                            Logger.WriteException(ex);
                            outcome.FailureStepNumber = stepCount;
                            outcome.FailureStepName   = step.Name;
                            outcome.Result            = UnitTestResult.Error;
                            outcome.Exception         = ex;
                            this.RaiseCompleteEvent(outcome);

                            if (UnitTestFile.BreakOnTestFailure)
                            {
                                throw new OperationCanceledException();
                            }

                            return(outcome);
                        }
                        finally
                        {
                            Logger.DecreaseIndent();
                        }
                    }
                }
            }
            finally
            {
                Logger.DecreaseIndent();
                AttributeConstructor.AttributeConstructorCompletedEvent -= AttributeConstructor_AttributeConstructorCompletedEvent;

                foreach (UnitTestStep step in executedSteps)
                {
                    try
                    {
                        step.Cleanup();
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteLine("Warning: An exception occurred during cleanup", LogLevel.Debug);
                        Logger.WriteException(ex, LogLevel.Debug);
                    }
                }
            }

            if (this.ExpectedConstructors.Count > 0)
            {
                outcome.MissingConstructors = this.ExpectedConstructors.Except(this.ExecutedConstructors).Where(t => !string.IsNullOrWhiteSpace(t)).ToList();

                if (outcome.MissingConstructors.Count > 0 && outcome.Result == UnitTestResult.Passed)
                {
                    outcome.Result = UnitTestResult.Inconclusive;
                    outcome.AdditionalFailureInformation =
                        string.Format("The following constructors did not execute: {0}", outcome.MissingConstructors.ToCommaSeparatedString());
                }
                else
                {
                    outcome.Result = UnitTestResult.Passed;
                }
            }
            else
            {
                outcome.Result = UnitTestResult.Passed;
            }

            if (outcome.Result == UnitTestResult.Passed && !this.Steps.Any(t => t is UnitTestStepObjectEvaluation))
            {
                outcome.Result = UnitTestResult.Inconclusive;
                outcome.AdditionalFailureInformation = "No object evaluations were present in the test";
            }

            this.RaiseCompleteEvent(outcome);
            return(outcome);
        }
Ejemplo n.º 2
0
        public IEnumerable <UnitTestStepObjectCreation> GetObjectCreationStepsBeforeItem(UnitTestStep step)
        {
            int index = this.Steps.IndexOf(step);

            for (int i = 0; i < index; i++)
            {
                if (this.Steps[i] is UnitTestStepObjectCreation)
                {
                    yield return(this.Steps[i] as UnitTestStepObjectCreation);
                }
            }
        }