Пример #1
0
        public static void WriteXml(TcUnitTestResult testResults, string filePath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            // <testsuites>
            XmlElement testSuitesNode = xmlDoc.CreateElement("testsuites");

            xmlDoc.AppendChild(testSuitesNode);

            // <testsuites> attributes
            XmlAttribute testSuitesAttributeFailures = xmlDoc.CreateAttribute("failures");

            testSuitesAttributeFailures.Value = testResults.GetNumberOfFailedTestCases().ToString();
            testSuitesNode.Attributes.Append(testSuitesAttributeFailures);
            XmlAttribute testSuitesAttributeTests = xmlDoc.CreateAttribute("tests");

            testSuitesAttributeTests.Value = testResults.GetNumberOfTestCases().ToString();
            testSuitesNode.Attributes.Append(testSuitesAttributeTests);

            foreach (TcUnitTestResult.TestSuiteResult tsResult in testResults)
            {
                // <testsuite>
                XmlElement testSuiteNode = xmlDoc.CreateElement("testsuite");
                // <testsuite> attributes
                XmlAttribute testSuiteAttributeIdentity = xmlDoc.CreateAttribute("id");
                testSuiteAttributeIdentity.Value = tsResult.Identity.ToString();
                testSuiteNode.Attributes.Append(testSuiteAttributeIdentity);
                XmlAttribute testSuiteAttributeName = xmlDoc.CreateAttribute("name");
                testSuiteAttributeName.Value = tsResult.Name;
                testSuiteNode.Attributes.Append(testSuiteAttributeName);
                XmlAttribute testSuiteAttributeTests = xmlDoc.CreateAttribute("tests");
                testSuiteAttributeTests.Value = tsResult.NumberOfTests.ToString();
                testSuiteNode.Attributes.Append(testSuiteAttributeTests);
                XmlAttribute testSuiteAttributeFailures = xmlDoc.CreateAttribute("failures");
                testSuiteAttributeFailures.Value = tsResult.NumberOfFailedTests.ToString();
                testSuiteNode.Attributes.Append(testSuiteAttributeFailures);

                // <testcase>
                foreach (TcUnitTestResult.TestCaseResult tcResult in tsResult.TestCaseResults)
                {
                    XmlElement testCaseNode = xmlDoc.CreateElement("testcase");

                    // <testcase> attributes
                    XmlAttribute testCaseAttributeName = xmlDoc.CreateAttribute("name");
                    testCaseAttributeName.Value = tcResult.TestName;
                    testCaseNode.Attributes.Append(testCaseAttributeName);
                    XmlAttribute testCaseAttributeNumberOfAsserts = xmlDoc.CreateAttribute("assertions");
                    testCaseAttributeNumberOfAsserts.Value = tcResult.NumberOfAsserts.ToString();
                    testCaseNode.Attributes.Append(testCaseAttributeNumberOfAsserts);
                    XmlAttribute testCaseAttributeTestClassName = xmlDoc.CreateAttribute("classname");
                    testCaseAttributeTestClassName.Value = tcResult.TestClassName;
                    testCaseNode.Attributes.Append(testCaseAttributeTestClassName);
                    XmlAttribute testCaseAttributeStatus = xmlDoc.CreateAttribute("status");
                    testCaseAttributeStatus.Value = tcResult.TestStatus;
                    testCaseNode.Attributes.Append(testCaseAttributeStatus);


                    if (tcResult.TestStatus.Equals("SKIP"))
                    {
                        // <skipped>
                        XmlElement testCaseSkippedNode = xmlDoc.CreateElement("skipped");

                        // Append <skipped> to <testcase>
                        testCaseNode.AppendChild(testCaseSkippedNode);
                    }
                    else if (tcResult.TestStatus.Equals("FAIL"))
                    {
                        // <failure>
                        XmlElement failureNode = xmlDoc.CreateElement("failure");

                        // <failure> attributes
                        XmlAttribute failureAttributeMessage = xmlDoc.CreateAttribute("message");
                        failureAttributeMessage.Value = tcResult.FailureMessage;
                        failureNode.Attributes.Append(failureAttributeMessage);
                        XmlAttribute failureAttributeType = xmlDoc.CreateAttribute("type");
                        failureAttributeType.Value = tcResult.AssertType;
                        failureNode.Attributes.Append(failureAttributeType);

                        // Append <failure> to <testcase>
                        testCaseNode.AppendChild(failureNode);
                    }

                    // Append <testcase> to <testesuite>
                    testSuiteNode.AppendChild(testCaseNode);
                }

                // Append <testsuite> to <testsuites>
                testSuitesNode.AppendChild(testSuiteNode);
            }

            BeautifyAndWriteToFile(xmlDoc, filePath);
        }