예제 #1
0
        public void ToTestResultShouldReturnConvertedTestResultWithFieldsSet()
        {
            var stackTrace               = new StackTraceInformation("DummyStackTrace", "filePath", 2, 3);
            TestFailedException ex       = new TestFailedException(UnitTestOutcome.Error, "DummyMessage", stackTrace);
            var            dummyTimeSpan = new TimeSpan(20);
            UnitTestResult result        = new UnitTestResult(ex)
            {
                DisplayName = "DummyDisplayName",
                Duration    = dummyTimeSpan
            };

            TestCase testCase  = new TestCase("Foo", new Uri("Uri", UriKind.Relative), Assembly.GetExecutingAssembly().FullName);
            var      startTime = DateTimeOffset.Now;
            var      endTime   = DateTimeOffset.Now;

            // Act
            var testResult = result.ToTestResult(testCase, startTime, endTime, false);

            // Validate
            Assert.AreEqual(testCase, testResult.TestCase);
            Assert.AreEqual("DummyDisplayName", testResult.DisplayName);
            Assert.AreEqual(dummyTimeSpan, testResult.Duration);
            Assert.AreEqual(TestOutcome.Failed, testResult.Outcome);
            Assert.AreEqual("DummyMessage", testResult.ErrorMessage);
            Assert.AreEqual("DummyStackTrace", testResult.ErrorStackTrace);
            Assert.AreEqual(startTime, testResult.StartTime);
            Assert.AreEqual(endTime, testResult.EndTime);
            Assert.AreEqual(0, testResult.Messages.Count);
        }
예제 #2
0
        /// <summary>
        /// Handles the exception that is thrown by a test method. The exception can either
        /// be expected or not expected
        /// </summary>
        /// <param name="ex">Exception that was thrown</param>
        /// <param name="className">The class name.</param>
        /// <param name="methodName">The method name.</param>
        /// <returns>Test framework exception with details.</returns>
        private Exception HandleMethodException(Exception ex, string className, string methodName)
        {
            Debug.Assert(ex != null, "exception should not be null.");

            var isTargetInvocationException = ex is TargetInvocationException;

            if (isTargetInvocationException && ex.InnerException == null)
            {
                var errorMessage = string.Format(CultureInfo.CurrentCulture, Resource.UTA_FailedToGetTestMethodException, className, methodName);
                return(new TestFailedException(UnitTestOutcome.Error, errorMessage));
            }

            // Get the real exception thrown by the test method
            Exception realException = this.GetRealException(ex);

            if (realException is UnitTestAssertException)
            {
                return(new TestFailedException(
                           realException is AssertInconclusiveException ? UnitTestOutcome.Inconclusive : UnitTestOutcome.Failed,
                           realException.TryGetMessage(),
                           realException.TryGetStackTraceInformation(),
                           realException));
            }
            else
            {
                string errorMessage;

                // Handle special case of UI objects in TestMethod to suggest UITestMethod
                if (realException.HResult == -2147417842)
                {
                    errorMessage = string.Format(
                        CultureInfo.CurrentCulture,
                        Resource.UTA_WrongThread,
                        string.Format(CultureInfo.CurrentCulture, Resource.UTA_TestMethodThrows, className, methodName, StackTraceHelper.GetExceptionMessage(realException)));
                }
                else
                {
                    errorMessage = string.Format(
                        CultureInfo.CurrentCulture,
                        Resource.UTA_TestMethodThrows,
                        className,
                        methodName,
                        StackTraceHelper.GetExceptionMessage(realException));
                }

                StackTraceInformation stackTrace = null;

                // For ThreadAbortException (that can be thrown only by aborting a thread as there's no public constructor)
                // there's no inner exception and exception itself contains reflection-related stack trace
                // (_RuntimeMethodHandle.InvokeMethodFast <- _RuntimeMethodHandle.Invoke <- UnitTestExecuter.RunTestMethod)
                // which has no meaningful info for the user. Thus, we do not show call stack for ThreadAbortException.
                if (realException.GetType().Name != "ThreadAbortException")
                {
                    stackTrace = StackTraceHelper.GetStackTraceInformation(realException);
                }

                return(new TestFailedException(UnitTestOutcome.Failed, errorMessage, stackTrace, realException));
            }
        }
