PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/interfaces/TestInfo.cs

#
C# | 254 lines | 126 code | 33 blank | 95 comment | 11 complexity | 182702afb31ec1ba81b0ff2624109be5 MD5 | raw file
  1// ****************************************************************
  2// Copyright 2007, Charlie Poole
  3// This is free software licensed under the NUnit license. You may
  4// obtain a copy of the license at http://nunit.org
  5// ****************************************************************
  6using System;
  7using System.Collections;
  8using System.Collections.Specialized;
  9
 10namespace NUnit.Core
 11{
 12	/// <summary>
 13	/// TestInfo holds common info about a test. It represents only
 14	/// a single test or a suite and contains no references to other
 15	/// tests. Since it is informational only, it can easily be passed
 16	/// around using .Net remoting.
 17	/// 
 18	/// TestInfo is used directly in all EventListener events and in
 19	/// TestResults. It contains an ID, which can be used by a 
 20	/// runner to locate the actual test.
 21	/// 
 22	/// TestInfo also serves as the base class for TestNode, which
 23	/// adds hierarchical information and is used in client code to
 24	/// maintain a visible image of the structure of the tests.
 25	/// </summary>
 26	[Serializable]
 27	public class TestInfo : ITest
 28	{
 29		#region Instance Variables
 30		/// <summary>
 31		/// TestName that identifies this test
 32		/// </summary>
 33		private TestName testName;
 34
 35		private string testType;
 36
 37        private RunState runState;
 38
 39		/// <summary>
 40		/// Reason for not running the test
 41		/// </summary>
 42		private string ignoreReason;
 43
 44		/// <summary>
 45		/// Number of test cases in this test or suite
 46		/// </summary>
 47		private int testCaseCount;
 48
 49		/// <summary>
 50		/// True if this is a suite
 51		/// </summary>
 52		private bool isSuite;
 53
 54		/// <summary>
 55		/// The test description
 56		/// </summary>
 57		private string description;
 58
 59		/// <summary>
 60		/// A list of all the categories assigned to a test
 61		/// </summary>
 62		private ArrayList categories = new ArrayList();
 63
 64		/// <summary>
 65		/// A dictionary of properties, used to add information
 66		/// to tests without requiring the class to change.
 67		/// </summary>
 68		private ListDictionary properties = new ListDictionary();
 69
 70		#endregion
 71
 72		#region Constructors
 73		/// <summary>
 74		/// Construct from an ITest
 75		/// </summary>
 76		/// <param name="test">Test from which a TestNode is to be constructed</param>
 77		public TestInfo( ITest test )
 78		{
 79			this.testName = (TestName)test.TestName.Clone();
 80			this.testType = test.TestType;
 81
 82            this.runState = test.RunState;
 83			this.ignoreReason = test.IgnoreReason;
 84			this.description = test.Description;
 85			this.isSuite = test.IsSuite;
 86
 87			if (test.Categories != null) 
 88				this.categories.AddRange(test.Categories);
 89			if (test.Properties != null)
 90			{
 91				this.properties = new ListDictionary();
 92				foreach( DictionaryEntry entry in test.Properties )
 93					this.properties.Add( entry.Key, entry.Value );
 94			}
 95
 96			this.testCaseCount = test.TestCount;
 97		}
 98
 99		/// <summary>
100		/// Construct as a parent to multiple tests.
101		/// </summary>
102		/// <param name="testName">The name to use for the new test</param>
103		/// <param name="tests">An array of child tests</param>
104		public TestInfo( TestName testName, ITest[] tests )
105		{
106			this.testName = testName;
107			this.testType = "Test Project";
108
109            this.runState = RunState.Runnable;
110			this.ignoreReason = null;
111			this.description = null;
112			this.isSuite = true;
113
114            if ( tests != null )
115			    foreach( ITest test in tests )
116    				this.testCaseCount += test.TestCount;
117		}
118
119		/// <summary>
120		/// Construct given a test name
121		/// </summary>
122		/// <param name="testName">The TestName for the new test</param>
123		public TestInfo( TestName testName ) : this( testName, null) { }
124		#endregion
125
126		#region Properties
127		/// <summary>
128		/// Gets the completely specified name of the test
129		/// encapsulated in a TestName object.
130		/// </summary>
131		public TestName TestName
132		{
133			get { return testName; }
134		}
135
136		/// <summary>
137		/// Gets a string representing the kind of test this
138		/// object represents for display purposes.
139		/// </summary>
140		public string TestType
141		{
142			get { return testType; }
143		}
144
145		/// <summary>
146		/// The test description 
147		/// </summary>
148		public string Description
149		{
150			get { return description; }
151			set { description = value; }
152		}
153
154		/// <summary>
155		/// Gets the RunState for this test
156		/// </summary>
157        public RunState RunState
158        {
159            get { return runState; }
160            set { runState = value; }
161        }
162
163		/// <summary>
164		/// The reason for ignoring a test
165		/// </summary>
166		public string IgnoreReason
167		{
168			get { return ignoreReason; }
169			set { ignoreReason = value; }
170		}
171
172		/// <summary>
173		/// Count of test cases in this test.
174		/// </summary>
175		public int TestCount
176		{ 
177			get { return testCaseCount; } 
178		}
179
180		/// <summary>
181		///  Gets the parent test of this test
182		/// </summary>
183		public virtual ITest Parent
184		{
185			get { return null; }
186		}
187
188		/// <summary>
189		/// Gets a list of the categories applied to this test
190		/// </summary>
191		public IList Categories 
192		{
193			get { return categories; }
194		}
195
196		/// <summary>
197		/// Gets a list of any child tests
198		/// </summary>
199		public virtual IList Tests
200		{
201			get { return null; }
202		}
203
204		/// <summary>
205		/// True if this is a suite, false if a test case
206		/// </summary>
207		public bool IsSuite
208		{
209			get { return isSuite; }
210		}
211
212		/// <summary>
213		/// Gets the Properties dictionary for this test
214		/// </summary>
215		public IDictionary Properties
216		{
217			get 
218			{
219				if ( properties == null )
220					properties = new ListDictionary();
221
222				return properties; 
223			}
224		}
225		#endregion
226
227        #region Methods
228		/// <summary>
229		/// Counts the test cases that would be run if this
230		/// test were executed using the provided filter.
231		/// </summary>
232		/// <param name="filter">The filter to apply</param>
233		/// <returns>A count of test cases</returns>
234        public virtual int CountTestCases(ITestFilter filter)
235        {
236            if (filter.IsEmpty)
237                return TestCount;
238
239            if (!isSuite)
240                return filter.Pass(this) ? 1 : 0;
241
242            int count = 0;
243            if (filter.Pass(this))
244            {
245                foreach (ITest test in Tests)
246                {
247                    count += test.CountTestCases(filter);
248                }
249            }
250            return count;
251        }
252        #endregion
253    }
254}