Beispiel #1
0
 private static void AppendTestCases(this TestSuiteReport self, ref StringBuilder sb, string tabbing)
 {
     foreach (var testCase in self.testResults)
     {
         testCase.AppendAsXml(ref sb, tabbing);
     }
 }
Beispiel #2
0
        public static List <TestSuiteReport> ParseXmlFile(string filename)
        {
            if (!File.Exists(filename))
            {
                return(outputReport);
            }

            string xmlString = File.ReadAllText(filename);

            using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
            {
                outputReport       = null;
                curReport          = null;
                _curSuiteReport    = null;
                _curTestCaseReport = null;

                // Parse the file and display each of the nodes.
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.Whitespace:
                        break;     // We simply accept that this exists, but otherwise don't really process it

                    case XmlNodeType.Element:
                        bool isEmptyElement = reader.IsEmptyElement;
                        ParseElementStart(reader, isEmptyElement);
                        if (isEmptyElement)
                        {
                            ParseElementEnd(reader, true);
                        }
                        break;

                    case XmlNodeType.Text:
                        TestFinishState tempState;
                        if (Enum.TryParse(reader.Value, true, out tempState))
                        {
                            _curTestCaseReport.finishState = tempState;
                        }
                        else
                        {
                            _curTestCaseReport.failureText = reader.Value;
                        }
                        break;

                    case XmlNodeType.EndElement:
                        ParseElementEnd(reader, false);
                        break;

                    default:
                        throw new Exception("Unexpected xml node: " + reader.NodeType);
                    }
                }
            }

            return(outputReport);
        }
Beispiel #3
0
        private static void ParseElementStart(XmlReader reader, bool isEmptyElement)
        {
            double tempSeconds;

            switch (reader.Name)
            {
            case ("testsuites"):
                _curReport = new List <TestSuiteReport>();
                break;

            case ("testsuite"):
                _curSuiteReport = new TestSuiteReport
                {
                    name = reader.GetAttribute("name")
                };
                int.TryParse(reader.GetAttribute("errors"), out _curSuiteReport.errors);
                int.TryParse(reader.GetAttribute("tests"), out _curSuiteReport.tests);
                int.TryParse(reader.GetAttribute("failures"), out _curSuiteReport.failures);
                int.TryParse(reader.GetAttribute("skipped"), out _curSuiteReport.skipped);
                double.TryParse(reader.GetAttribute("time"), out tempSeconds);
                _curSuiteReport.time = TimeSpan.FromSeconds(tempSeconds);
                DateTime.TryParseExact(reader.GetAttribute("timestamp"), PlayFabUtil.DefaultDateTimeFormats, null, System.Globalization.DateTimeStyles.RoundtripKind, out _curSuiteReport.timestamp);
                break;

            case ("properties"):
                _curSuiteReport.properties = new Dictionary <string, string>();
                break;

            case ("property"):
                _curSuiteReport.properties[reader.GetAttribute("name")] = reader.GetAttribute("value");
                break;

            case ("testcase"):
                _curTestCaseReport = new TestCaseReport
                {
                    classname   = reader.GetAttribute("classname"),
                    name        = reader.GetAttribute("name"),
                    finishState = isEmptyElement ? UUnitFinishState.PASSED : UUnitFinishState.FAILED,     // Empty element means no notes about failure, non-empty will almost certainly override this value
                };
                double.TryParse(reader.GetAttribute("time"), out tempSeconds);
                _curTestCaseReport.time = TimeSpan.FromSeconds(tempSeconds);
                break;

            case ("failure"):
                _curTestCaseReport.finishState = UUnitFinishState.FAILED;
                _curTestCaseReport.message     = reader.GetAttribute("message");
                break;

            case ("skipped"):
                _curTestCaseReport.finishState = UUnitFinishState.SKIPPED;
                _curTestCaseReport.message     = reader.GetAttribute("message");
                break;

            default:
                throw new Exception("Unexpected element: " + reader.Name);
            }
        }
