1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
4 ' Copyright © 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright © 2000-2003 Philip A. Craig
7 ' This software is provided 'as-is', without any express or implied warranty. In no
8 ' event will the authors be held liable for any damages arising from the use of this
11 ' Permission is granted to anyone to use this software for any purpose, including
12 ' commercial applications, and to alter it and redistribute it freely, subject to the
13 ' following restrictions:
15 ' 1. The origin of this software must not be misrepresented; you must not claim that
16 ' you wrote the original software. If you use this software in a product, an
17 ' acknowledgment (see the following) in the product documentation is required.
19 ' Portions Copyright © 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright © 2000-2003 Philip A. Craig
22 ' 2. Altered source versions must be plainly marked as such, and must not be
23 ' misrepresented as being the original software.
25 ' 3. This notice may not be removed or altered from any source distribution.
27 '***********************************************************************************/
33 using System.Collections;
34 using System.Reflection;
39 public abstract class Test : LongLivingMarshalByRefObject, ITest, IComparable
41 #region Private Fields
46 private string testName;
49 /// Full Name of the test
51 private string fullName;
54 /// Int used to distinguish suites of the same
55 /// name across multiple assemblies.
57 private int assemblyKey;
60 /// Whether or not the test should be run
62 private bool shouldRun;
65 /// Reason for not running the test, if applicable
67 private string ignoreReason;
70 /// Description for this test
72 private string description;
75 /// Test suite containing this test, or null
77 private TestSuite parent;
80 /// List of categories applying to this test
82 private IList categories;
85 /// True if the test had the Explicit attribute
87 private bool isExplicit;
93 public Test( string name ) : this( name, 0 ) { }
95 public Test( string name, int assemblyKey )
97 fullName = testName = name;
98 this.assemblyKey = assemblyKey;
101 protected Test( string pathName, string testName )
102 : this( pathName, testName, 0 ) { }
104 protected Test( string pathName, string testName, int assemblyKey )
106 fullName = pathName + "." + testName;
107 this.testName = testName;
108 this.assemblyKey = assemblyKey;
118 get { return testName; }
121 public string FullName
123 get { return fullName; }
127 /// If the name is a path, this just returns the file part
129 public string ShortName
134 int val = name.LastIndexOf("\\");
136 name = name.Substring(val+1);
142 /// Int used to distinguish suites of the same
143 /// name across multiple assemblies.
145 public int AssemblyKey
147 get { return assemblyKey; }
148 set { assemblyKey = value; }
152 /// Key used to look up a test in a hash table
154 public string UniqueName
156 get { return string.Format( "[{0}]{1}", assemblyKey, fullName ); }
160 /// Whether or not the test should be run
162 public virtual bool ShouldRun
164 get { return shouldRun; }
165 set { shouldRun = value; }
169 /// Reason for not running the test, if applicable
171 public string IgnoreReason
173 get { return ignoreReason; }
174 set { ignoreReason = value; }
177 public TestSuite Parent
179 get { return parent; }
180 set { parent = value; }
183 public string TestPath
187 string testPath = "";
189 testPath = parent.TestPath;
190 return testPath + FullName;
194 public IList Categories
196 get { return categories; }
197 set { categories = value; }
200 public bool HasCategory( string name )
202 return categories != null && categories.Contains( name );
205 public bool HasCategory( IList names )
207 if ( categories == null )
210 foreach( string name in names )
211 if ( categories.Contains( name ) )
217 public bool IsDescendant(Test test)
221 return parent == test || parent.IsDescendant(test);
227 public String Description
229 get { return description; }
230 set { description = value; }
233 public bool IsExplicit
235 get { return isExplicit; }
236 set { isExplicit = value; }
241 #region Abstract Methods and Properties
244 /// Count of the test cases ( 1 if this is a test case )
246 public abstract int CountTestCases();
247 public abstract int CountTestCases(IFilter filter);
249 public abstract bool IsSuite { get; }
250 public abstract bool IsFixture{ get; }
251 public abstract bool IsTestCase{ get; }
252 public abstract ArrayList Tests { get; }
254 public abstract bool Filter(IFilter filter);
256 public abstract TestResult Run( EventListener listener );
257 public abstract TestResult Run(EventListener listener, IFilter filter);
261 #region Protected Helper Methods
263 protected MethodInfo FindMethodByAttribute(object fixture, Type type)
265 foreach(MethodInfo method in fixture.GetType().GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.NonPublic))
267 if(method.IsDefined(type,true))
275 protected void InvokeMethod(MethodInfo method, object fixture)
281 method.Invoke(fixture, null);
283 catch(TargetInvocationException e)
285 Exception inner = e.InnerException;
286 throw new NunitException("Rethrown",inner);
293 #region IComparable Members
295 public int CompareTo(object obj)
297 Test other = obj as Test;
302 return this.FullName.CompareTo( other.FullName );