// **************************************************************** // 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.Collections; using System.Collections.Specialized; using System.Reflection; /// /// Test Class. /// public abstract class Test : ITest, IComparable { #region Fields /// /// TestName that identifies this test /// private TestName testName; /// /// Indicates whether the test should be executed /// private RunState runState; /// /// The reason for not running the test /// private string ignoreReason; /// /// Description for this test /// private string description; /// /// Test suite containing this test, or null /// private Test parent; /// /// List of categories applying to this test /// private IList categories; /// /// A dictionary of properties, used to add information /// to tests without requiring the class to change. /// private IDictionary properties; /// /// The System.Type of the fixture for this test suite, if there is one /// private Type fixtureType; /// /// The fixture object, if it has been created /// private object fixture; #endregion #region Construction /// /// Constructs a test given its name /// /// The name of the test protected Test( string name ) { this.testName = new TestName(); this.testName.FullName = name; this.testName.Name = name; this.testName.TestID = new TestID(); this.runState = RunState.Runnable; } /// /// Constructs a test given the path through the /// test hierarchy to its parent and a name. /// /// The parent tests full name /// The name of the test protected Test( string pathName, string name ) { this.testName = new TestName(); this.testName.FullName = pathName == null || pathName == string.Empty ? name : pathName + "." + name; this.testName.Name = name; this.testName.TestID = new TestID(); this.runState = RunState.Runnable; } /// /// Constructs a test given a TestName object /// /// The TestName for this test protected Test( TestName testName ) { this.testName = testName; this.runState = RunState.Runnable; } /// /// Constructs a test given a fixture type /// /// The type to use in constructiong the test protected Test( Type fixtureType ) { this.testName = new TestName(); this.testName.FullName = fixtureType.FullName; this.testName.Name = fixtureType.Namespace != null ? TestName.FullName.Substring( TestName.FullName.LastIndexOf( '.' ) + 1 ) : fixtureType.FullName; this.testName.TestID = new TestID(); this.fixtureType = fixtureType; this.RunState = RunState.Runnable; } /// /// Construct a test given a MethodInfo /// /// The method to be used protected Test( MethodInfo method ) : this( method.ReflectedType ) { this.testName.Name = method.DeclaringType == method.ReflectedType ? method.Name : method.DeclaringType.Name + "." + method.Name; this.testName.FullName = method.ReflectedType.FullName + "." + method.Name; } /// /// Sets the runner id of a test and optionally its children /// /// The runner id to be used /// True if all children should get the same id public void SetRunnerID( int runnerID, bool recursive ) { this.testName.RunnerID = runnerID; if ( recursive && this.Tests != null ) foreach( Test child in this.Tests ) child.SetRunnerID( runnerID, true ); } #endregion #region ITest Members #region Properties /// /// Gets the TestName of the test /// public TestName TestName { get { return testName; } } /// /// Gets a string representing the kind of test /// that this object represents, for use in display. /// public abstract string TestType { get; } /// /// Whether or not the test should be run /// public RunState RunState { get { return runState; } set { runState = value; } } /// /// Reason for not running the test, if applicable /// public string IgnoreReason { get { return ignoreReason; } set { ignoreReason = value; } } /// /// Gets a count of test cases represented by /// or contained under this test. /// public virtual int TestCount { get { return 1; } } /// /// Gets a list of categories associated with this test. /// public IList Categories { get { return categories; } set { categories = value; } } /// /// Gets a desctiption associated with this test. /// public String Description { get { return description; } set { description = value; } } /// /// Gets the property dictionary for this test /// public IDictionary Properties { get { if ( properties == null ) properties = new ListDictionary(); return properties; } set { properties = value; } } /// /// Indicates whether this test is a suite /// public abstract bool IsSuite { get; } /// /// Gets the parent test of this test /// ITest ITest.Parent { get { return parent; } } /// /// Gets the parent as a Test object. /// Used by the core to set the parent. /// public Test Parent { get { return parent; } set { parent = value; } } /// /// Gets this test's child tests /// public abstract IList Tests { get; } /// /// Gets the Type of the fixture used in running this test /// public Type FixtureType { get { return fixtureType; } } /// /// Gets or sets a fixture object for running this test /// public object Fixture { get { return fixture; } set { fixture = value; } } #endregion #region Methods /// /// Gets a count of test cases that would be run using /// the specified filter. /// /// /// public abstract int CountTestCases(ITestFilter filter); #endregion #endregion #region Abstract Run Methods /// /// Runs the test, sending notifications to a listener. /// /// An event listener to receive notifications /// A TestResult public abstract TestResult Run(EventListener listener); /// /// Runs the test under a particular filter, sending /// notifications to a listener. /// /// An event listener to receive notifications /// A filter used in running the test /// public abstract TestResult Run(EventListener listener, ITestFilter filter); #endregion #region IComparable Members /// /// Compares this test to another test for sorting purposes /// /// The other test /// Value of -1, 0 or +1 depending on whether the current test is less than, equal to or greater than the other test public int CompareTo(object obj) { Test other = obj as Test; if ( other == null ) return -1; return this.TestName.FullName.CompareTo( other.TestName.FullName ); } #endregion } }