Beispiel #1
0
 private void InvokeStep(ITestRunStep step, TestRunExecutionContext context, TestRunResult result,
                         ITestRunLogger logger)
 {
     context.Step = step;
     try
     {
         step.Run(context);
     }
     catch (TestRunAssertException)
     {
         result.AddStep(new TestRunStepResult(context.StepNumber, step.ToString(), false));
         throw;
     }
     catch (Exception ex)
     {
         result.IsSuccess = false;
         if (context.FirstException == null)
         {
             result.FailException   = ex;
             context.FirstException = ex;
         }
         else
         {
             context.LastException = ex;
         }
     }
     result.AddStep(new TestRunStepResult(context.StepNumber, step.ToString(), result.IsSuccess));
 }
Beispiel #2
0
        private TestRunResult RunInternal(TestRun testRun, ITestRunLogger logger,
                                          out TestRunExecutionContext context)
        {
            if (testRun == null)
            {
                throw new ArgumentNullException(nameof(testRun));
            }
            if (logger == null)
            {
                logger = new ConsoleTestRunLogger();
            }

            logger.LogWithTime($"Started to run \"{testRun.Name}\" test.");
            context = new TestRunExecutionContext
            {
                StartedAt = DateTime.Now,
                Logger    = logger
            };

            Initialize(context);

            context.Logger.LogWithTime("Run OnBeforeTestRun.");
            OnBeforeTestRun(context);
            var result = new TestRunResult(testRun.Name);

            context.Logger.LogWithTime("Run Test.");
            InvokeTestRun(testRun, context, result, logger);

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Run test run.
        /// </summary>
        /// <param name="testRun">Test run.</param>
        /// <param name="logger">Logger. Console is used by default.</param>
        /// <returns>Test run result.</returns>
        public virtual TestRunResult Run(TestRun testRun, ITestRunLogger logger = null)
        {
            TestRunExecutionContext context = null;

            try
            {
                return(RunInternal(testRun, logger, out context));
            }
            finally
            {
                context?.Logger.LogWithTime("Run OnAfterTestRun.");
                OnAfterTestRun(context);
            }
        }
Beispiel #4
0
 private void Initialize(TestRunExecutionContext context)
 {
     if (!isInitialized)
     {
         lock (SyncRoot)
         {
             if (!isInitialized)
             {
                 context.Logger.LogWithTime("Run initialization.");
                 OnInitialize(context);
                 isInitialized = true;
             }
         }
     }
 }
Beispiel #5
0
        /// <summary>
        /// Run test run with assert method. OnAfterTestRun will be called afterwards.
        /// </summary>
        /// <param name="testRun">Test run.</param>
        /// <param name="assert">Assert method.</param>
        /// <param name="logger">Logger. Console is used by default.</param>
        /// <returns>Test run result.</returns>
        public virtual TestRunResult RunWithAssert(
            TestRun testRun,
            Action <TestRun, TestRunResult> assert,
            ITestRunLogger logger = null)
        {
            TestRunExecutionContext context = null;

            try
            {
                var result = RunInternal(testRun, logger, out context);
                context.Logger.LogWithTime("Run asserts.");
                assert(testRun, result);
                return(result);
            }
            finally
            {
                context?.Logger.LogWithTime("Run OnAfterTestRun.");
                OnAfterTestRun(context);
            }
        }
Beispiel #6
0
 private void InvokeTestRun(TestRun testRun, TestRunExecutionContext context, TestRunResult result,
                            ITestRunLogger logger)
 {
     context.TestRun         = testRun;
     context.ServiceProvider = ServiceProviderFactory.Create();
     OnBeforeTestRun(context);
     try
     {
         int stepNumber = 1;
         foreach (ITestRunStep testRunStep in testRun.Steps)
         {
             context.StepNumber = stepNumber++;
             InvokeStep(testRunStep, context, result, logger);
         }
     }
     finally
     {
         context.Step = null;
         var disposableServiceProvider = context.ServiceProvider as IDisposable;
         disposableServiceProvider?.Dispose();
         OnAfterTestRun(context);
     }
 }
Beispiel #7
0
 /// <summary>
 /// The method is called after all test were done.
 /// </summary>
 /// <param name="context">Test run execution context. It may be null if called from
 /// Dispose method.</param>
 public virtual void OnShutdown(TestRunExecutionContext context)
 {
 }
Beispiel #8
0
 /// <summary>
 /// The method is called after every test run. Here you can implement all
 /// finalization steps (connections close, rollback, etc).
 /// </summary>
 /// <param name="context">Test run execution context.</param>
 public virtual void OnAfterTestRun(TestRunExecutionContext context)
 {
 }
Beispiel #9
0
 /// <summary>
 /// The method is called before every test run. Here you can implement all
 /// preparation steps for test, for example init test data.
 /// </summary>
 /// <param name="context">Test run execution context.</param>
 public virtual void OnBeforeTestRun(TestRunExecutionContext context)
 {
 }
Beispiel #10
0
 /// <summary>
 /// Setup and initialize tests environment. Setup context resolver if needed.
 /// </summary>
 /// <param name="context">Test run execution context.</param>
 public virtual void OnInitialize(TestRunExecutionContext context)
 {
 }
Beispiel #11
0
 /// <summary>
 /// Throw exception and format it with current execution context.
 /// </summary>
 /// <param name="message">Message to display to user.</param>
 /// <param name="context">Current test execution context.</param>
 /// <param name="step">Test run step instance.</param>
 public static void ThrowWithExecutionContext(string message, TestRunExecutionContext context, ITestRunStep step)
 {
     throw new TestRunAssertException(message);
 }