Beispiel #4
0
        private static void AppendTestSuiteLine(this TestSuiteReport self, ref StringBuilder sb, bool isSingleLine, string tabbing)
        {
            var suffix = isSingleLine ? " /" : "";

            if (self.skipped == 0)
            {
                sb.Append(tabbing).AppendFormat("<testsuite name=\"{0}\" errors=\"{1}\" tests=\"{2}\" failures=\"{3}\" time=\"{4}\" timestamp=\"{5}\"{6}>\n", self.name, self.errors, self.tests, self.failures, self.time.TotalSeconds.ToString("0.###"), self.timestamp.ToString(PlayFabUtil.DefaultDateTimeFormats[PlayFabUtil.DEFAULT_UTC_OUTPUT_INDEX]), suffix);
            }
            else
            {
                sb.Append(tabbing).AppendFormat("<testsuite name=\"{0}\" errors=\"{1}\" skipped=\"{2}\" tests=\"{3}\" failures=\"{4}\" time=\"{5}\" timestamp=\"{6}\"{7}>\n", self.name, self.errors, self.skipped, self.tests, self.failures, self.time.TotalSeconds.ToString("0.###"), self.timestamp.ToString(PlayFabUtil.DefaultDateTimeFormats[PlayFabUtil.DEFAULT_UTC_OUTPUT_INDEX]), suffix);
            }
        }
Beispiel #5
0
        private static void AppendProperties(this TestSuiteReport self, ref StringBuilder sb, string tabbing)
        {
            if (self.properties == null || self.properties.Count == 0)
            {
                return;
            }

            sb.Append(tabbing).Append("<properties>\n");
            tabbing += "  ";
            foreach (var propPair in self.properties)
            {
                sb.Append(tabbing).AppendFormat("<property name=\"{0}\" value=\"{1}\" />\n", propPair.Key, propPair.Value);
            }
            tabbing = tabbing.Substring(2);
            sb.Append(tabbing).Append("</properties>\n");
        }
Beispiel #6
0
        public static void AppendAsXml(this TestSuiteReport self, ref StringBuilder sb, string tabbing)
        {
            var isSingleLine = (self.properties == null || self.properties.Count == 0) &&
                               (self.testResults == null || self.testResults.Count == 0) &&
                               self.tests == 0 &&
                               self.failures == 0;

            self.AppendTestSuiteLine(ref sb, isSingleLine, tabbing);
            if (isSingleLine)
            {
                return; // Nothing else is written if it's a single line
            }
            tabbing += "  ";
            self.AppendProperties(ref sb, tabbing);
            self.AppendTestCases(ref sb, tabbing);
            tabbing = tabbing.Substring(2);
            sb.Append(tabbing).Append("</testsuite>\n");
        }
Beispiel #7
0
        private void SendEmail(int testJobID, string userName)
        {
            try
            {
                var report = TestSuiteReport.CreateReport(testJobID);

                MailMessage message = new MailMessage();
                message.To.Add(userName + "@testapp.com");
                message.From       = new MailAddress("*****@*****.**");
                message.Subject    = "Automated Test Results for JobID " + testJobID;
                message.IsBodyHtml = false;
                message.Body       = report;

                SmtpClient client = new SmtpClient("smtprelay.services.testapp");
                client.UseDefaultCredentials = true;
                client.Send(message);
            }
            catch (Exception ex)
            {
                Log("Exception when sending email: " + ex);
            }
        }
Beispiel #8
0
        private static void ParseElementEnd(XmlReader reader)
        {
            switch (reader.Name)
            {
            case ("testsuites"):
                _outputReport = _curReport;
                break;

            case ("testsuite"):
                _curReport.Add(_curSuiteReport);
                _curSuiteReport = null;
                break;

            case ("properties"):
                break;

            case ("property"):
                break;

            case ("testcase"):
                if (_curSuiteReport.testResults == null)
                {
                    _curSuiteReport.testResults = new List <TestCaseReport>();
                }
                _curSuiteReport.testResults.Add(_curTestCaseReport);
                _curTestCaseReport = null;
                break;

            case ("failure"):
                break;

            case ("skipped"):
                break;

            default:
                throw new Exception("Unexpected element: " + reader.Name);
            }
        }