using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml; namespace TestMonkey { public class NUnitReport { private static string input_dir = string.Empty; private static string output_file = string.Empty; static void Main (string[] args) { if (args.Length != 2) { Console.WriteLine ("Expected Usage:"); Console.WriteLine (" mono NUnitReport.exe input_directory output_file"); return; } // Get our input directory and our output file input_dir = args[0]; output_file = args[1]; // Start the output file StreamWriter sw = new StreamWriter (output_file); StartReport (sw); int assembly = 0; int fail_total = 0; int pass_total = 0; int run_total = 0; // Loop through the inputs, outputting the results to the output file foreach (string file in Directory.GetFiles (input_dir)) { assembly++; Dictionary failed_tests = new Dictionary (); List ignored_tests = new List (); int tests_passed = PopulateFailureTable (file, failed_tests, ignored_tests); fail_total += failed_tests.Count; pass_total += tests_passed; run_total += failed_tests.Count + tests_passed; if (failed_tests.Count > 0) { sw.WriteLine (" ", assembly); sw.WriteLine (@" "); } else { sw.WriteLine (@" "); sw.WriteLine (@" "); } sw.WriteLine (@" {0}", Path.GetFileName (file)); sw.WriteLine (@" {0}", failed_tests.Count); sw.WriteLine (@" {0}", tests_passed); sw.WriteLine (@" {0}", tests_passed + failed_tests.Count); sw.WriteLine (@" "); if (failed_tests.Count == 0) continue; sw.WriteLine (@" ", assembly); sw.WriteLine (@" "); sw.WriteLine (@" "); sw.WriteLine (@" "); int test_num = 0; foreach (FailedTest ft in failed_tests.Values) { sw.WriteLine (" ", assembly, test_num); sw.WriteLine (@" "); sw.WriteLine (@" ", ft.Name); sw.WriteLine (@" "); sw.WriteLine (@" ", assembly, test_num); sw.WriteLine (@" "); sw.WriteLine (@" "); sw.WriteLine (@" "); test_num++; } sw.WriteLine (@"
{0}
"); sw.WriteLine (@" "); sw.WriteLine (@" "); } // Write totals WriteTotals (sw, fail_total, pass_total, run_total); // Finish up the output file FinishReport (sw); sw.Close (); sw.Dispose (); } public static int PopulateFailureTable (string filename, Dictionary output, List ignored) { XmlDocument doc = new XmlDocument (); doc.Load (filename); return FindTestCases (doc.DocumentElement, output, ignored); } public static int FindTestCases (XmlElement xe, Dictionary output, List ignored) { if (xe.Name == "test-case") { OutputFailedTestCase (xe, output, ignored); return 1; } int i = 0; foreach (XmlElement child in xe.ChildNodes) i += FindTestCases (child, output, ignored); return i; } public static void OutputFailedTestCase (XmlElement xe, Dictionary output, List ignored) { if (xe.GetAttribute ("executed") == "False") ignored.Add (xe.GetAttribute ("name")); if (xe.GetAttribute ("success") == "True" || xe.GetAttribute ("executed") == "False") return; FailedTest ft = new FailedTest (xe.GetAttribute ("name"), xe["failure"]["message"].InnerText, xe["failure"]["stack-trace"].InnerText); output[ft.Name] = ft; } public static void StartReport (StreamWriter sw) { sw.WriteLine (@""); sw.WriteLine (@""); sw.WriteLine (@"Mono: Class Libraries NUnit Test Results"); sw.WriteLine (@""); sw.WriteLine (@""); sw.WriteLine (@""); sw.WriteLine (@"
"); sw.WriteLine (@"
"); sw.WriteLine (@" "); sw.WriteLine (@"
"); sw.WriteLine (@"
Class Libraries NUnit Test Results
"); sw.WriteLine (@"
"); sw.WriteLine (@"
"); sw.WriteLine (@" Generated:
"); sw.WriteLine (@" {0}

", DateTime.Now.ToString ()); sw.WriteLine (@" Click on failure row for more details.

"); sw.WriteLine (@" Icons courtesy of famfamfam"); sw.WriteLine (@"
"); sw.WriteLine (@" "); sw.WriteLine (@" "); sw.WriteLine (@" "); sw.WriteLine (@" "); sw.WriteLine (@" "); sw.WriteLine (@" "); sw.WriteLine (@" "); sw.WriteLine (@" "); } public static void WriteTotals (StreamWriter sw, int failed, int passed, int run) { sw.WriteLine (@" "); sw.WriteLine (@" "); sw.WriteLine (@" "); sw.WriteLine (@" ", failed); sw.WriteLine (@" ", passed); sw.WriteLine (@" ", run); sw.WriteLine (@" "); } public static void FinishReport (StreamWriter sw) { sw.WriteLine (@"
Tested AssemblyFailedPassedRun
Totals{0}{0}{0}
"); sw.WriteLine (@""); sw.WriteLine (@""); } } }