PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/interfaces/Test.cs

#
C# | 361 lines | 197 code | 47 blank | 117 comment | 22 complexity | c295125cfafffbbbc71b076667292181 MD5 | raw file
Possible License(s): GPL-2.0
  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.
  5. // ****************************************************************
  6. namespace NUnit.Core
  7. {
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Specialized;
  11. using System.Threading;
  12. using System.Reflection;
  13. /// <summary>
  14. /// Test Class.
  15. /// </summary>
  16. public abstract class Test : ITest, IComparable
  17. {
  18. #region Constants
  19. //private static readonly string SETCULTURE = "_SETCULTURE";
  20. private static readonly string DESCRIPTION = "_DESCRIPTION";
  21. private static readonly string IGNOREREASON = "_IGNOREREASON";
  22. private static readonly string CATEGORIES = "_CATEGORIES";
  23. #endregion
  24. #region Fields
  25. /// <summary>
  26. /// TestName that identifies this test
  27. /// </summary>
  28. private TestName testName;
  29. /// <summary>
  30. /// Indicates whether the test should be executed
  31. /// </summary>
  32. private RunState runState;
  33. /// <summary>
  34. /// Test suite containing this test, or null
  35. /// </summary>
  36. private Test parent;
  37. /// <summary>
  38. /// A dictionary of properties, used to add information
  39. /// to tests without requiring the class to change.
  40. /// </summary>
  41. private IDictionary properties;
  42. #endregion
  43. #region Properties
  44. /// <summary>
  45. /// Return true if the test requires a thread
  46. /// </summary>
  47. public bool RequiresThread
  48. {
  49. get { return Properties.Contains("RequiresThread") && (bool)Properties["RequiresThread"]; }
  50. }
  51. /// <summary>
  52. /// Get the desired apartment state for running the test
  53. /// </summary>
  54. public ApartmentState ApartmentState
  55. {
  56. get
  57. {
  58. return Properties.Contains("APARTMENT_STATE")
  59. ? (ApartmentState)Properties["APARTMENT_STATE"]
  60. : GetCurrentApartment();
  61. }
  62. }
  63. /// <summary>
  64. /// Get the current apartment state of the test
  65. /// </summary>
  66. protected ApartmentState GetCurrentApartment()
  67. {
  68. #if NET_2_0
  69. return Thread.CurrentThread.GetApartmentState();
  70. #else
  71. return Thread.CurrentThread.ApartmentState;
  72. #endif
  73. }
  74. /// <summary>
  75. /// Gets a boolean value indicating whether this
  76. /// test should run on it's own thread.
  77. /// </summary>
  78. protected virtual bool ShouldRunOnOwnThread
  79. {
  80. get
  81. {
  82. return RequiresThread
  83. || ApartmentState != ApartmentState.Unknown
  84. && ApartmentState != GetCurrentApartment();
  85. }
  86. }
  87. #endregion
  88. #region Construction
  89. /// <summary>
  90. /// Constructs a test given its name
  91. /// </summary>
  92. /// <param name="name">The name of the test</param>
  93. protected Test( string name )
  94. {
  95. this.testName = new TestName();
  96. this.testName.FullName = name;
  97. this.testName.Name = name;
  98. this.testName.TestID = new TestID();
  99. this.runState = RunState.Runnable;
  100. }
  101. /// <summary>
  102. /// Constructs a test given the path through the
  103. /// test hierarchy to its parent and a name.
  104. /// </summary>
  105. /// <param name="pathName">The parent tests full name</param>
  106. /// <param name="name">The name of the test</param>
  107. protected Test( string pathName, string name )
  108. {
  109. this.testName = new TestName();
  110. this.testName.FullName = pathName == null || pathName == string.Empty
  111. ? name : pathName + "." + name;
  112. this.testName.Name = name;
  113. this.testName.TestID = new TestID();
  114. this.runState = RunState.Runnable;
  115. }
  116. /// <summary>
  117. /// Constructs a test given a TestName object
  118. /// </summary>
  119. /// <param name="testName">The TestName for this test</param>
  120. protected Test( TestName testName )
  121. {
  122. this.testName = testName;
  123. this.runState = RunState.Runnable;
  124. }
  125. /// <summary>
  126. /// Sets the runner id of a test and optionally its children
  127. /// </summary>
  128. /// <param name="runnerID">The runner id to be used</param>
  129. /// <param name="recursive">True if all children should get the same id</param>
  130. public void SetRunnerID( int runnerID, bool recursive )
  131. {
  132. this.testName.RunnerID = runnerID;
  133. if ( recursive && this.Tests != null )
  134. foreach( Test child in this.Tests )
  135. child.SetRunnerID( runnerID, true );
  136. }
  137. #endregion
  138. #region ITest Members
  139. #region Properties
  140. /// <summary>
  141. /// Gets the TestName of the test
  142. /// </summary>
  143. public TestName TestName
  144. {
  145. get { return testName; }
  146. }
  147. /// <summary>
  148. /// Gets a string representing the kind of test
  149. /// that this object represents, for use in display.
  150. /// </summary>
  151. public abstract string TestType
  152. {
  153. get;
  154. }
  155. /// <summary>
  156. /// Whether or not the test should be run
  157. /// </summary>
  158. public RunState RunState
  159. {
  160. get { return runState; }
  161. set { runState = value; }
  162. }
  163. /// <summary>
  164. /// Reason for not running the test, if applicable
  165. /// </summary>
  166. public string IgnoreReason
  167. {
  168. get { return (string)Properties[IGNOREREASON]; }
  169. set
  170. {
  171. if (value == null)
  172. Properties.Remove(IGNOREREASON);
  173. else
  174. Properties[IGNOREREASON] = value;
  175. }
  176. }
  177. /// <summary>
  178. /// Gets a count of test cases represented by
  179. /// or contained under this test.
  180. /// </summary>
  181. public virtual int TestCount
  182. {
  183. get { return 1; }
  184. }
  185. /// <summary>
  186. /// Gets a list of categories associated with this test.
  187. /// </summary>
  188. public IList Categories
  189. {
  190. get
  191. {
  192. if (Properties[CATEGORIES] == null)
  193. Properties[CATEGORIES] = new ArrayList();
  194. return (IList)Properties[CATEGORIES];
  195. }
  196. set
  197. {
  198. Properties[CATEGORIES] = value;
  199. }
  200. }
  201. /// <summary>
  202. /// Gets a description associated with this test.
  203. /// </summary>
  204. public String Description
  205. {
  206. get { return (string)Properties[DESCRIPTION]; }
  207. set
  208. {
  209. if (value == null)
  210. Properties.Remove(DESCRIPTION);
  211. else
  212. Properties[DESCRIPTION] = value;
  213. }
  214. }
  215. /// <summary>
  216. /// Gets the property dictionary for this test
  217. /// </summary>
  218. public IDictionary Properties
  219. {
  220. get
  221. {
  222. if ( properties == null )
  223. properties = new ListDictionary();
  224. return properties;
  225. }
  226. set
  227. {
  228. properties = value;
  229. }
  230. }
  231. /// <summary>
  232. /// Indicates whether this test is a suite
  233. /// </summary>
  234. public virtual bool IsSuite
  235. {
  236. get { return false; }
  237. }
  238. /// <summary>
  239. /// Gets the parent test of this test
  240. /// </summary>
  241. ITest ITest.Parent
  242. {
  243. get { return parent; }
  244. }
  245. /// <summary>
  246. /// Gets the parent as a Test object.
  247. /// Used by the core to set the parent.
  248. /// </summary>
  249. public Test Parent
  250. {
  251. get { return parent; }
  252. set { parent = value; }
  253. }
  254. /// <summary>
  255. /// Gets this test's child tests
  256. /// </summary>
  257. public virtual IList Tests
  258. {
  259. get { return null; }
  260. }
  261. /// <summary>
  262. /// Gets the Type of the fixture used in running this test
  263. /// </summary>
  264. public virtual Type FixtureType
  265. {
  266. get { return null; }
  267. }
  268. /// <summary>
  269. /// Gets or sets a fixture object for running this test
  270. /// </summary>
  271. public abstract object Fixture
  272. {
  273. get; set;
  274. }
  275. #endregion
  276. #region Methods
  277. /// <summary>
  278. /// Gets a count of test cases that would be run using
  279. /// the specified filter.
  280. /// </summary>
  281. /// <param name="filter"></param>
  282. /// <returns></returns>
  283. public virtual int CountTestCases(ITestFilter filter)
  284. {
  285. if (filter.Pass(this))
  286. return 1;
  287. return 0;
  288. }
  289. /// <summary>
  290. /// Runs the test under a particular filter, sending
  291. /// notifications to a listener.
  292. /// </summary>
  293. /// <param name="listener">An event listener to receive notifications</param>
  294. /// <param name="filter">A filter used in running the test</param>
  295. /// <returns></returns>
  296. public abstract TestResult Run(EventListener listener, ITestFilter filter);
  297. #endregion
  298. #endregion
  299. #region IComparable Members
  300. /// <summary>
  301. /// Compares this test to another test for sorting purposes
  302. /// </summary>
  303. /// <param name="obj">The other test</param>
  304. /// <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>
  305. public int CompareTo(object obj)
  306. {
  307. Test other = obj as Test;
  308. if ( other == null )
  309. return -1;
  310. return this.TestName.FullName.CompareTo( other.TestName.FullName );
  311. }
  312. #endregion
  313. }
  314. }