Exemple #1
0
        /// <summary>
        /// Executes the test method. Creates a new instance of the class
        /// under tests and passes it to the inner command. Also catches
        /// any exceptions and converts them into <see cref="FailedResult"/>s.
        /// </summary>
        /// <param name="testClass">The instance of the test class</param>
        /// <returns>Returns information about the test run</returns>
        public override MethodResult Execute(object testClass)
        {
            try
            {
                if (testClass == null)
                {
                    testClass = method.CreateInstance();
                }
            }
            catch (TargetInvocationException ex)
            {
                ExceptionUtility.RethrowWithNoStackTraceLoss(ex.InnerException);
            }

            try
            {
                return(InnerCommand.Execute(testClass));
            }
            finally
            {
                IDisposable disposable = testClass as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
        }
 public override MethodResult Execute(object testClass)
 {
     try
     {
         return(InnerCommand.Execute(testClass));
     }
     catch
     {
         owner.OnFailure();
         throw;
     }
 }
Exemple #3
0
        /// <summary>
        /// Executes the inner test method, gathering the amount of time it takes to run.
        /// </summary>
        /// <returns>Returns information about the test run</returns>
        public override MethodResult Execute(object testClass)
        {
            TestTimer timer = new TestTimer();

            timer.Start();
            MethodResult methodResult = InnerCommand.Execute(testClass);

            timer.Stop();

            methodResult.ExecutionTime = timer.ElapsedMilliseconds / 1000.00;

            return(methodResult);
        }
Exemple #4
0
                private MethodResult ExecuteInternal(object testClass)
                {
                    Exception    exception    = null;
                    MethodResult result       = null;
                    MethodResult failedResult = null;

                    for (int i = 0; i < _retries + 1; ++i)
                    {
                        try
                        {
                            result = InnerCommand.Execute(testClass);
                            if (result is FailedResult)
                            {
                                TraceIf(_retries > 0, "Retry {0}/{1} failed.", i, _retries);
                                failedResult = (FailedResult)result;
                            }
                            else
                            {
                                TraceIf(_retries > 0 && i > 0, "Retry {0}/{1} passed successfully.", i, _retries);
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            TraceIf(_retries > 0, "Retry {0}/{1} failed with {2}", i, _retries, ex);

                            // optimize to preserve stacktrace
                            if (i >= _retries)
                            {
                                throw;
                            }

                            if (exception == null)
                            {
                                exception = ex;
                            }
                        }
                    }

                    if (_suppressError && result is PassedResult)
                    {
                        return(result);
                    }

                    if (exception != null)
                    {
                        ExceptionUtility.RethrowWithNoStackTraceLoss(exception);
                    }

                    return(failedResult ?? result);
                }
Exemple #5
0
        public override MethodResult Execute(object testClass)
        {
            MethodResult result = null;

            try
            {
                result = InnerCommand.Execute(testClass);
            }
            catch (Exception ex)
            {
                result = new FailedResult(_method, ex, DisplayName);
            }

            return(result);
        }
Exemple #6
0
            public override MethodResult Execute(object testClass)
            {
                var task = Task.Factory.StartNew(() =>
                {
                    try
                    {
                        using (testClass as IDisposable)
                            return(InnerCommand.Execute(testClass));
                    }
                    catch (Exception ex)
                    {
                        return(new FailedResult(_testMethod, ex, DisplayName));
                    }
                });

                return(task.Result);
            }
Exemple #7
0
        /// <inheritdoc/>
        public override MethodResult Execute(object testClass)
        {
            MethodResult         result       = null;
            List <TraceListener> oldListeners = new List <TraceListener>();
            TextWriter           oldOut       = Console.Out;
            TextWriter           oldError     = Console.Error;

            using (StringWriter outputWriter = new StringWriter())
            {
                try
                {
                    foreach (TraceListener oldListener in Trace.Listeners)
                    {
                        oldListeners.Add(oldListener);
                    }

                    Trace.Listeners.Clear();
                    Trace.Listeners.Add(new AssertTraceListener());
                    Trace.Listeners.Add(new TextWriterTraceListener(outputWriter));

                    Console.SetOut(outputWriter);
                    Console.SetError(outputWriter);

                    result = InnerCommand.Execute(testClass);
                }
                catch (Exception ex)
                {
                    result = new FailedResult(method, ex, DisplayName);
                }
                finally
                {
                    Console.SetOut(oldOut);
                    Console.SetError(oldError);

                    try
                    {
                        Trace.Listeners.Clear();
                        Trace.Listeners.AddRange(oldListeners.ToArray());
                    }
                    catch (Exception) { }
                }

                result.Output = outputWriter.ToString();
                return(result);
            }
        }
Exemple #8
0
        /// <summary>
        /// Executes the test method, failing if it takes too long.
        /// </summary>
        /// <returns>Returns information about the test run</returns>
        public override MethodResult Execute(object testClass)
        {
            WorkerThreadHandler timedMethod = () => { return(InnerCommand.Execute(testClass)); };

            IAsyncResult asyncResult = timedMethod.BeginInvoke(null, null);
            MethodResult methodResult;

            if (!asyncResult.AsyncWaitHandle.WaitOne(Timeout, false))
            {
                methodResult = new FailedResult(testMethod, new TimeoutException(Timeout), DisplayName);
            }
            else
            {
                methodResult = timedMethod.EndInvoke(asyncResult);
            }

            return(methodResult);
        }
        public override MethodResult Execute(object testClass)
        {
            MethodResult result;

            try
            {
                result = InnerCommand.Execute(testClass);
            }
            catch (TargetInvocationException ex)
            {
                result = new ExceptionResult(method, ex.InnerException, DisplayName);
            }
            catch (Exception ex)
            {
                result = new ExceptionResult(method, ex, DisplayName);
            }

            return(result);
        }
        public override MethodResult Execute(object testClass)
        {
            List <BeforeAfterTestAttribute> beforeCalled = new List <BeforeAfterTestAttribute>();
            bool testExceptionThrown = false;

            try
            {
                foreach (BeforeAfterTestAttribute attr in testMethod.GetCustomAttributes(typeof(BeforeAfterTestAttribute), true))
                {
                    attr.Before(testMethod);
                    beforeCalled.Add(attr);
                }

                return(InnerCommand.Execute(testClass));
            }
            catch
            {
                testExceptionThrown = true;
                throw;
            }
            finally
            {
                List <Exception> afterExceptions = new List <Exception>();
                beforeCalled.Reverse();

                foreach (BeforeAfterTestAttribute attr in beforeCalled)
                {
                    try
                    {
                        attr.After(testMethod);
                    }
                    catch (Exception ex)
                    {
                        afterExceptions.Add(ex);
                    }
                }

                if (!testExceptionThrown && afterExceptions.Count > 0)
                {
                    throw new AfterTestException(afterExceptions);
                }
            }
        }
        /// <summary>
        /// Sets the fixtures on the test class by calling SetFixture, then
        /// calls the inner command.
        /// </summary>
        /// <param name="testClass">The instance of the test class</param>
        /// <returns>Returns information about the test run</returns>
        public override MethodResult Execute(object testClass)
        {
            try
            {
                if (testClass != null)
                {
                    foreach (KeyValuePair <MethodInfo, object> fixture in fixtures)
                    {
                        fixture.Key.Invoke(testClass, new object[] { fixture.Value });
                    }
                }
            }
            catch (TargetInvocationException ex)
            {
                ExceptionUtility.RethrowWithNoStackTraceLoss(ex.InnerException);
            }

            return(InnerCommand.Execute(testClass));
        }
Exemple #12
0
        public override MethodResult Execute(object testClass)
        {
            /*
             * We have to do timeout ourselves, because xunit can't handle the fact that it's own TimeOutCommand returns exceptions with null stack traces
             * It also leaves the method executing, which hangs the build.
             */

            return(WithRetry(3, 6,
                             delegate
            {
                try
                {
                    return ManualTimeout.ExecuteWithTimeout(() => InnerCommand.Execute(testClass));
                }
                finally
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }));
        }
                private MethodResult ExecuteInternal(object testClass)
                {
                    Exception    exception    = null;
                    MethodResult result       = null;
                    MethodResult failedResult = null;
                    string       retryMessage = null;

                    for (int i = 0; i < _retries + 1; ++i)
                    {
                        try
                        {
                            result = InnerCommand.Execute(testClass);
                            if (result is FailedResult)
                            {
                                TraceIf(_retries > 0, "Retry {0}/{1} failed.", i, _retries);
                                failedResult = (FailedResult)result;
                            }
                            else
                            {
                                retryMessage = String.Format("Retry {0}/{1} passed successfully.", i, _retries);
                                TraceIf(_retries > 0 && i > 0, retryMessage);
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            TraceIf(_retries > 0, "Retry {0}/{1} failed with {2}", i, _retries, ex);

                            // optimize to preserve stacktrace
                            if (i >= _retries)
                            {
                                throw;
                            }

                            if (exception == null)
                            {
                                exception = ex;
                            }
                        }
                    }

                    if (_suppressError && result is PassedResult)
                    {
                        return(result);
                    }

                    if (exception != null)
                    {
                        if (String.IsNullOrEmpty(retryMessage))
                        {
                            ExceptionUtility.RethrowWithNoStackTraceLoss(exception);
                        }

                        if (failedResult == null)
                        {
                            failedResult = new FailedResult(_testMethod, new RetrySuccessfulException(retryMessage, exception), DisplayName);
                        }
                    }

                    return(failedResult ?? result);
                }