예제 #3
0
        public void IsUnitTestAssertExceptionReturnsFalseIfExceptionIsNotAssertException()
        {
            var exception = new NotImplementedException();

            UTF.UnitTestOutcome outcome          = UTF.UnitTestOutcome.Unknown;
            string exceptionMessage              = null;
            StackTraceInformation stackTraceInfo = null;

            Assert.IsFalse(exception.TryGetUnitTestAssertException(out outcome, out exceptionMessage, out stackTraceInfo));
        }
예제 #4
0
        public void IsUnitTestAssertExceptionReturnsTrueIfExceptionIsAssertException()
        {
            var exception = new UTF.AssertInconclusiveException();

            UTF.UnitTestOutcome outcome          = UTF.UnitTestOutcome.Unknown;
            string exceptionMessage              = null;
            StackTraceInformation stackTraceInfo = null;

            Assert.IsTrue(exception.TryGetUnitTestAssertException(out outcome, out exceptionMessage, out stackTraceInfo));
        }
예제 #5
0
        private bool RunTestInitializeMethod(object classInstance, TestResult result)
        {
            Debug.Assert(classInstance != null, "classInstance != null");
            Debug.Assert(result != null, "result != null");

            MethodInfo testInitializeMethod = null;

            try
            {
                // TestInitialize methods for base classes are called in reverse order of discovery
                // Grandparent -> Parent -> Child TestClass
                var baseTestInitializeStack = new Stack <MethodInfo>(this.Parent.BaseTestInitializeMethodsQueue);
                while (baseTestInitializeStack.Count > 0)
                {
                    testInitializeMethod = baseTestInitializeStack.Pop();
                    testInitializeMethod?.InvokeAsSynchronousTask(classInstance, null);
                }

                testInitializeMethod = this.Parent.TestInitializeMethod;
                testInitializeMethod?.InvokeAsSynchronousTask(classInstance, null);

                return(true);
            }
            catch (Exception ex)
            {
                var    innerException   = ex.GetInnerExceptionOrDefault();
                string exceptionMessage = null;
                StackTraceInformation exceptionStackTraceInfo = null;
                var outcome = TestTools.UnitTesting.UnitTestOutcome.Failed;

                if (innerException.TryGetUnitTestAssertException(out outcome, out exceptionMessage, out exceptionStackTraceInfo))
                {
                    result.Outcome = outcome;
                    result.TestFailureException = new TestFailedException(
                        UnitTestOutcome.Failed,
                        exceptionMessage,
                        exceptionStackTraceInfo);
                }
                else
                {
                    var stackTrace   = StackTraceHelper.GetStackTraceInformation(innerException);
                    var errorMessage = string.Format(
                        CultureInfo.CurrentCulture,
                        Resource.UTA_InitMethodThrows,
                        this.TestClassName,
                        testInitializeMethod?.Name,
                        StackTraceHelper.GetExceptionMessage(innerException));

                    result.Outcome = TestTools.UnitTesting.UnitTestOutcome.Failed;
                    result.TestFailureException = new TestFailedException(UnitTestOutcome.Failed, errorMessage, stackTrace);
                }
            }

            return(false);
        }
예제 #6
0
        public void IsUnitTestAssertExceptionSetsOutcomeAsFailedIfAssertFailedException()
        {
            var exception = new UTF.AssertFailedException("Dummy Message", new NotImplementedException("notImplementedException"));

            UTF.UnitTestOutcome outcome          = UTF.UnitTestOutcome.Unknown;
            string exceptionMessage              = null;
            StackTraceInformation stackTraceInfo = null;

            exception.TryGetUnitTestAssertException(out outcome, out exceptionMessage, out stackTraceInfo);

            Assert.AreEqual(UTF.UnitTestOutcome.Failed, outcome);
            Assert.AreEqual("Dummy Message", exceptionMessage);
        }
