Пример #1
0
        public void OnTestResult(TestResultEventArgs ev)
        {
            if (ev == null)
            {
                throw new ArgumentNullException(nameof(ev));
            }

            var result   = ev.Result;
            var testCase = result.TestCase;

            if (!_testCaseFilter.IsSupported(testCase))
            {
                return;
            }

            var suiteName       = _suiteNameProvider.GetSuiteName(_options.TestRunDirectory, testCase.Source);
            var testSuiteWriter = GetTestSuiteWriter(suiteName);
            var testName        = testCase.FullyQualifiedName ?? testCase.DisplayName ?? testCase.Id.ToString();

            using (var testWriter = testSuiteWriter.OpenTest(testName))
            {
                // ReSharper disable once SuspiciousTypeConversion.Global
                testWriter.WriteDuration(result.Duration);
                if (result.Messages != null && result.Messages.Count > 0)
                {
                    foreach (var message in result.Messages)
                    {
                        if (TestResultMessage.StandardOutCategory.Equals(message.Category, StringComparison.CurrentCultureIgnoreCase) ||
                            TestResultMessage.AdditionalInfoCategory.Equals(message.Category, StringComparison.CurrentCultureIgnoreCase) ||
                            TestResultMessage.DebugTraceCategory.Equals(message.Category, StringComparison.CurrentCultureIgnoreCase))
                        {
                            testWriter.WriteStdOutput(message.Text);
                            continue;
                        }

                        if (TestResultMessage.StandardErrorCategory.Equals(message.Category, StringComparison.CurrentCultureIgnoreCase))
                        {
                            testWriter.WriteErrOutput(message.Text);
                        }
                    }
                }

                foreach (var attachments in result.Attachments)
                {
                    _attachments.SendAttachmentSet(testName, attachments, testWriter);
                }

                switch (result.Outcome)
                {
                case TestOutcome.Passed:
                    break;

                case TestOutcome.Failed:
                    testWriter.WriteFailed(result.ErrorMessage ?? string.Empty, result.ErrorStackTrace ?? string.Empty);
                    break;

                case TestOutcome.Skipped:
                case TestOutcome.None:     // https://github.com/JetBrains/TeamCity.VSTest.TestAdapter/issues/23
                case TestOutcome.NotFound:
                    if (string.IsNullOrEmpty(result.ErrorMessage))
                    {
                        testWriter.WriteIgnored();
                    }
                    else
                    {
                        testWriter.WriteIgnored(result.ErrorMessage);
                    }

                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(result.Outcome), result.Outcome, "Invalid value");
                }
            }
        }