2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / nunit24 / NUnitCore / interfaces / Test.cs
blob071b19c8d07e381989badeea69335e0671b7bdc8
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/?p=license&r=2.4.
5 // ****************************************************************
7 namespace NUnit.Core
9 using System;
10 using System.Collections;
11 using System.Collections.Specialized;
12 using System.Reflection;
14 /// <summary>
15 /// Test Class.
16 /// </summary>
17 public abstract class Test : ITest, IComparable
19 #region Fields
20 /// <summary>
21 /// TestName that identifies this test
22 /// </summary>
23 private TestName testName;
25 /// <summary>
26 /// Indicates whether the test should be executed
27 /// </summary>
28 private RunState runState;
30 /// <summary>
31 /// The reason for not running the test
32 /// </summary>
33 private string ignoreReason;
35 /// <summary>
36 /// Description for this test
37 /// </summary>
38 private string description;
40 /// <summary>
41 /// Test suite containing this test, or null
42 /// </summary>
43 private Test parent;
45 /// <summary>
46 /// List of categories applying to this test
47 /// </summary>
48 private IList categories;
50 /// <summary>
51 /// A dictionary of properties, used to add information
52 /// to tests without requiring the class to change.
53 /// </summary>
54 private IDictionary properties;
56 /// <summary>
57 /// The System.Type of the fixture for this test suite, if there is one
58 /// </summary>
59 private Type fixtureType;
61 /// <summary>
62 /// The fixture object, if it has been created
63 /// </summary>
64 private object fixture;
66 #endregion
68 #region Construction
70 /// <summary>
71 /// Constructs a test given its name
72 /// </summary>
73 /// <param name="name">The name of the test</param>
74 protected Test( string name )
76 this.testName = new TestName();
77 this.testName.FullName = name;
78 this.testName.Name = name;
79 this.testName.TestID = new TestID();
81 this.runState = RunState.Runnable;
84 /// <summary>
85 /// Constructs a test given the path through the
86 /// test hierarchy to its parent and a name.
87 /// </summary>
88 /// <param name="pathName">The parent tests full name</param>
89 /// <param name="name">The name of the test</param>
90 protected Test( string pathName, string name )
92 this.testName = new TestName();
93 this.testName.FullName = pathName == null || pathName == string.Empty
94 ? name : pathName + "." + name;
95 this.testName.Name = name;
96 this.testName.TestID = new TestID();
98 this.runState = RunState.Runnable;
101 /// <summary>
102 /// Constructs a test given a TestName object
103 /// </summary>
104 /// <param name="testName">The TestName for this test</param>
105 protected Test( TestName testName )
107 this.testName = testName;
109 this.runState = RunState.Runnable;
112 /// <summary>
113 /// Constructs a test given a fixture type
114 /// </summary>
115 /// <param name="fixtureType">The type to use in constructiong the test</param>
116 protected Test( Type fixtureType )
118 this.testName = new TestName();
119 this.testName.FullName = fixtureType.FullName;
120 this.testName.Name = fixtureType.Namespace != null
121 ? TestName.FullName.Substring( TestName.FullName.LastIndexOf( '.' ) + 1 )
122 : fixtureType.FullName;
123 this.testName.TestID = new TestID();
125 this.fixtureType = fixtureType;
126 this.RunState = RunState.Runnable;
129 /// <summary>
130 /// Construct a test given a MethodInfo
131 /// </summary>
132 /// <param name="method">The method to be used</param>
133 protected Test( MethodInfo method )
134 : this( method.ReflectedType )
136 this.testName.Name = method.DeclaringType == method.ReflectedType
137 ? method.Name : method.DeclaringType.Name + "." + method.Name;
138 this.testName.FullName = method.ReflectedType.FullName + "." + method.Name;
141 /// <summary>
142 /// Sets the runner id of a test and optionally its children
143 /// </summary>
144 /// <param name="runnerID">The runner id to be used</param>
145 /// <param name="recursive">True if all children should get the same id</param>
146 public void SetRunnerID( int runnerID, bool recursive )
148 this.testName.RunnerID = runnerID;
150 if ( recursive && this.Tests != null )
151 foreach( Test child in this.Tests )
152 child.SetRunnerID( runnerID, true );
155 #endregion
157 #region ITest Members
159 #region Properties
160 /// <summary>
161 /// Gets the TestName of the test
162 /// </summary>
163 public TestName TestName
165 get { return testName; }
168 /// <summary>
169 /// Gets a string representing the kind of test
170 /// that this object represents, for use in display.
171 /// </summary>
172 public abstract string TestType { get; }
174 /// <summary>
175 /// Whether or not the test should be run
176 /// </summary>
177 public RunState RunState
179 get { return runState; }
180 set { runState = value; }
183 /// <summary>
184 /// Reason for not running the test, if applicable
185 /// </summary>
186 public string IgnoreReason
188 get { return ignoreReason; }
189 set { ignoreReason = value; }
192 /// <summary>
193 /// Gets a count of test cases represented by
194 /// or contained under this test.
195 /// </summary>
196 public virtual int TestCount
198 get { return 1; }
201 /// <summary>
202 /// Gets a list of categories associated with this test.
203 /// </summary>
204 public IList Categories
206 get { return categories; }
207 set { categories = value; }
210 /// <summary>
211 /// Gets a desctiption associated with this test.
212 /// </summary>
213 public String Description
215 get { return description; }
216 set { description = value; }
219 /// <summary>
220 /// Gets the property dictionary for this test
221 /// </summary>
222 public IDictionary Properties
224 get
226 if ( properties == null )
227 properties = new ListDictionary();
229 return properties;
233 properties = value;
237 /// <summary>
238 /// Indicates whether this test is a suite
239 /// </summary>
240 public abstract bool IsSuite { get; }
242 /// <summary>
243 /// Gets the parent test of this test
244 /// </summary>
245 ITest ITest.Parent
247 get { return parent; }
250 /// <summary>
251 /// Gets the parent as a Test object.
252 /// Used by the core to set the parent.
253 /// </summary>
254 public Test Parent
256 get { return parent; }
257 set { parent = value; }
260 /// <summary>
261 /// Gets this test's child tests
262 /// </summary>
263 public abstract IList Tests { get; }
265 /// <summary>
266 /// Gets the Type of the fixture used in running this test
267 /// </summary>
268 public Type FixtureType
270 get { return fixtureType; }
273 /// <summary>
274 /// Gets or sets a fixture object for running this test
275 /// </summary>
276 public object Fixture
278 get { return fixture; }
279 set { fixture = value; }
281 #endregion
283 #region Methods
284 /// <summary>
285 /// Gets a count of test cases that would be run using
286 /// the specified filter.
287 /// </summary>
288 /// <param name="filter"></param>
289 /// <returns></returns>
290 public abstract int CountTestCases(ITestFilter filter);
291 #endregion
293 #endregion
295 #region Abstract Run Methods
296 /// <summary>
297 /// Runs the test, sending notifications to a listener.
298 /// </summary>
299 /// <param name="listener">An event listener to receive notifications</param>
300 /// <returns>A TestResult</returns>
301 public abstract TestResult Run(EventListener listener);
303 /// <summary>
304 /// Runs the test under a particular filter, sending
305 /// notifications to a listener.
306 /// </summary>
307 /// <param name="listener">An event listener to receive notifications</param>
308 /// <param name="filter">A filter used in running the test</param>
309 /// <returns></returns>
310 public abstract TestResult Run(EventListener listener, ITestFilter filter);
311 #endregion
313 #region IComparable Members
314 /// <summary>
315 /// Compares this test to another test for sorting purposes
316 /// </summary>
317 /// <param name="obj">The other test</param>
318 /// <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>
319 public int CompareTo(object obj)
321 Test other = obj as Test;
323 if ( other == null )
324 return -1;
326 return this.TestName.FullName.CompareTo( other.TestName.FullName );
328 #endregion