Esempio n. 1
0
        public void ConvertExceptionToVSTestResult()
        {
            LogEntryException exception = new LogEntryException("C string: some error", new SourceFileInfo("unknown location", 0));

            exception.LastCheckpoint   = new SourceFileInfo("boostunittestsample.cpp", 13);
            exception.CheckpointDetail = "Going to throw an exception";

            BoostTestResult testCaseResult = new BoostTestResultBuilder().
                                             For(this.TestCase).
                                             Aborted().
                                             Duration(0).
                                             Log(exception).
                                             Build();

            VSTestResult result = testCaseResult.AsVSTestResult(this.TestCase);

            AssertVSTestModelProperties(result);

            Assert.That(result.Outcome, Is.EqualTo(TestOutcome.Failed));

            // A 0 duration should list as 1 tick to avoid UI issues in the test adapter
            Assert.That(result.Duration, Is.EqualTo(TimeSpan.FromTicks(1)));

            AssertVsTestModelError(result, exception);

            Assert.That(result.Messages.Count, Is.EqualTo(1));

            TestResultMessage message = result.Messages.First();

            Assert.That(message.Category, Is.EqualTo(TestResultMessage.StandardErrorCategory));
        }
        /// <summary>
        /// Formats a LogEntryException to append to test result string
        /// </summary>
        /// <param name="exception">The exception to format</param>
        /// <param name="sb">The StringBuilder which will host the output</param>
        /// <returns>sb</returns>
        private static StringBuilder FormatException(LogEntryException exception, StringBuilder sb)
        {
            if (exception.LastCheckpoint != null)
            {
                sb.Append(Environment.NewLine);
                AppendSourceInfo(exception.LastCheckpoint, sb);
                sb.Append("last checkpoint: ").Append(exception.CheckpointDetail);
            }

            return(sb);
        }
        private static string GetTestResultMessageText(TestUnit unit, LogEntry entry)
        {
            Code.Require(unit, "unit");
            Code.Require(entry, "entry");

            if ((entry is LogEntryStandardOutputMessage) || (entry is LogEntryStandardErrorMessage))
            {
                return(entry.Detail.TrimEnd() + Environment.NewLine);
            }

            StringBuilder sb = new StringBuilder();

            if (entry.Source != null)
            {
                AppendSourceInfo(entry.Source, sb);
            }

            sb.Append(entry.ToString().ToLowerInvariant()).
            Append(" in \"").
            Append(unit.Name).
            Append("\"");

            LogEntryMemoryLeak memoryLeak = entry as LogEntryMemoryLeak;

            if (memoryLeak == null)
            {
                sb.Append(": ").Append(entry.Detail.TrimEnd());
            }

            LogEntryException exception = entry as LogEntryException;

            if (exception != null)
            {
                FormatException(exception, sb);
            }

            LogEntryError error = entry as LogEntryError;

            if (error != null)
            {
                FormatError(error, sb);
            }

            if (memoryLeak != null)
            {
                FormatMemoryLeak(memoryLeak, sb);
            }

            // Append NewLine so that log entries are listed one per line
            return(sb.Append(Environment.NewLine).ToString());
        }
        /// <summary>
        /// Parse a LogEntryException from the provided node.
        /// </summary>
        /// <param name="node">The Xml node which contains exception information.</param>
        /// <returns>A LogEntryException populated from the provided Xml node.</returns>
        private static LogEntryException ParseLogException(XmlNode node)
        {
            LogEntryException exception = ParseLogEntry <LogEntryException>(node);

            foreach (XmlNode child in node.ChildNodes)
            {
                if ((child.NodeType == XmlNodeType.Element) && (child.Name == Xml.LastCheckpoint))
                {
                    exception.LastCheckpoint   = ParseSourceInfo(child);
                    exception.CheckpointDetail = child.InnerText;
                }
            }

            return(exception);
        }