Exemplo n.º 1
0
        /// <summary>
        /// Builds the fixture level blocks
        /// Adds all tests to the output
        /// </summary>
        /// <param name="html"></param>
        private void BuildFixtureBlocks(string html)
        {
            Console.WriteLine("[INFO] Building fixture blocks...");

            string      errorMsg      = null;
            string      descMsg       = null;
            ArrayList   fixtureStatus = new ArrayList();
            XmlNodeList testSuite     = doc.SelectNodes("//test-suite[@type='TestFixture']");
            int         testCount     = 0;

            // run for each test-suite
            foreach (XmlNode suite in testSuite)
            {
                html = html.Replace(OrangeHelper.MarkupFlag("fixtureresult"), OrangeHelper.GetFixtureStatus(fixtureStatus))
                       .Replace(OrangeHelper.MarkupFlag("inserttest"), "")
                       .Replace(OrangeHelper.MarkupFlag("insertfixture"), HTML.TestSuiteLevelPage.Fixture)
                       .Replace(OrangeHelper.MarkupFlag("fixturename"), suite.Attributes["name"].InnerText);

                if (suite.Attributes["start-time"] != null && suite.Attributes["end-time"] != null)
                {
                    html = html.Replace(OrangeHelper.MarkupFlag("fixtureStartedAt"), suite.Attributes["start-time"].InnerText.Replace("Z", ""))
                           .Replace(OrangeHelper.MarkupFlag("fixtureEndedAt"), suite.Attributes["end-time"].InnerText.Replace("Z", ""))
                           .Replace(OrangeHelper.MarkupFlag("footerDisplay"), "")
                           .Replace(OrangeHelper.MarkupFlag("fixtureStartedAtDisplay"), "")
                           .Replace(OrangeHelper.MarkupFlag("fixtureEndedAtDisplay"), "");
                }
                else if (suite.Attributes["time"] != null)
                {
                    html = html.Replace(OrangeHelper.MarkupFlag("fixtureStartedAt"), suite.Attributes["time"].InnerText)
                           .Replace(OrangeHelper.MarkupFlag("footerDisplay"), "")
                           .Replace(OrangeHelper.MarkupFlag("fixtureStartedAtDisplay"), "")
                           .Replace(OrangeHelper.MarkupFlag("fixtureEndedAtDisplay"), "style='display:none;'");
                }
                else
                {
                    html = html.Replace(OrangeHelper.MarkupFlag("footerDisplay"), "style='display:none;'")
                           .Replace(OrangeHelper.MarkupFlag("fixtureStartedAtDisplay"), "style='display:none;'")
                           .Replace(OrangeHelper.MarkupFlag("fixtureEndedAtDisplay"), "style='display:none;'");
                }

                fixtureStatus.Clear();

                // add each test of the test-suite
                foreach (XmlNode testcase in suite.SelectNodes(".//test-case"))
                {
                    fixtureStatus.Add(testcase.Attributes["result"].InnerText);
                    errorMsg = descMsg = "";

                    if (testcase.SelectNodes(".//message").Count == 1)
                    {
                        errorMsg  = testcase.SelectNodes(".//message").Count == 1 ? "<pre>" + testcase.SelectNodes(".//message")[0].InnerText : "";
                        errorMsg += testcase.SelectNodes(".//stack-trace").Count == 1 ? " -> " + testcase.SelectNodes(".//stack-trace")[0].InnerText.Replace("\r", "").Replace("\n", "") : "";
                        errorMsg += "</pre>";
                        errorMsg  = errorMsg == "<pre></pre>" ? "" : errorMsg;
                    }

                    if (testcase.SelectNodes(".//property[@name='Description']").Count == 1)
                    {
                        descMsg += testcase.SelectNodes(".//property[@name='Description']").Count == 1 ? "<p class='description'>Description: " + testcase.SelectNodes(".//property[@name='Description']")[0].Attributes["value"].InnerText : "";
                        descMsg += "</p>";
                        descMsg  = descMsg == "<p class='description'>Description: </p>" ? "" : descMsg;
                    }

                    // test-level replacements
                    html = html.Replace(OrangeHelper.MarkupFlag("inserttest"), HTML.TestSuiteLevelPage.Test)
                           .Replace(OrangeHelper.MarkupFlag("testname"), testcase.Attributes["name"].InnerText.Replace("<", "[").Replace(">", "]"))
                           .Replace(OrangeHelper.MarkupFlag("teststatus"), testcase.Attributes["result"].InnerText.ToLower())
                           .Replace(OrangeHelper.MarkupFlag("teststatusmsg"), descMsg + errorMsg);

                    Console.Write("\r{0} tests processed...", ++testCount);
                }
            }

            html = html.Replace(OrangeHelper.MarkupFlag("fixtureresult"), OrangeHelper.GetFixtureStatus(fixtureStatus));

            // add topbar for folder-level report to allow backward navigation to Index.html
            if (addTopbar)
            {
                html = html.Replace(OrangeHelper.MarkupFlag("topbar"), HTML.TestSuiteLevelPage.Topbar);
            }

            // finally, save the source as the output file
            File.WriteAllText(orangeFile, html);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Builds the report by consuming TestSuiteLevelPage source
        /// Adds dashboard level data
        /// </summary>
        /// <returns>Dictionary</returns>
        public Dictionary <string, string> BuildReport()
        {
            // if XML file is invalid or if it does not contain the require tags, exit
            if (doc.DocumentElement == null)
            {
                return(null);
            }

            // create a data instance to be passed to the folder level report
            if (data == null)
            {
                data = new Dictionary <string, string>();
            }

            data.Clear();

            Console.WriteLine("\n[INFO] Processing file '" + nunitResultFile + "'..");

            // base TestSuite html
            string html = HTML.TestSuiteLevelPage.Base;

            // get total count of tests from the input file
            int totalTests = doc.GetElementsByTagName("test-case").Count;

            // only proceed if the test count is more than 0
            if (totalTests >= 1)
            {
                Console.WriteLine("[INFO] Processing root and test-suite elements...");

                // pull values from XML source
                int    passed       = doc.SelectNodes(".//test-case[@result='Success' or @result='Passed']").Count;
                int    failed       = doc.SelectNodes(".//test-case[@result='Failed' or @result='Failure']").Count;
                int    inconclusive = doc.SelectNodes(".//test-case[@result='Inconclusive' or @result='NotRunnable']").Count;
                int    skipped      = doc.SelectNodes(".//test-case[@result='Skipped' or @result='Ignored']").Count;
                int    errors       = doc.SelectNodes(".//test-case[@result='Error']").Count;
                string runResult    = doc.SelectNodes("//test-suite")[0].Attributes["result"].InnerText;
                string name         = doc.SelectNodes("//test-suite")[0].Attributes["name"].InnerText;
                string timeTaken    = "";

                data.Add("Total", totalTests.ToString());
                data.Add("Passed", passed.ToString());
                data.Add("Failed", (failed + errors).ToString());
                data.Add("Other", (inconclusive + skipped).ToString());
                data.Add("Result", runResult);
                data.Add("AssemblyName", name);

                Console.WriteLine("[INFO] Number of tests: " + totalTests);

                try { timeTaken = doc.SelectNodes("//test-suite")[0].Attributes["duration"].InnerText; }
                catch { try { timeTaken = doc.SelectNodes("//test-suite")[0].Attributes["time"].InnerText; } catch { } }

                // do the replacing here
                html = html.Replace(OrangeHelper.MarkupFlag("totalTests"), totalTests.ToString())
                       .Replace(OrangeHelper.MarkupFlag("passed"), passed.ToString())
                       .Replace(OrangeHelper.MarkupFlag("failed"), failed.ToString())
                       .Replace(OrangeHelper.MarkupFlag("inconclusive"), inconclusive.ToString())
                       .Replace(OrangeHelper.MarkupFlag("skipped"), skipped.ToString())
                       .Replace(OrangeHelper.MarkupFlag("errors"), errors.ToString())
                       .Replace(OrangeHelper.MarkupFlag("inXml"), Path.GetFullPath(nunitResultFile))
                       .Replace(OrangeHelper.MarkupFlag("duration"), timeTaken)
                       .Replace(OrangeHelper.MarkupFlag("result"), runResult)
                       .Replace(OrangeHelper.MarkupFlag("name"), name);

                try
                {
                    // try to parse the environment node
                    // some attributes in the environment node are different for 2.x and 3.x
                    XmlNode env = doc.GetElementsByTagName("environment")[0];

                    html = html.Replace(OrangeHelper.MarkupFlag("userDomain"), env.Attributes["user-domain"].InnerText)
                           .Replace(OrangeHelper.MarkupFlag("user"), env.Attributes["user"].InnerText)
                           .Replace(OrangeHelper.MarkupFlag("machineName"), env.Attributes["machine-name"].InnerText)
                           .Replace(OrangeHelper.MarkupFlag("platform"), env.Attributes["platform"].InnerText)
                           .Replace(OrangeHelper.MarkupFlag("osVersion"), env.Attributes["os-version"].InnerText)
                           .Replace(OrangeHelper.MarkupFlag("clrVersion"), env.Attributes["clr-version"].InnerText)
                           .Replace(OrangeHelper.MarkupFlag("nunitVersion"), env.Attributes["nunit-version"].InnerText);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("[ERROR] There was an error processing the _ENVIRONMENT_ node: " + ex.Message);
                }

                BuildFixtureBlocks(html);
            }
            else
            {
                Console.Write("There are no tests available in this file.");

                try
                {
                    // replace OPTIONALCSS here with css to hide elements
                    html = html.Replace(OrangeHelper.MarkupFlag("insertNoTestsMessage"), HTML.TestSuiteLevelPage.NoTestsMessage)
                           .Replace("/*%OPTIONALCSS%*/", HTML.TestSuiteLevelPage.NoTestsCSS)
                           .Replace(OrangeHelper.MarkupFlag("inXml"), Path.GetFileName(nunitResultFile));

                    // add topbar for folder-level report to allow backward navigation to Index.html
                    if (addTopbar)
                    {
                        html = html.Replace(OrangeHelper.MarkupFlag("topbar"), HTML.TestSuiteLevelPage.Topbar);
                    }

                    // finally, save the source as the output file
                    File.WriteAllText(orangeFile, html);

                    data.Add("Total", "0");
                    data.Add("Passed", "0");
                    data.Add("Failed", "0");
                    data.Add("Other", "0");
                    data.Add("Result", "Passed");
                    data.Add("AssemblyName", doc.SelectNodes("//test-suite")[0].Attributes["name"].InnerText);

                    return(data);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Something weird happened: " + ex.Message);
                    return(null);
                }
            }

            return(data);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Builds the folder-level / executive-summary report from all input XML files
        /// </summary>
        public void BuildReport()
        {
            List <string> allFiles = Directory.GetFiles(inDir, "*.*", SearchOption.TopDirectoryOnly)
                                     .Where(s => s.ToLower().EndsWith("xml"))
                                     .ToList();

            // if no XML files, end process
            if (allFiles.Count == 0)
            {
                Console.WriteLine("[INFO] No XML files were found in the given location. Exiting..");
                return;
            }

            TestSuiteParser fileParser = new TestSuiteParser();

            // data passed from the TestSuite level parser
            Dictionary <string, string> data;

            // folder-level HTML source
            string html = HTML.FolderLevelPage.Base;

            // build report for each input file
            foreach (string file in allFiles)
            {
                data = fileParser.SetFiles(file, Path.Combine(outDir, Path.GetFileNameWithoutExtension(file) + ".html")).AddTopBar(true).BuildReport();

                if (data != null)
                {
                    if (data["Total"] == "0")
                    {
                        html = html.Replace(OrangeHelper.MarkupFlag("insertResult"), HTML.FolderLevelPage.Row)
                               .Replace(OrangeHelper.MarkupFlag("fullFilename"), Path.GetFileNameWithoutExtension(file) + ".html")
                               .Replace(OrangeHelper.MarkupFlag("filename"), Path.GetFileNameWithoutExtension(file))
                               .Replace(OrangeHelper.MarkupFlag("assembly"), Path.GetFileName(data["AssemblyName"]))
                               .Replace(OrangeHelper.MarkupFlag("runresult"), data["Result"].ToLower())
                               .Replace(OrangeHelper.MarkupFlag("totalTests"), data["Total"])
                               .Replace(OrangeHelper.MarkupFlag("totalPassed"), data["Passed"])
                               .Replace(OrangeHelper.MarkupFlag("totalFailed"), data["Failed"])
                               .Replace(OrangeHelper.MarkupFlag("allOtherTests"), data["Other"])
                               .Replace(OrangeHelper.MarkupFlag("passedPercentage"), "0")
                               .Replace(OrangeHelper.MarkupFlag("failedPercentage"), "0")
                               .Replace(OrangeHelper.MarkupFlag("othersPercentage"), "0");
                    }
                    else
                    {
                        html = html.Replace(OrangeHelper.MarkupFlag("insertResult"), HTML.FolderLevelPage.Row)
                               .Replace(OrangeHelper.MarkupFlag("fullFilename"), Path.GetFileNameWithoutExtension(file) + ".html")
                               .Replace(OrangeHelper.MarkupFlag("filename"), Path.GetFileNameWithoutExtension(file))
                               .Replace(OrangeHelper.MarkupFlag("assembly"), Path.GetFileName(data["AssemblyName"]))
                               .Replace(OrangeHelper.MarkupFlag("runresult"), data["Result"].ToLower())
                               .Replace(OrangeHelper.MarkupFlag("totalTests"), data["Total"])
                               .Replace(OrangeHelper.MarkupFlag("totalPassed"), data["Passed"])
                               .Replace(OrangeHelper.MarkupFlag("totalFailed"), data["Failed"])
                               .Replace(OrangeHelper.MarkupFlag("allOtherTests"), data["Other"])
                               .Replace(OrangeHelper.MarkupFlag("passedPercentage"), (Convert.ToInt32(data["Passed"]) * 100 / Convert.ToInt32(data["Total"])).ToString())
                               .Replace(OrangeHelper.MarkupFlag("failedPercentage"), (Convert.ToInt32(data["Failed"]) * 100 / Convert.ToInt32(data["Total"])).ToString())
                               .Replace(OrangeHelper.MarkupFlag("othersPercentage"), (Convert.ToInt32(data["Other"]) * 100 / Convert.ToInt32(data["Total"])).ToString());
                    }
                }
            }
            string outputfile = Path.Combine(outDir, "Index.html");

            // write the entire source with all fixture/test-suite level data row-wise
            File.WriteAllText(outputfile, html);

            Console.WriteLine("\nNUnitOrange executive summary created: " + outputfile);
        }