File: TestResult.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 (428 lines) | stat: -rw-r--r-- 11,534 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// ****************************************************************
// This is free software licensed under the NUnit license. You
// may obtain a copy of the license as well as information regarding
// copyright ownership at http://nunit.org/?p=license&r=2.4.
// ****************************************************************

namespace NUnit.Core
{
	using System;
	using System.Text;

	/// <summary>
	/// The TestResult abstract class represents
	/// the result of a test and is used to
	/// communicate results across AppDomains.
	/// </summary>
	/// 
	[Serializable]
	public abstract class TestResult
	{
		#region Fields
		/// <summary>
		/// Indicates whether the test was executed or not
		/// </summary>
		private RunState runState;

		/// <summary>
		/// Indicates the result of the test
		/// </summary>
		private ResultState resultState;

		/// <summary>
		/// Indicates the location of a failure
		/// </summary>
        private FailureSite failureSite;

		/// <summary>
		/// The elapsed time for executing this test
		/// </summary>
		private double time = 0.0;

		/// <summary>
		/// The name of the test
		/// </summary>
		private string name;

		/// <summary>
		/// The test that this result pertains to
		/// </summary>
		private TestInfo test;

		/// <summary>
		/// The stacktrace at the point of failure
		/// </summary>
		private string stackTrace;

		/// <summary>
		/// Description of this test
		/// </summary>
		private string description;

		/// <summary>
		/// Message giving the reason for failure
		/// </summary>
		protected string messageString;

		/// <summary>
		/// Number of asserts executed by this test
		/// </summary>
		private int assertCount = 0;

		#endregion

		#region Protected Constructor
		/// <summary>
		/// Protected constructor constructs a test result given
		/// a test and a name.
		/// </summary>
		/// <param name="test">The test to be used</param>
		/// <param name="name">Name for this result</param>
		protected TestResult(TestInfo test, string name)
		{
			this.name = name;
			this.test = test;
            this.RunState = RunState.Runnable;
            if (test != null)
            {
                this.description = test.Description;
                this.runState = test.RunState;
                this.messageString = test.IgnoreReason;
            }
        }
		#endregion

        #region Properties

		/// <summary>
		/// Gets the RunState of the result, which indicates
		/// whether or not it has executed and why.
		/// </summary>
        public RunState RunState
        {
            get { return runState; }
            set { runState = value; }
        }

		/// <summary>
		/// Gets the ResultState of the test result, which 
		/// indicates the success or failure of the test.
		/// </summary>
        public ResultState ResultState
        {
            get { return resultState; }
        }

		/// <summary>
		/// Gets the stage of the test in which a failure
		/// or error occured.
		/// </summary>
        public FailureSite FailureSite
        {
            get { return failureSite; }
        }

		/// <summary>
		/// Indicates whether the test executed
		/// </summary>
        public bool Executed
        {
            get { return runState == RunState.Executed; }
        }

		/// <summary>
		/// Gets the name of the test result
		/// </summary>
        public virtual string Name
        {
            get { return name; }
        }

		/// <summary>
		/// Gets the test associated with this result
		/// </summary>
        public ITest Test
        {
            get { return test; }
        }

		/// <summary>
		/// Indicates whether the test ran successfully
		/// </summary>
        public virtual bool IsSuccess
        {
            // TODO: Redefine this more precisely
            get { return !IsFailure; }
            //get { return resultState == ResultState.Success; }
        }

        /// <summary>
        /// Indicates whether the test failed
        /// </summary>
		// TODO: Distinguish errors from failures
        public virtual bool IsFailure
        {
            get { return resultState == ResultState.Failure || resultState == ResultState.Error; }
        }

		/// <summary>
		/// Gets a description associated with the test
		/// </summary>
        public virtual string Description
        {
            get { return description; }
            set { description = value; }
        }

		/// <summary>
		/// Gets the elapsed time for running the test
		/// </summary>
        public double Time
        {
            get { return time; }
            set { time = value; }
        }

		/// <summary>
		/// Gets the message associated with a test
		/// failure or with not running the test
		/// </summary>
        public string Message
        {
            get { return messageString; }
        }

		/// <summary>
		/// Gets any stacktrace associated with an
		/// error or failure.
		/// </summary>
        public virtual string StackTrace
        {
            get
            {
                return stackTrace;
            }
            set
            {
                stackTrace = value;
            }
        }

		/// <summary>
		/// Gets or sets the count of asserts executed
		/// when running the test.
		/// </summary>
        public int AssertCount
        {
            get { return assertCount; }
            set { assertCount = value; }
        }

        #endregion

        #region Public Methods
        /// <summary>
		/// Mark the test as succeeding
		/// </summary>
		public void Success() 
		{ 
			this.runState = RunState.Executed;
			this.resultState = ResultState.Success; 
		}

		/// <summary>
		/// Mark the test as ignored.
		/// </summary>
		/// <param name="reason">The reason the test was not run</param>
		public void Ignore(string reason)
		{
			Ignore( reason, null );
		}

		/// <summary>
		/// Mark the test as ignored.
		/// </summary>
		/// <param name="ex">The ignore exception that was thrown</param>
		public void Ignore( Exception ex )
		{
			Ignore( ex.Message, BuildStackTrace( ex ) );
		}

