Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestRunner.cs « NUnitConsole « src « nunit « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 77493e48866d29d66559cb621fd3674906ebf763 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
namespace NUnit.TextUI 
{
	using System;
	using System.IO;
	using System.Reflection;
	using NUnit.Framework;
	using NUnit.Runner;

	/// <summary>A command line based tool to run tests.</summary><remarks>
	/// <code>
	/// C:\NUnitConsole.exe /t [/wait] TestCaseClass
	/// </code>
	/// TestRunner expects the name of a TestCase class as argument.
	/// If this class defines a static <c>Suite</c> property it 
	/// will be invoked and the returned test is run. Otherwise all 
	/// the methods starting with "Test" having no arguments are run.
	///
	/// When the wait command line argument is given TestRunner
	/// waits until the users types RETURN.
	///
	/// TestRunner prints a trace as the tests are executed followed by a
	/// summary at the end.</remarks>
	public class TestRunner : BaseTestRunner 
	{
		int fColumn = 0;
		TextWriter fWriter = Console.Out;

		/// <summary>
		/// Constructs a TestRunner.
		/// </summary>
		public TestRunner() {}

		/// <summary>
		/// Constructs a TestRunner using the given stream for all the output
		/// </summary>
		public TestRunner(TextWriter writer) : this() 
		{
			if (writer != null)
			{
				fWriter= writer;
			}
			else
			{
				throw new ArgumentNullException("writer");
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="test"></param>
		/// <param name="t"></param>
		public override void AddError(ITest test, Exception t) 
		{
			lock(this)
				this.Writer.Write("E");
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="test"></param>
		/// <param name="t"></param>
		public override void AddFailure(ITest test, AssertionFailedError t) 
		{
			lock (this)
				this.Writer.Write("F");
		}

		/// <summary>Creates the TestResult to be used for the test run.</summary>
		protected TestResult CreateTestResult() 
		{
			return new TestResult();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="suite"></param>
		/// <param name="wait"></param>
		/// <returns></returns>
		protected TestResult DoRun(ITest suite, bool wait) 
		{
			TestResult result= CreateTestResult();
			result.AddListener(this);
			long startTime= System.DateTime.Now.Ticks;
			suite.Run(result);
			long endTime= System.DateTime.Now.Ticks;
			long runTime= (endTime-startTime) / 10000;
			Writer.WriteLine();
			Writer.WriteLine("Time: "+ElapsedTimeAsString(runTime));
			Print(result);

			Writer.WriteLine();
            
			if (wait) 
			{
				Writer.WriteLine("<RETURN> to continue");
				try 
				{
					Console.ReadLine();
				}
				catch(Exception) 
				{
				}
			}
			return result;    
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="test"></param>
    
		public override void EndTest(ITest test) 
		{
		}
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public override ITestLoader GetLoader() 
		{
			return new StandardLoader();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="result"></param>
		public void Print(TestResult result) 
		{
			lock(this) 
			{
				PrintErrors(result);
				PrintFailures(result);
				PrintHeader(result);
			}
		}

		/// <summary>Prints the errors to the standard output.</summary>
		public void PrintErrors(TestResult result) 
		{
			if (result.ErrorCount != 0) 
			{
				if (result.ErrorCount == 1)
					Writer.WriteLine("There was "+result.ErrorCount+" error:");
				else
					Writer.WriteLine("There were "+result.ErrorCount+" errors:");
                
				int i= 1;
				foreach (TestFailure failure in result.Errors) 
				{
					Writer.WriteLine(i++ + ") "+failure+"("+failure.ThrownException.GetType().ToString()+")");
					Writer.Write(GetFilteredTrace(failure.ThrownException));
				}
			}
		}

		/// <summary>Prints failures to the standard output.</summary>
		public void PrintFailures(TestResult result) 
		{
			if (result.FailureCount != 0) 
			{
				if (result.FailureCount == 1)
					Writer.WriteLine("There was " + result.FailureCount + " failure:");
				else
					Writer.WriteLine("There were " + result.FailureCount + " failures:");
				int i = 1;
				foreach (TestFailure failure in result.Failures) 
				{
					Writer.Write(i++ + ") " + failure.FailedTest);
					Exception t= failure.ThrownException;
					if (t.Message != "")
						Writer.WriteLine(" \"" + Truncate(t.Message) + "\"");
					else 
					{
						Writer.WriteLine();
						Writer.Write(GetFilteredTrace(failure.ThrownException));
					}
				}
			}
		}

		/// <summary>Prints the header of the report.</summary>
		public void PrintHeader(TestResult result) 
		{
			if (result.WasSuccessful) 
			{
				Writer.WriteLine();
				Writer.Write("OK");
				Writer.WriteLine (" (" + result.RunCount + " tests)");
                
			} 
			else 
			{
				Writer.WriteLine();
				Writer.WriteLine("FAILURES!!!");
				Writer.WriteLine("Tests Run: "+result.RunCount+ 
					", Failures: "+result.FailureCount+
					", Errors: "+result.ErrorCount);
			}
		}

		/// <summary>Runs a Suite extracted from a TestCase subclass.</summary>
		static public void Run(Type testClass) 
		{
			Run(new TestSuite(testClass));
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="suite"></param>
		static public void Run(ITest suite) 
		{
			TestRunner aTestRunner= new TestRunner();
			aTestRunner.DoRun(suite, false);
		}

		/// <summary>Runs a single test and waits until the user
		/// types RETURN.</summary>
		static public void RunAndWait(ITest suite) 
		{
			TestRunner aTestRunner= new TestRunner();
			aTestRunner.DoRun(suite, true);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="message"></param>
		protected override void RunFailed(string message) 
		{
			Console.Error.WriteLine(message);
			Environment.ExitCode = 1;
			throw new ApplicationException(message);
		}

		/// <summary>Starts a test run. Analyzes the command line arguments
		/// and runs the given test suite.</summary>
		public TestResult Start(string[] args) 
		{
			bool wait = false;
			string testCase = ProcessArguments(args, ref wait);
			if (testCase.Equals(""))
				throw new ApplicationException("Usage: NUnitConsole.exe [/wait] testCaseName, where\n"
					+ "name is the name of the TestCase class");

			try 
			{
				ITest suite = GetTest(testCase);
				return DoRun(suite, wait);
			} 
			catch (Exception e) 
			{
				throw new ApplicationException("Could not create and run test suite.", e);
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="test"></param>
		public override void StartTest(ITest test) 
		{
			lock (this) 
			{
				Writer.Write(".");
				if (fColumn++ >= 40) 
				{
					Writer.WriteLine();
					fColumn = 0;
				}
			}
		}
		/// <summary>
		/// 
		/// </summary>
		protected TextWriter Writer 
		{
			get { return fWriter; }
		}
	}
}