private static XElement CreateTestElement(TestResultInfo result)
        {
            var element = new XElement("test-case",
                                       new XAttribute("name", result.Name),
                                       new XAttribute("fullname", result.Type + "." + result.Method),
                                       new XAttribute("result", OutcomeToString(result.Outcome)),
                                       new XAttribute("duration", result.Time.TotalSeconds),
                                       new XAttribute("asserts", 0));

            StringBuilder stdOut = new StringBuilder();

            foreach (var m in result.Messages)
            {
                if (TestResultMessage.StandardOutCategory.Equals(m.Category, StringComparison.OrdinalIgnoreCase))
                {
                    stdOut.AppendLine(m.Text);
                }
            }

            if (!string.IsNullOrWhiteSpace(stdOut.ToString()))
            {
                element.Add(new XElement("output", new XCData(stdOut.ToString())));
            }

            if (result.Outcome == TestOutcome.Failed)
            {
                element.Add(new XElement("failure",
                                         new XElement("message", RemoveInvalidXmlChar(result.ErrorMessage)),
                                         new XElement("stack-trace", RemoveInvalidXmlChar(result.ErrorStackTrace))));
            }

            return(element);
        }
Example #2
0
        public static Type GetTestFixtureType(TestResultInfo result)
        {
            if (result == null)
            {
                return(null);
            }

            var assemblyName = Path.GetFileNameWithoutExtension(result.AssemblyPath);

            return(Type.GetType($"{result.FullTypeName}, {assemblyName}"));
        }
Example #3
0
        public static (string fullTypeName, string methodName) TestResultKey(this TestResultInfo testResult)
        {
            var idx = testResult.Method.IndexOf("(", StringComparison.CurrentCulture);

            if (idx != -1)
            {
                var methodName = testResult.Method.Substring(0, idx);
                return(testResult.FullTypeName, methodName);
            }

            return(testResult.FullTypeName, testResult.Method);
        }
Example #4
0
        public override bool Equals(object obj)
        {
            if (obj is TestResultInfo)
            {
                TestResultInfo objectToCompare = (TestResultInfo)obj;
                if (string.Compare(this.ErrorMessage, objectToCompare.ErrorMessage) == 0 &&
                    string.Compare(this.ErrorStackTrace, objectToCompare.ErrorStackTrace) == 0)
                {
                    return(true);
                }
            }

            return(false);
        }
        private static XElement CreateTestCaseElement(TestResultInfo result)
        {
            var element = new XElement(
                "test-case",
                new XAttribute("name", result.TestCase.DisplayName),
                new XAttribute("fullname", result.FullTypeName + "." + result.Method),
                new XAttribute("methodname", result.Method),
                new XAttribute("classname", result.Type),
                new XAttribute("result", OutcomeToString(result.Outcome)),
                new XAttribute("start-time", result.StartTime.ToString(DateFormat, CultureInfo.InvariantCulture)),
                new XAttribute("end-time", result.EndTime.ToString(DateFormat, CultureInfo.InvariantCulture)),
                new XAttribute("duration", result.Duration.TotalSeconds),
                new XAttribute("asserts", 0),
                CreateSeedAttribute(result.TestCase),
                CreatePropertiesElement(result.TestCase));

            StringBuilder stdOut = new StringBuilder();

            foreach (var m in result.Messages)
            {
                if (TestResultMessage.StandardOutCategory.Equals(m.Category, StringComparison.OrdinalIgnoreCase))
                {
                    stdOut.AppendLine(m.Text);
                }
            }

            if (!string.IsNullOrWhiteSpace(stdOut.ToString()))
            {
                element.Add(new XElement("output", new XCData(stdOut.ToString())));
            }

            if (result.Outcome == TestOutcome.Failed)
            {
                element.Add(new XElement(
                                "failure",
                                new XElement("message", result.ErrorMessage.ReplaceInvalidXmlChar()),
                                new XElement("stack-trace", result.ErrorStackTrace.ReplaceInvalidXmlChar())));
            }

            return(element);
        }