예제 #7
0
        public void UnitTestResultConstrutorWithTestFailedExceptionShouldSetRequiredFields()
        {
            var stackTrace         = new StackTraceInformation("trace", "filePath", 2, 3);
            TestFailedException ex = new TestFailedException(UnitTestOutcome.Error, "DummyMessage", stackTrace);

            UnitTestResult result = new UnitTestResult(ex);

            Assert.AreEqual(UnitTestOutcome.Error, result.Outcome);
            Assert.AreEqual("DummyMessage", result.ErrorMessage);
            Assert.AreEqual("trace", result.ErrorStackTrace);
            Assert.AreEqual("filePath", result.ErrorFilePath);
            Assert.AreEqual(2, result.ErrorLineNumber);
            Assert.AreEqual(3, result.ErrorColumnNumber);
        }
예제 #8
0
        public void RunAssemblyInitialize(TestContext testContext)
        {
            // No assembly initialize => nothing to do.
            if (this.AssemblyInitializeMethod == null)
            {
                return;
            }

            if (testContext == null)
            {
                throw new NullReferenceException(Resource.TestContextIsNull);
            }

            // If assembly initialization is not done, then do it.
            if (!this.IsAssemblyInitializeExecuted)
            {
                // Acquiring a lock is usually a costly operation which does not need to be
                // performed every time if the assembly init is already executed.
                lock (this.assemblyInfoExecuteSyncObject)
                {
                    // Perform a check again.
                    if (!this.IsAssemblyInitializeExecuted)
                    {
                        try
                        {
                            this.AssemblyInitializeMethod.InvokeAsSynchronousTask(null, testContext);
                        }
                        catch (Exception ex)
                        {
                            this.AssemblyInitializationException = ex;
                        }
                        finally
                        {
                            this.IsAssemblyInitializeExecuted = true;
                        }
                    }
                }
            }

            // If assemblyInitialization was successful, then don't do anything
            if (this.AssemblyInitializationException == null)
            {
                return;
            }

            // Cache and return an already created TestFailedException.
            if (this.AssemblyInitializationException is TestFailedException)
            {
                throw this.AssemblyInitializationException;
            }

            var realException = this.AssemblyInitializationException.InnerException
                                ?? this.AssemblyInitializationException;

            var    outcome      = UnitTestOutcome.Failed;
            string errorMessage = null;
            StackTraceInformation stackTraceInfo = null;

            if (!realException.TryGetUnitTestAssertException(out outcome, out errorMessage, out stackTraceInfo))
            {
                var exception = realException.GetType().ToString();
                var message   = StackTraceHelper.GetExceptionMessage(realException);
                errorMessage = string.Format(
                    CultureInfo.CurrentCulture,
                    Resource.UTA_AssemblyInitMethodThrows,
                    this.AssemblyInitializeMethod.DeclaringType.FullName,
                    this.AssemblyInitializeMethod.Name,
                    exception,
                    message);
                stackTraceInfo = StackTraceHelper.GetStackTraceInformation(realException);
            }

            var testFailedException = new TestFailedException(outcome, errorMessage, stackTraceInfo);

            this.AssemblyInitializationException = testFailedException;

            throw testFailedException;
        }
