Пример #1
0
 private void RaiseCompleteEvent(UnitTestOutcome outcome)
 {
     if (UnitTestCompletedEvent != null)
     {
         UnitTestCompletedEvent(this, outcome);
     }
 }
Пример #2
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);
        }