示例#1
0
        private TestOutcome HandleAbort(MarkupDocumentWriter markupDocumentWriter, string actionDescription, ThreadAbortException ex)
        {
            TestOutcome outcome = abortOutcome.Value;

            if (ex == null && alreadyLoggedAbortOnce)
            {
                return(outcome);
            }

            alreadyLoggedAbortOnce = true;
            LogMessage(markupDocumentWriter, actionDescription, outcome, abortMessage, null);
            return(outcome);
        }
示例#2
0
        private static MarkupStreamWriter GetLogStreamWriterForOutcome(MarkupDocumentWriter markupDocumentWriter, TestOutcome outcome)
        {
            switch (outcome.Status)
            {
            case TestStatus.Passed:
                return(markupDocumentWriter.Default);

            case TestStatus.Failed:
                return(markupDocumentWriter.Failures);

            default:
                return(markupDocumentWriter.Warnings);
            }
        }
            private void HandleTestOrSuiteFinished(NUnit.Core.TestResult nunitResult)
            {
                if (testContextStack.Count == 0)
                {
                    return;
                }

                ITestContext testContext = testContextStack.Peek();
                NUnitTest    test        = (NUnitTest)testContext.TestStep.Test;

                if (test.Test.TestName != nunitResult.Test.TestName)
                {
                    return;
                }

                testContextStack.Pop();

                progressMonitor.Worked(1);

                string logStreamName = nunitResult.ResultState == ResultState.Success ? MarkupStreamNames.Warnings : MarkupStreamNames.Failures;

                MarkupDocumentWriter logWriter = testContext.LogWriter;

                if (nunitResult.Message != null)
                {
                    using (logWriter[logStreamName].BeginSection(Resources.NUnitTestController_ResultMessageSectionName))
                        logWriter[logStreamName].Write(nunitResult.Message);
                }
                if (nunitResult.StackTrace != null)
                {
                    using (logWriter[logStreamName].BeginSection(Resources.NUnitTestController_ResultStackTraceSectionName))
                        using (logWriter[logStreamName].BeginMarker(Marker.StackTrace))
                            logWriter[logStreamName].Write(nunitResult.StackTrace);
                }

                testContext.AddAssertCount(nunitResult.AssertCount);
                TestResult result = testContext.FinishStep(CreateOutcomeFromResult(nunitResult), null);

                if (testContextStack.Count == 0)
                {
                    topResult = result;
                }
            }
示例#4
0
        private static void LogMessage(MarkupDocumentWriter markupDocumentWriter, string actionDescription, TestOutcome outcome, string message, Exception ex)
        {
            if (string.IsNullOrEmpty(message) && ex == null)
            {
                return;
            }

            MarkupStreamWriter stream = GetLogStreamWriterForOutcome(markupDocumentWriter, outcome);

            using (actionDescription != null ? stream.BeginSection(actionDescription) : null)
            {
                if (!string.IsNullOrEmpty(message))
                {
                    stream.WriteLine(message);
                }

                if (ex != null)
                {
                    stream.WriteException(StackTraceFilter.FilterException(ex));
                }
            }
        }
示例#5
0
 protected virtual void PrepareLogWriterForWriteToTest(MarkupDocumentWriter writer)
 {
 }
示例#6
0
        public TestOutcome Run(MarkupDocumentWriter markupDocumentWriter, Action action, string description)
        {
            // NOTE: This method has been optimized to minimize the total stack depth of the action
            //       by inlining blocks on the critical path that had previously been factored out.

            if (markupDocumentWriter == null)
            {
                throw new ArgumentNullException("markupDocumentWriter");
            }
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            ThreadAbortScope scope = null;

            try
            {
                lock (syncRoot)
                {
                    ThrowIfDisposed();

                    if (!abortOutcome.HasValue)
                    {
                        if (scopesAndThreads == null)
                        {
                            scopesAndThreads = new List <Pair <ThreadAbortScope, Thread> >();
                        }

                        scope = new ThreadAbortScope();
                        scopesAndThreads.Add(new Pair <ThreadAbortScope, Thread>(scope, Thread.CurrentThread));
                    }
                }

                if (scope == null)
                {
                    return(HandleAbort(markupDocumentWriter, description, null));
                }

                // Run the action within the scope we have acquired.
                try
                {
                    ThreadAbortException ex = scope.Run(action);
                    if (ex != null)
                    {
                        return(HandleAbort(markupDocumentWriter, description, ex));
                    }

                    return(TestOutcome.Passed);
                }
                catch (Exception ex)
                {
                    // If the test itself threw a thread abort, not because we aborted it
                    // ourselves but most likely due to a bug in the test subject, then we
                    // prevent the abort from bubbling up any further.
                    if (ex is ThreadAbortException &&
                        !AppDomain.CurrentDomain.IsFinalizingForUnload())
                    {
                        Thread.ResetAbort();
                    }

                    TestOutcome   outcome;
                    TestException testException = ex as TestException;
                    if (testException != null)
                    {
                        outcome = testException.Outcome;

                        if (testException.ExcludeStackTrace)
                        {
                            LogMessage(markupDocumentWriter, description, outcome, testException.HasNonDefaultMessage ? testException.Message : null, null);
                        }
                        else
                        {
                            LogMessage(markupDocumentWriter, description, outcome, null, testException);
                        }
                    }
                    else
                    {
                        outcome = TestOutcome.Failed;
                        LogMessage(markupDocumentWriter, description, outcome, null, ex);
                    }

                    return(outcome);
                }
            }
            finally
            {
                if (scope != null)
                {
                    lock (syncRoot)
                    {
                        if (scopesAndThreads != null)
                        {
                            for (int i = 0; i < scopesAndThreads.Count; i++)
                            {
                                if (scopesAndThreads[i].First == scope)
                                {
                                    scopesAndThreads.RemoveAt(i);
                                }
                            }
                        }
                    }
                }
            }
        }
示例#7
0
 protected override void PrepareLogWriterForWriteToTest(MarkupDocumentWriter writer)
 {
     writer.AttachPlainText("attachment1", "abc");
     writer.AttachPlainText("attachment2", "def");
 }