예제 #9
0
        private void RunTestCleanupMethod(object classInstance, TestResult result)
        {
            Debug.Assert(classInstance != null, "classInstance != null");
            Debug.Assert(result != null, "result != null");

            var testCleanupMethod = this.Parent.TestCleanupMethod;

            try
            {
                try
                {
                    // Test cleanups are called in the order of discovery
                    // Current TestClass -> Parent -> Grandparent
                    testCleanupMethod?.InvokeAsSynchronousTask(classInstance, null);
                    var baseTestCleanupQueue = new Queue <MethodInfo>(this.Parent.BaseTestCleanupMethodsQueue);
                    while (baseTestCleanupQueue.Count > 0)
                    {
                        testCleanupMethod = baseTestCleanupQueue.Dequeue();
                        testCleanupMethod?.InvokeAsSynchronousTask(classInstance, null);
                    }
                }
                finally
                {
                    (classInstance as IDisposable)?.Dispose();
                }
            }
            catch (Exception ex)
            {
                var cleanupOutcome    = UTF.UnitTestOutcome.Failed;
                var cleanupError      = new StringBuilder();
                var cleanupStackTrace = new StringBuilder();
                StackTraceInformation cleanupStackTraceInfo = null;

                TestFailedException testFailureException = result.TestFailureException as TestFailedException;
                testFailureException.TryGetTestFailureExceptionMessageAndStackTrace(cleanupError, cleanupStackTrace);

                if (cleanupStackTrace.Length > 0)
                {
                    cleanupStackTrace.Append(Resource.UTA_CleanupStackTrace);
                    cleanupStackTrace.Append(Environment.NewLine);
                }

                Exception             realException               = ex.GetInnerExceptionOrDefault();
                string                exceptionMessage            = null;
                StackTraceInformation realExceptionStackTraceInfo = null;

                // special case UnitTestAssertException to trim off part of the stack trace
                if (!realException.TryGetUnitTestAssertException(out cleanupOutcome, out exceptionMessage, out realExceptionStackTraceInfo))
                {
                    cleanupOutcome              = UTF.UnitTestOutcome.Failed;
                    exceptionMessage            = this.GetTestCleanUpExceptionMessage(testCleanupMethod, realException);
                    realExceptionStackTraceInfo = realException.TryGetStackTraceInformation();
                }

                cleanupError.Append(exceptionMessage);
                if (realExceptionStackTraceInfo != null)
                {
                    cleanupStackTrace.Append(realExceptionStackTraceInfo.ErrorStackTrace);
                    cleanupStackTraceInfo = cleanupStackTraceInfo ?? realExceptionStackTraceInfo;
                }

                UTF.UnitTestOutcome   outcome             = testFailureException == null ? cleanupOutcome : cleanupOutcome.GetMoreImportantOutcome(result.Outcome);
                StackTraceInformation finalStackTraceInfo = cleanupStackTraceInfo != null ?
                                                            new StackTraceInformation(
                    cleanupStackTrace.ToString(),
                    cleanupStackTraceInfo.ErrorFilePath,
                    cleanupStackTraceInfo.ErrorLineNumber,
                    cleanupStackTraceInfo.ErrorColumnNumber) :
                                                            new StackTraceInformation(cleanupStackTrace.ToString());

                result.Outcome = outcome;
                result.TestFailureException = new TestFailedException(outcome.ToUnitTestOutcome(), cleanupError.ToString(), finalStackTraceInfo);
            }
        }
예제 #10
0
        public void RunClassInitialize(TestContext testContext)
        {
            // If no class initialize return
            if (this.ClassInitializeMethod == null)
            {
                return;
            }

            if (testContext == null)
            {
                throw new NullReferenceException(Resource.TestContextIsNull);
            }

            // If class initialization is not done, then do it.
            if (!this.IsClassInitializeExecuted)
            {
                try
                {
                    this.ClassInitializeMethod.InvokeAsSynchronousTask(null, testContext);
                }
                catch (Exception ex)
                {
                    this.ClassInitializationException = ex;
                }
                finally
                {
                    this.IsClassInitializeExecuted = true;
                }
            }

            // If classInitialization was successful, then dont do anything
            if (this.ClassInitializationException == null)
            {
                return;
            }

            if (this.ClassInitializationException is TestFailedException)
            {
                throw this.ClassInitializationException;
            }

            // Fail the current test if it was a failure.
            var realException = this.ClassInitializationException.InnerException ?? this.ClassInitializationException;

            var    outcome      = UnitTestOutcome.Failed;
            string errorMessage = null;
            StackTraceInformation exceptionStackTraceInfo = null;

            if (!realException.TryGetUnitTestAssertException(out outcome, out errorMessage, out exceptionStackTraceInfo))
            {
                errorMessage = string.Format(
                    CultureInfo.CurrentCulture,
                    Resource.UTA_ClassInitMethodThrows,
                    this.ClassType.FullName,
                    this.ClassInitializeMethod.Name,
                    realException.GetType().ToString(),
                    StackTraceHelper.GetExceptionMessage(realException));

                exceptionStackTraceInfo = realException.TryGetStackTraceInformation();
            }

            var testFailedException = new TestFailedException(outcome, errorMessage, exceptionStackTraceInfo);

            this.ClassInitializationException = testFailedException;

            throw testFailedException;
        }
