File: NUnitReport.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (186 lines) | stat: -rw-r--r-- 7,240 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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<string, FailedTest> failed_tests = new Dictionary<string,FailedTest> ();
				List<string> ignored_tests = new List<string> ();
				
				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 ("        <tr class='errorrow' onclick=\"toggle('el{0}')\" onmouseover='highlight(this)' onmouseout='unhighlight(this)'>", assembly);
					sw.WriteLine (@"            <td><img src='Media/fail.png' /></td>");
				} else {
					sw.WriteLine (@"        <tr>");
					sw.WriteLine (@"            <td><img src='Media/pass.png' /></td>");
				}
				
				sw.WriteLine (@"            <td>{0}</td>", Path.GetFileName (file));
				sw.WriteLine (@"            <td>{0}</td>", failed_tests.Count);
				sw.WriteLine (@"            <td>{0}</td>", tests_passed);
				sw.WriteLine (@"            <td>{0}</td>", tests_passed + failed_tests.Count);
				sw.WriteLine (@"        </tr>");
				
				if (failed_tests.Count == 0)
					continue;

				sw.WriteLine (@"        <tr id='el{0}' class='errorlist' style='display: none'>", assembly);
				sw.WriteLine (@"            <td></td>");
				sw.WriteLine (@"            <td colspan='4'>");
				sw.WriteLine (@"                <table cellpadding='2' cellspacing='0' width='100%'>");
					
				int test_num = 0;
				
				foreach (FailedTest ft in failed_tests.Values) {
					sw.WriteLine ("                    <tr onclick=\"toggle('as{0}ed{1}')\" onmouseover='highlight(this)' onmouseout='unhighlight(this)'>", assembly, test_num);
					sw.WriteLine (@"                        <td style='width: 17px'><img src='Media/bullet.png' /></td>");
					sw.WriteLine (@"                        <td>{0}</td>", ft.Name);
					sw.WriteLine (@"                    </tr>");
					sw.WriteLine (@"                    <tr id='as{0}ed{1}' class='errordetail' style='display: none'>", assembly, test_num);
					sw.WriteLine (@"                        <td></td>");
					sw.WriteLine (@"                        <td>");
					sw.WriteLine (@"{0}", ft.Message.Trim ().Trim ('\n').Replace ("\n", "<br/>"));
					if (!string.IsNullOrEmpty (ft.StackTrace.Trim ()))
						sw.WriteLine (@"<br /><br /><strong>StackTrace:</strong><br />{0}", ft.StackTrace.Replace ("\n", "<br/>"));
					sw.WriteLine (@"                        </td>");
					sw.WriteLine (@"                    </tr>");
					
					test_num++;
				}

				sw.WriteLine (@"                </table>");
				sw.WriteLine (@"            </td>");
				sw.WriteLine (@"        </tr>");
			}
			
			// 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<string, FailedTest> output, List<string> ignored)
		{
			XmlDocument doc = new XmlDocument ();
			doc.Load (filename);

			return FindTestCases (doc.DocumentElement, output, ignored);
		}

		public static int FindTestCases (XmlElement xe, Dictionary<string, FailedTest> output, List<string> 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<string, FailedTest> output, List<string> 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 (@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">");
			sw.WriteLine (@"<html xmlns=""http://www.w3.org/1999/xhtml""><head>");
			sw.WriteLine (@"<title>Mono: Class Libraries NUnit Test Results</title>");
			sw.WriteLine (@"<link rel=""stylesheet"" type=""text/css"" href=""Media/style.css"" />");
			sw.WriteLine (@"<script type=""text/ecmascript"" src=""Media/scripts.js""></script></head>");
			sw.WriteLine (@"<body>");
			sw.WriteLine (@"    <div class='header'>");
			sw.WriteLine (@"        <div class='headerimage'>");
			sw.WriteLine (@"            <img src='Media/rupert.png' width='48' height='48' />");
			sw.WriteLine (@"        </div>");
			sw.WriteLine (@"        <div class='headertext'>Class Libraries NUnit Test Results</div>");
			sw.WriteLine (@"    </div>");
			sw.WriteLine (@"    <div class='legend'>");
			sw.WriteLine (@"        Generated:<br />");
			sw.WriteLine (@"        {0}<br /><br />", DateTime.Now.ToString ());
			sw.WriteLine (@"        Click on failure row for more details.<br /><br />");
			sw.WriteLine (@"        Icons courtesy of <a href='http://www.famfamfam.com/lab/icons/silk'>famfamfam</a>");
			sw.WriteLine (@"    </div>");
			sw.WriteLine (@"    <table cellpadding='2' cellspacing='0' class='maintable'>");
			sw.WriteLine (@"        <tr class='tableheader'>");
			sw.WriteLine (@"            <td style='width: 17px'></td>");
			sw.WriteLine (@"            <td>Tested Assembly</td>");
			sw.WriteLine (@"            <td>Failed</td>");
			sw.WriteLine (@"            <td>Passed</td>");
			sw.WriteLine (@"            <td>Run</td>");
			sw.WriteLine (@"        </tr>");
		}

		public static void WriteTotals (StreamWriter sw, int failed, int passed, int run)
		{
			sw.WriteLine (@"        <tr class='tabletotal'>");
			sw.WriteLine (@"            <td style='width: 17px'></td>");
			sw.WriteLine (@"            <td>Totals</td>");
			sw.WriteLine (@"            <td>{0}</td>", failed);
			sw.WriteLine (@"            <td>{0}</td>", passed);
			sw.WriteLine (@"            <td>{0}</td>", run);
			sw.WriteLine (@"        </tr>");
		}
		
		public static void FinishReport (StreamWriter sw)
		{
			sw.WriteLine (@"    </table>");
			sw.WriteLine (@"</body>");
			sw.WriteLine (@"</html>");
		}
	}
}