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 // ****************************************************************
10 using System
.Collections
;
11 using System
.Collections
.Specialized
;
12 using System
.Reflection
;
17 public abstract class Test
: ITest
, IComparable
21 /// TestName that identifies this test
23 private TestName testName
;
26 /// Indicates whether the test should be executed
28 private RunState runState
;
31 /// The reason for not running the test
33 private string ignoreReason
;
36 /// Description for this test
38 private string description
;
41 /// Test suite containing this test, or null
46 /// List of categories applying to this test
48 private IList categories
;
51 /// A dictionary of properties, used to add information
52 /// to tests without requiring the class to change.
54 private IDictionary properties
;
57 /// The System.Type of the fixture for this test suite, if there is one
59 private Type fixtureType
;
62 /// The fixture object, if it has been created
64 private object fixture
;
71 /// Constructs a test given its name
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
;
85 /// Constructs a test given the path through the
86 /// test hierarchy to its parent and a name.
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
;
102 /// Constructs a test given a TestName object
104 /// <param name="testName">The TestName for this test</param>
105 protected Test( TestName testName
)
107 this.testName
= testName
;
109 this.runState
= RunState
.Runnable
;
113 /// Constructs a test given a fixture type
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
;
130 /// Construct a test given a MethodInfo
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
;
142 /// Sets the runner id of a test and optionally its children
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 );
157 #region ITest Members
161 /// Gets the TestName of the test
163 public TestName TestName
165 get { return testName; }
169 /// Gets a string representing the kind of test
170 /// that this object represents, for use in display.
172 public abstract string TestType { get; }
175 /// Whether or not the test should be run
177 public RunState RunState
179 get { return runState; }
180 set { runState = value; }
184 /// Reason for not running the test, if applicable
186 public string IgnoreReason
188 get { return ignoreReason; }
189 set { ignoreReason = value; }
193 /// Gets a count of test cases represented by
194 /// or contained under this test.
196 public virtual int TestCount
202 /// Gets a list of categories associated with this test.
204 public IList Categories
206 get { return categories; }
207 set { categories = value; }
211 /// Gets a desctiption associated with this test.
213 public String Description
215 get { return description; }
216 set { description = value; }
220 /// Gets the property dictionary for this test
222 public IDictionary Properties
226 if ( properties
== null )
227 properties
= new ListDictionary();
238 /// Indicates whether this test is a suite
240 public abstract bool IsSuite { get; }
243 /// Gets the parent test of this test
247 get { return parent; }
251 /// Gets the parent as a Test object.
252 /// Used by the core to set the parent.
256 get { return parent; }
257 set { parent = value; }
261 /// Gets this test's child tests
263 public abstract IList Tests { get; }
266 /// Gets the Type of the fixture used in running this test
268 public Type FixtureType
270 get { return fixtureType; }
274 /// Gets or sets a fixture object for running this test
276 public object Fixture
278 get { return fixture; }
279 set { fixture = value; }
285 /// Gets a count of test cases that would be run using
286 /// the specified filter.
288 /// <param name="filter"></param>
289 /// <returns></returns>
290 public abstract int CountTestCases(ITestFilter filter
);
295 #region Abstract Run Methods
297 /// Runs the test, sending notifications to a listener.
299 /// <param name="listener">An event listener to receive notifications</param>
300 /// <returns>A TestResult</returns>
301 public abstract TestResult
Run(EventListener listener
);
304 /// Runs the test under a particular filter, sending
305 /// notifications to a listener.
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
);
313 #region IComparable Members
315 /// Compares this test to another test for sorting purposes
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
;
326 return this.TestName
.FullName
.CompareTo( other
.TestName
.FullName
);