PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/interfaces/TestInfo.cs

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