PageRenderTime 20ms CodeModel.GetById 7ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/interfaces/Test.cs

#
C# | 361 lines | 197 code | 47 blank | 117 comment | 22 complexity | c295125cfafffbbbc71b076667292181 MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You
  3// may obtain a copy of the license as well as information regarding
  4// copyright ownership at http://nunit.org.
  5// ****************************************************************
  6
  7namespace NUnit.Core
  8{
  9	using System;
 10	using System.Collections;
 11	using System.Collections.Specialized;
 12    using System.Threading;
 13	using System.Reflection;
 14
 15	/// <summary>
 16	///		Test Class.
 17	/// </summary>
 18	public abstract class Test : ITest, IComparable
 19    {
 20        #region Constants
 21        //private static readonly string SETCULTURE = "_SETCULTURE";
 22        private static readonly string DESCRIPTION = "_DESCRIPTION";
 23        private static readonly string IGNOREREASON = "_IGNOREREASON";
 24        private static readonly string CATEGORIES = "_CATEGORIES";
 25        #endregion
 26
 27        #region Fields
 28        /// <summary>
 29		/// TestName that identifies this test
 30		/// </summary>
 31		private TestName testName;
 32
 33		/// <summary>
 34		/// Indicates whether the test should be executed
 35		/// </summary>
 36		private RunState runState;
 37
 38		/// <summary>
 39		/// Test suite containing this test, or null
 40		/// </summary>
 41		private Test parent;
 42		
 43		/// <summary>
 44		/// A dictionary of properties, used to add information
 45		/// to tests without requiring the class to change.
 46		/// </summary>
 47		private IDictionary properties;
 48
 49		#endregion
 50
 51        #region Properties
 52        /// <summary>
 53        /// Return true if the test requires a thread
 54        /// </summary>
 55        public bool RequiresThread
 56        {
 57            get { return Properties.Contains("RequiresThread") && (bool)Properties["RequiresThread"]; }
 58        }
 59
 60        /// <summary>
 61        /// Get the desired apartment state for running the test
 62        /// </summary>
 63        public ApartmentState ApartmentState
 64        {
 65            get
 66            {
 67                return Properties.Contains("APARTMENT_STATE")
 68                    ? (ApartmentState)Properties["APARTMENT_STATE"]
 69                    : GetCurrentApartment();
 70            }
 71        }
 72
 73        /// <summary>
 74        /// Get the current apartment state of the test
 75        /// </summary>
 76        protected ApartmentState GetCurrentApartment()
 77        {
 78#if NET_2_0
 79            return Thread.CurrentThread.GetApartmentState();
 80#else
 81            return Thread.CurrentThread.ApartmentState;
 82#endif
 83        }
 84
 85        /// <summary>
 86        /// Gets a boolean value indicating whether this 
 87        /// test should run on it's own thread.
 88        /// </summary>
 89        protected virtual bool ShouldRunOnOwnThread
 90        {
 91            get
 92            {
 93                return RequiresThread
 94                    || ApartmentState != ApartmentState.Unknown
 95                    && ApartmentState != GetCurrentApartment();
 96            }
 97        }
 98		#endregion
 99
100        #region Construction
101
102        /// <summary>
103		/// Constructs a test given its name
104		/// </summary>
105		/// <param name="name">The name of the test</param>
106		protected Test( string name )
107		{
108			this.testName = new TestName();
109			this.testName.FullName = name;
110			this.testName.Name = name;
111			this.testName.TestID = new TestID();
112
113            this.runState = RunState.Runnable;
114		}
115
116		/// <summary>
117		/// Constructs a test given the path through the
118		/// test hierarchy to its parent and a name.
119		/// </summary>
120		/// <param name="pathName">The parent tests full name</param>
121		/// <param name="name">The name of the test</param>
122		protected Test( string pathName, string name ) 
123		{ 
124			this.testName = new TestName();
125			this.testName.FullName = pathName == null || pathName == string.Empty 
126				? name : pathName + "." + name;
127			this.testName.Name = name;
128			this.testName.TestID = new TestID();
129
130            this.runState = RunState.Runnable;
131		}
132
133		/// <summary>
134		/// Constructs a test given a TestName object
135		/// </summary>
136		/// <param name="testName">The TestName for this test</param>
137		protected Test( TestName testName )
138		{
139			this.testName = testName;
140			
141			this.runState = RunState.Runnable;
142		}
143
144		/// <summary>
145		/// Sets the runner id of a test and optionally its children
146		/// </summary>
147		/// <param name="runnerID">The runner id to be used</param>
148		/// <param name="recursive">True if all children should get the same id</param>
149		public void SetRunnerID( int runnerID, bool recursive )
150		{
151			this.testName.RunnerID = runnerID;
152
153			if ( recursive && this.Tests != null )
154				foreach( Test child in this.Tests )
155					child.SetRunnerID( runnerID, true );
156		}
157
158		#endregion
159
160		#region ITest Members
161
162        #region Properties
163		/// <summary>
164		/// Gets the TestName of the test
165		/// </summary>
166        public TestName TestName
167		{
168			get { return testName; }
169		}
170
171		/// <summary>
172		/// Gets a string representing the kind of test
173		/// that this object represents, for use in display.
174		/// </summary>
175        public abstract string TestType
176        {
177            get;
178        }
179
180
181		/// <summary>
182		/// Whether or not the test should be run
183		/// </summary>
184        public RunState RunState
185        {
186            get { return runState; }
187            set { runState = value; }
188        }
189
190		/// <summary>
191		/// Reason for not running the test, if applicable
192		/// </summary>
193		public string IgnoreReason
194		{
195			get { return (string)Properties[IGNOREREASON]; }
196			set 
197            {
198                if (value == null)
199                    Properties.Remove(IGNOREREASON);
200                else
201                    Properties[IGNOREREASON] = value;
202            }
203		}
204
205		/// <summary>
206		/// Gets a count of test cases represented by
207		/// or contained under this test.
208		/// </summary>
209		public virtual int TestCount 
210		{ 
211			get { return 1; } 
212		}
213
214		/// <summary>
215		/// Gets a list of categories associated with this test.
216		/// </summary>
217		public IList Categories 
218		{
219			get 
220            {
221                if (Properties[CATEGORIES] == null)
222                    Properties[CATEGORIES] = new ArrayList();
223
224                return (IList)Properties[CATEGORIES]; 
225            }
226			set 
227            {
228                Properties[CATEGORIES] = value; 
229            }
230		}
231
232		/// <summary>
233		/// Gets a description associated with this test.
234		/// </summary>
235		public String Description
236		{
237			get { return (string)Properties[DESCRIPTION]; }
238			set 
239            {
240                if (value == null)
241                    Properties.Remove(DESCRIPTION);
242                else
243                    Properties[DESCRIPTION] = value; 
244            }
245		}
246
247		/// <summary>
248		/// Gets the property dictionary for this test
249		/// </summary>
250		public IDictionary Properties
251		{
252			get 
253			{
254				if ( properties == null )
255					properties = new ListDictionary();
256
257				return properties; 
258			}
259			set
260			{
261				properties = value;
262			}
263		}
264
265		/// <summary>
266		/// Indicates whether this test is a suite
267		/// </summary>
268        public virtual bool IsSuite
269        {
270            get { return false; }
271        }
272
273
274		/// <summary>
275		/// Gets the parent test of this test
276		/// </summary>
277		ITest ITest.Parent 
278		{
279			get { return parent; }
280		}
281
282		/// <summary>
283		/// Gets the parent as a Test object.
284		/// Used by the core to set the parent.
285		/// </summary>
286		public Test Parent
287		{
288			get { return parent; }
289			set { parent = value; }
290		}
291
292		/// <summary>
293		/// Gets this test's child tests
294		/// </summary>
295		public virtual IList Tests 
296        {
297            get { return null; } 
298        }
299
300		/// <summary>
301		/// Gets the Type of the fixture used in running this test
302		/// </summary>
303		public virtual Type FixtureType
304		{
305			get { return null; }
306		}
307
308		/// <summary>
309		/// Gets or sets a fixture object for running this test
310		/// </summary>
311		public  abstract object Fixture
312		{
313			get; set;
314        }
315        #endregion
316
317        #region Methods
318		/// <summary>
319		/// Gets a count of test cases that would be run using
320		/// the specified filter.
321		/// </summary>
322		/// <param name="filter"></param>
323		/// <returns></returns>
324        public virtual int CountTestCases(ITestFilter filter)
325        {
326            if (filter.Pass(this))
327                return 1;
328
329            return 0;
330        }
331
332        /// <summary>
333        /// Runs the test under a particular filter, sending
334        /// notifications to a listener.
335        /// </summary>
336        /// <param name="listener">An event listener to receive notifications</param>
337        /// <param name="filter">A filter used in running the test</param>
338        /// <returns></returns>
339        public abstract TestResult Run(EventListener listener, ITestFilter filter);
340        #endregion
341
342        #endregion
343
344		#region IComparable Members
345		/// <summary>
346		/// Compares this test to another test for sorting purposes
347		/// </summary>
348		/// <param name="obj">The other test</param>
349		/// <returns>Value of -1, 0 or +1 depending on whether the current test is less than, equal to or greater than the other test</returns>
350		public int CompareTo(object obj)
351		{
352			Test other = obj as Test;
353			
354			if ( other == null )
355				return -1;
356
357			return this.TestName.FullName.CompareTo( other.TestName.FullName );
358		}
359		#endregion
360	}
361}