예제 #11
0
        public void RunAssemblyInitialize(TestContext testContext)
        {
            // No assembly initialize => nothing to do.
            if (this.AssemblyInitializeMethod == null)
            {
                return;
            }

            if (testContext == null)
            {
                throw new NullReferenceException(Resource.TestContextIsNull);
            }

            // If assembly initialization is not done, then do it.
            if (!this.IsAssemblyInitializeExecuted)
            {
                try
                {
                    this.AssemblyInitializeMethod.InvokeAsSynchronousTask(null, testContext);
                }
                catch (Exception ex)
                {
                    this.AssemblyInitializationException = ex;
                }
                finally
                {
                    this.IsAssemblyInitializeExecuted = true;
                }
            }

            // If assemblyInitialization was successful, then dont do anything
            if (this.AssemblyInitializationException == null)
            {
                return;
            }

            // Cache and return an already created TestFailedException.
            if (this.AssemblyInitializationException is TestFailedException)
            {
                throw this.AssemblyInitializationException;
            }

            var realException = this.AssemblyInitializationException.InnerException
                                ?? this.AssemblyInitializationException;

            var    outcome      = UnitTestOutcome.Failed;
            string errorMessage = null;
            StackTraceInformation stackTraceInfo = null;

            if (realException is UnitTestAssertException)
            {
                outcome = realException is AssertInconclusiveException ?
                          UnitTestOutcome.Inconclusive : UnitTestOutcome.Failed;

                errorMessage   = realException.TryGetMessage();
                stackTraceInfo = realException.TryGetStackTraceInformation();
            }
            else
            {
                var exception = realException.GetType().ToString();
                var message   = StackTraceHelper.GetExceptionMessage(realException);
                errorMessage = string.Format(
                    CultureInfo.CurrentCulture,
                    Resource.UTA_AssemblyInitMethodThrows,
                    this.AssemblyInitializeMethod.DeclaringType.FullName,
                    this.AssemblyInitializeMethod.Name,
                    exception,
                    message);
                stackTraceInfo = StackTraceHelper.GetStackTraceInformation(realException);
            }

            var testFailedException = new TestFailedException(outcome, errorMessage, stackTraceInfo);

            this.AssemblyInitializationException = testFailedException;

            throw testFailedException;
        }
예제 #12
0
        /// <summary>
        /// Checks whether exception is an Assert exception
        /// </summary>
        /// <param name="exception">An <see cref="Exception"/> instance.</param>
        /// <param name="outcome"> Adapter's Outcome depending on type of assertion.</param>
        /// <param name="exceptionMessage">Exception message.</param>
        /// <param name="exceptionStackTrace">StackTraceInformation for the exception</param>
        /// <returns>True, if Assert exception. False, otherwise.</returns>
        internal static bool TryGetUnitTestAssertException(this Exception exception, out UnitTestOutcome outcome, out string exceptionMessage, out StackTraceInformation exceptionStackTrace)
        {
            if (exception is UTF.UnitTestAssertException)
            {
                outcome = exception is UTF.AssertInconclusiveException ?
                          UnitTestOutcome.Inconclusive : UnitTestOutcome.Failed;

                exceptionMessage    = exception.TryGetMessage();
                exceptionStackTrace = exception.TryGetStackTraceInformation();
                return(true);
            }
            else
            {
                outcome             = UnitTestOutcome.Failed;
                exceptionMessage    = null;
                exceptionStackTrace = null;
                return(false);
            }
        }