Example #6
0
        public static string GetDescription(TestResultInfo result)
        {
            var splitMethod  = result.Method.Split('(', ')');
            var methodName   = splitMethod[0];
            var testCaseArgs = splitMethod.Length == 1 ? null : splitMethod[1];

            var testFixtureType = ReflectionUtility.GetTestFixtureType(result);

            var matchingMethod = testFixtureType.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                 .Where(x => x.Name == methodName)
                                 .FirstOrDefault(x => IsSignatureMatching(x, testCaseArgs));

            if (matchingMethod == null)
            {
                return(null);
            }

            var attributes = matchingMethod.GetCustomAttributes(false).Cast <Attribute>();

            return(GetDescription(attributes));
        }
        private static XElement CreatePropertiesElement(TestResultInfo result)
        {
            var propertyElements = new HashSet <XElement>(result.Traits.Select(CreatePropertyElement));

#pragma warning disable CS0618 // Type or member is obsolete

            // Required since TestCase.Properties is a superset of TestCase.Traits
            // Unfortunately not all NUnit properties are available as traits
            var traitProperties = result.TestCase.Properties.Where(t => t.Attributes.HasFlag(TestPropertyAttributes.Trait));

#pragma warning restore CS0618 // Type or member is obsolete

            foreach (var p in traitProperties)
            {
                var propValue = result.TestCase.GetPropertyValue(p);

                if (p.Id == "NUnit.TestCategory")
                {
                    var elements = CreatePropertyElement("Category", (string[])propValue);

                    foreach (var element in elements)
                    {
                        propertyElements.Add(element);
                    }
                }
            }

            // NUnit attributes not passed through in traits.
            var description = ReflectionUtility.GetDescription(result);
            if (description != null)
            {
                var propertyElement = CreatePropertyElement("Description", description).Single();
                propertyElements.Add(propertyElement);
            }

            return(propertyElements.Any()
                ? new XElement("properties", propertyElements.Distinct())
                : null);
        }
        private static XElement CreateErrorElement(TestResultInfo result)
        {
            string errorMessage = result.ErrorMessage;

            int indexOfErrorType = errorMessage.IndexOf('(');

            errorMessage = errorMessage.Substring(indexOfErrorType + 1);

            int    indexOfName = errorMessage.IndexOf(')');
            string name        = errorMessage.Substring(0, indexOfName);

            errorMessage = errorMessage.Substring(indexOfName + 4);

            int    indexOfExceptionType = errorMessage.IndexOf(':');
            string exceptionType        = errorMessage.Substring(0, indexOfExceptionType - 1);

            XElement errorElement = new XElement("error");

            errorElement.SetAttributeValue("name", name);

            errorElement.Add(CreateFailureElement(exceptionType, errorMessage, result.ErrorStackTrace));

            return(errorElement);
        }
#pragma warning disable SA1204 // Static elements should appear before instance elements
        private static TestCase CreateTestCase(TestResultInfo result)
#pragma warning restore SA1204 // Static elements should appear before instance elements
        {
            var element = new XElement(
                "test-case",
                new XAttribute("name", result.Name),
                new XAttribute("fullname", result.FullTypeName + "." + result.Method),
                new XAttribute("methodname", result.Method),
                new XAttribute("classname", result.FullTypeName),
                new XAttribute("result", OutcomeToString(result.Outcome)),
                new XAttribute("start-time", result.StartTime.ToString(DateFormat, CultureInfo.InvariantCulture)),
                new XAttribute("end-time", result.EndTime.ToString(DateFormat, CultureInfo.InvariantCulture)),
                new XAttribute("duration", result.Duration.TotalSeconds),
                new XAttribute("asserts", 0));

            StringBuilder stdOut = new StringBuilder();

            foreach (var m in result.Messages)
            {
                if (TestResultMessage.StandardOutCategory.Equals(m.Category, StringComparison.OrdinalIgnoreCase))
                {
                    stdOut.AppendLine(m.Text);
                }
            }

            if (!string.IsNullOrWhiteSpace(stdOut.ToString()))
            {
                element.Add(new XElement("output", new XCData(stdOut.ToString())));
            }

            if (result.Outcome == TestOutcome.Failed)
            {
                element.Add(new XElement(
                                "failure",
                                new XElement("message", result.ErrorMessage.ReplaceInvalidXmlChar()),
                                new XElement("stack-trace", result.ErrorStackTrace.ReplaceInvalidXmlChar())));
            }

            var testCase = new TestCase
            {
                Element   = element,
                Name      = result.Name,
                FullName  = result.FullTypeName + "." + result.Method,
                StartTime = result.StartTime,
                EndTime   = result.EndTime,
                Time      = result.Duration
            };

            switch (result.Outcome)
            {
            case TestOutcome.Failed:
                testCase.Failed = 1;
                testCase.Total  = 1;
                break;

            case TestOutcome.Passed:
                testCase.Passed = 1;
                testCase.Total  = 1;
                break;

            case TestOutcome.Skipped:
                testCase.Skipped = 1;
                testCase.Total   = 1;
                break;

            case TestOutcome.None:
                testCase.Inconclusive = 1;
                testCase.Total        = 1;
                break;
            }

            return(testCase);
        }