		/// <summary>
		/// Mark the test as ignored.
		/// </summary>
		/// <param name="reason">The reason the test was not run</param>
		/// <param name="stackTrace">Stack trace giving the location of the command</param>
		public void Ignore(string reason, string stackTrace)
		{
			NotRun( RunState.Ignored, reason, stackTrace );
		}

		/// <summary>
		/// Mark the test as skipped.
		/// </summary>
		/// <param name="reason">The reason the test was not run</param>
		public void Skip(string reason)
		{
			Skip( reason, null );
		}

		/// <summary>
		/// Mark the test as ignored.
		/// </summary>
		/// <param name="ex">The ignore exception that was thrown</param>
		public void Skip( Exception ex )
		{
			Skip( ex.Message, BuildStackTrace( ex ) );
		}

		/// <summary>
		/// Mark the test as skipped.
		/// </summary>
		/// <param name="reason">The reason the test was not run</param>
		/// <param name="stackTrace">Stack trace giving the location of the command</param>
		public void Skip(string reason, string stackTrace)
		{
			NotRun( RunState.Skipped, reason, stackTrace );
		}

		/// <summary>
		/// Mark the test as Not Run - either skipped or ignored
		/// </summary>
		/// <param name="runState">The RunState to use in the result</param>
		/// <param name="reason">The reason the test was not run</param>
		/// <param name="stackTrace">Stack trace giving the location of the command</param>
		public void NotRun(RunState runState, string reason, string stackTrace)
		{
			this.runState = runState;
			this.messageString = reason;
			this.stackTrace = stackTrace;
		}


		/// <summary>
		/// Mark the test as a failure due to an
		/// assertion having failed.
		/// </summary>
		/// <param name="message">Message to display</param>
		/// <param name="stackTrace">Stack trace giving the location of the failure</param>
		public void Failure(string message, string stackTrace)
        {
            Failure(message, stackTrace, FailureSite.Test);
        }

		/// <summary>
		/// Mark the test as a failure due to an
		/// assertion having failed.
		/// </summary>
		/// <param name="message">Message to display</param>
		/// <param name="stackTrace">Stack trace giving the location of the failure</param>
		/// <param name="failureSite">The site of the failure</param>
		public void Failure(string message, string stackTrace, FailureSite failureSite )
		{
			this.runState = RunState.Executed;
			this.resultState = ResultState.Failure;
            this.failureSite = failureSite;
			this.messageString = message;
			this.stackTrace = stackTrace;
		}

		/// <summary>
		/// Marks the result as an error due to an exception thrown
		/// by the test.
		/// </summary>
		/// <param name="exception">The exception that was caught</param>
        public void Error(Exception exception)
        {
            Error(exception, FailureSite.Test);
        }

		/// <summary>
		/// Marks the result as an error due to an exception thrown
		/// from the indicated FailureSite.
		/// </summary>
		/// <param name="exception">The exception that was caught</param>
		/// <param name="failureSite">The site from which it was thrown</param>
		public void Error( Exception exception, FailureSite failureSite )
		{
			this.runState = RunState.Executed;
			this.resultState = ResultState.Error;
            this.failureSite = failureSite;

            string message = BuildMessage(exception);
            string stackTrace = BuildStackTrace(exception);

            if (failureSite == FailureSite.TearDown)
            {
                message = "TearDown : " + message;
                stackTrace = "--TearDown" + Environment.NewLine + stackTrace;

                if (this.messageString != null)
                    message = this.messageString + Environment.NewLine + message;
                if (this.stackTrace != null)
                    stackTrace = this.stackTrace + Environment.NewLine + stackTrace;
            }

            this.messageString = message;
            this.stackTrace = stackTrace;
		}
		#endregion

		#region Exception Helpers

		private string BuildMessage(Exception exception)
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat( "{0} : {1}", exception.GetType().ToString(), exception.Message );

			Exception inner = exception.InnerException;
			while( inner != null )
			{
				sb.Append( Environment.NewLine );
				sb.AppendFormat( "  ----> {0} : {1}", inner.GetType().ToString(), inner.Message );
				inner = inner.InnerException;
			}

			return sb.ToString();
		}
		
		private string BuildStackTrace(Exception exception)
		{
            StringBuilder sb = new StringBuilder( GetStackTrace( exception ) );

            Exception inner = exception.InnerException;
            while( inner != null )
            {
                sb.Append( Environment.NewLine );
                sb.Append( "--" );
                sb.Append( inner.GetType().Name );
                sb.Append( Environment.NewLine );
                sb.Append( GetStackTrace( inner ) );

                inner = inner.InnerException;
            }

            return sb.ToString();
		}

		private string GetStackTrace(Exception exception)
		{
			try
			{
				return exception.StackTrace;
			}
			catch( Exception )
			{
				return "No stack trace available";
			}
		}

		#endregion

		/// <summary>
		/// Abstract method that accepts a ResultVisitor
		/// </summary>
		/// <param name="visitor">The visitor</param>
		public abstract void Accept(ResultVisitor visitor);
	}
}