PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/src/NUnit/interfaces/TestResult.cs

#
C# | 503 lines | 248 code | 64 blank | 191 comment | 32 complexity | 911b29b5604dc6a6f6a054c2a236ab6d 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.Text;
  10. using System.Collections;
  11. /// <summary>
  12. /// The TestResult class represents
  13. /// the result of a test and is used to
  14. /// communicate results across AppDomains.
  15. /// </summary>
  16. ///
  17. [Serializable]
  18. public class TestResult
  19. {
  20. #region Fields
  21. /// <summary>
  22. /// Indicates the result of the test
  23. /// </summary>
  24. private ResultState resultState;
  25. /// <summary>
  26. /// Indicates the location of a failure
  27. /// </summary>
  28. private FailureSite failureSite;
  29. /// <summary>
  30. /// The elapsed time for executing this test
  31. /// </summary>
  32. private double time = 0.0;
  33. /// <summary>
  34. /// The test that this result pertains to
  35. /// </summary>
  36. private readonly TestInfo test;
  37. /// <summary>
  38. /// The stacktrace at the point of failure
  39. /// </summary>
  40. private string stackTrace;
  41. /// <summary>
  42. /// Message giving the reason for failure
  43. /// </summary>
  44. private string message;
  45. /// <summary>
  46. /// List of child results
  47. /// </summary>
  48. private IList results;
  49. /// <summary>
  50. /// Number of asserts executed by this test
  51. /// </summary>
  52. private int assertCount = 0;
  53. #endregion
  54. #region Constructor
  55. /// <summary>
  56. /// Construct a test result given a TestInfo
  57. /// </summary>
  58. /// <param name="test">The test to be used</param>
  59. public TestResult(TestInfo test)
  60. {
  61. this.test = test;
  62. this.message = test.IgnoreReason;
  63. }
  64. /// <summary>
  65. /// Construct a TestResult given an ITest
  66. /// </summary>
  67. /// <param name="test"></param>
  68. public TestResult(ITest test) : this( new TestInfo(test) ) { }
  69. /// <summary>
  70. /// Construct a TestResult given a TestName
  71. /// </summary>
  72. /// <param name="testName">A TestName</param>
  73. public TestResult(TestName testName) : this( new TestInfo( testName ) ) { }
  74. #endregion
  75. #region Properties
  76. /// <summary>
  77. /// Gets the ResultState of the test result, which
  78. /// indicates the success or failure of the test.
  79. /// </summary>
  80. public ResultState ResultState
  81. {
  82. get { return resultState; }
  83. }
  84. /// <summary>
  85. /// Gets the stage of the test in which a failure
  86. /// or error occured.
  87. /// </summary>
  88. public FailureSite FailureSite
  89. {
  90. get { return failureSite; }
  91. }
  92. /// <summary>
  93. /// Indicates whether the test executed
  94. /// </summary>
  95. public bool Executed
  96. {
  97. get
  98. {
  99. return resultState == ResultState.Success ||
  100. resultState == ResultState.Failure ||
  101. resultState == ResultState.Error ||
  102. resultState == ResultState.Inconclusive;
  103. }
  104. }
  105. /// <summary>
  106. /// Gets the name of the test result
  107. /// </summary>
  108. public virtual string Name
  109. {
  110. get { return test.TestName.Name; }
  111. }
  112. /// <summary>
  113. /// Gets the full name of the test result
  114. /// </summary>
  115. public virtual string FullName
  116. {
  117. get { return test.TestName.FullName; }
  118. }
  119. /// <summary>
  120. /// Gets the test associated with this result
  121. /// </summary>
  122. public ITest Test
  123. {
  124. get { return test; }
  125. }
  126. /// <summary>
  127. /// Indicates whether the test ran successfully
  128. /// </summary>
  129. public virtual bool IsSuccess
  130. {
  131. get { return resultState == ResultState.Success; }
  132. }
  133. /// <summary>
  134. /// Indicates whether the test failed
  135. /// </summary>
  136. public virtual bool IsFailure
  137. {
  138. get { return resultState == ResultState.Failure; }
  139. }
  140. /// <summary>
  141. /// Indicates whether the test had an error (as opposed to a failure)
  142. /// </summary>
  143. public virtual bool IsError
  144. {
  145. get { return resultState == ResultState.Error; }
  146. }
  147. /// <summary>
  148. /// Gets a description associated with the test
  149. /// </summary>
  150. public string Description
  151. {
  152. get { return test.Description; }
  153. }
  154. /// <summary>
  155. /// Gets the elapsed time for running the test
  156. /// </summary>
  157. public double Time
  158. {
  159. get { return time; }
  160. set { time = value; }
  161. }
  162. /// <summary>
  163. /// Gets the message associated with a test
  164. /// failure or with not running the test
  165. /// </summary>
  166. public string Message
  167. {
  168. get { return message; }
  169. }
  170. /// <summary>
  171. /// Gets any stacktrace associated with an
  172. /// error or failure.
  173. /// </summary>
  174. public virtual string StackTrace
  175. {
  176. get { return stackTrace; }
  177. set { stackTrace = value; }
  178. }
  179. /// <summary>
  180. /// Gets or sets the count of asserts executed
  181. /// when running the test.
  182. /// </summary>
  183. public int AssertCount
  184. {
  185. get { return assertCount; }
  186. set { assertCount = value; }
  187. }
  188. /// <summary>
  189. /// Return true if this result has any child results
  190. /// </summary>
  191. public bool HasResults
  192. {
  193. get { return results != null && results.Count > 0; }
  194. }
  195. /// <summary>
  196. /// Gets a list of the child results of this TestResult
  197. /// </summary>
  198. public IList Results
  199. {
  200. get { return results; }
  201. }
  202. #endregion
  203. #region Public Methods
  204. /// <summary>
  205. /// Mark the test as succeeding
  206. /// </summary>
  207. public void Success()
  208. {
  209. SetResult( ResultState.Success, null, null );
  210. }
  211. /// <summary>
  212. /// Mark the test as succeeding and set a message
  213. /// </summary>
  214. public void Success( string message )
  215. {
  216. SetResult( ResultState.Success, message, null );
  217. }
  218. /// <summary>
  219. /// Mark the test as ignored.
  220. /// </summary>
  221. /// <param name="reason">The reason the test was not run</param>
  222. public void Ignore(string reason)
  223. {
  224. Ignore( reason, null );
  225. }
  226. /// <summary>
  227. /// Mark the test as ignored.
  228. /// </summary>
  229. /// <param name="ex">The ignore exception that was thrown</param>
  230. public void Ignore( Exception ex )
  231. {
  232. Ignore( ex.Message, BuildStackTrace( ex ) );
  233. }
  234. /// <summary>
  235. /// Mark the test as ignored.
  236. /// </summary>
  237. /// <param name="reason">The reason the test was not run</param>
  238. /// <param name="stackTrace">Stack trace giving the location of the command</param>
  239. public void Ignore(string reason, string stackTrace)
  240. {
  241. SetResult( ResultState.Ignored, reason, stackTrace );
  242. }
  243. /// <summary>
  244. /// Mark the test as skipped.
  245. /// </summary>
  246. /// <param name="reason">The reason the test was not run</param>
  247. public void Skip(string reason)
  248. {
  249. SetResult(ResultState.Skipped, reason, null);
  250. }
  251. /// <summary>
  252. /// Mark the test a not runnable with a reason
  253. /// </summary>
  254. /// <param name="reason">The reason the test is invalid</param>
  255. public void Invalid( string reason )
  256. {
  257. SetResult( ResultState.NotRunnable, reason, null );
  258. }
  259. /// <summary>
  260. /// Mark the test as not runnable due to a builder exception
  261. /// </summary>
  262. /// <param name="ex">The exception thrown by the builder or an addin</param>
  263. public void Invalid(Exception ex)
  264. {
  265. SetResult(ResultState.NotRunnable, BuildMessage( ex ), BuildStackTrace(ex));
  266. }
  267. /// <summary>
  268. /// Set the result of the test
  269. /// </summary>
  270. /// <param name="resultState">The ResultState to use in the result</param>
  271. /// <param name="reason">The reason the test was not run</param>
  272. /// <param name="stackTrace">Stack trace giving the location of the command</param>
  273. /// <param name="failureSite">The location of the failure, if any</param>
  274. public void SetResult(ResultState resultState, string reason, string stackTrace, FailureSite failureSite)
  275. {
  276. if (failureSite == FailureSite.SetUp)
  277. reason = "SetUp : " + reason;
  278. else if (failureSite == FailureSite.TearDown)
  279. {
  280. reason = "TearDown : " + reason;
  281. stackTrace = "--TearDown" + Environment.NewLine + stackTrace;
  282. if (this.message != null)
  283. reason = this.message + Environment.NewLine + reason;
  284. if (this.stackTrace != null)
  285. stackTrace = this.stackTrace + Environment.NewLine + stackTrace;
  286. }
  287. this.resultState = resultState;
  288. this.message = reason;
  289. this.stackTrace = stackTrace;
  290. this.failureSite = failureSite;
  291. }
  292. /// <summary>
  293. /// Set the result of the test
  294. /// </summary>
  295. /// <param name="resultState">The ResultState to use in the result</param>
  296. /// <param name="reason">The reason the test was not run</param>
  297. /// <param name="stackTrace">Stack trace giving the location of the command</param>
  298. public void SetResult(ResultState resultState, string reason, string stackTrace)
  299. {
  300. SetResult(resultState, reason, stackTrace, FailureSite.Test);
  301. }
  302. /// <summary>
  303. /// Set the result of the test.
  304. /// </summary>
  305. /// <param name="resultState">The ResultState to use in the result</param>
  306. /// <param name="ex">The exception that caused this result</param>
  307. /// <param name="failureSite">The site at which an error or failure occured</param>
  308. public void SetResult(ResultState resultState, Exception ex, FailureSite failureSite)
  309. {
  310. if (resultState == ResultState.Cancelled)
  311. SetResult(resultState, "Test cancelled by user", BuildStackTrace(ex));
  312. else if (resultState == ResultState.Error)
  313. SetResult( resultState, BuildMessage(ex), BuildStackTrace(ex), failureSite);
  314. else
  315. SetResult(resultState, ex.Message, ex.StackTrace, failureSite);
  316. }
  317. /// <summary>
  318. /// Mark the test as a failure due to an
  319. /// assertion having failed.
  320. /// </summary>
  321. /// <param name="message">Message to display</param>
  322. /// <param name="stackTrace">Stack trace giving the location of the failure</param>
  323. public void Failure(string message, string stackTrace)
  324. {
  325. Failure(message, stackTrace, FailureSite.Test);
  326. }
  327. /// <summary>
  328. /// Mark the test as a failure due to an
  329. /// assertion having failed.
  330. /// </summary>
  331. /// <param name="message">Message to display</param>
  332. /// <param name="stackTrace">Stack trace giving the location of the failure</param>
  333. /// <param name="failureSite">The site of the failure</param>
  334. public void Failure(string message, string stackTrace, FailureSite failureSite )
  335. {
  336. SetResult( Core.ResultState.Failure, message, stackTrace );
  337. this.failureSite = failureSite;
  338. }
  339. /// <summary>
  340. /// Marks the result as an error due to an exception thrown
  341. /// by the test.
  342. /// </summary>
  343. /// <param name="exception">The exception that was caught</param>
  344. public void Error(Exception exception)
  345. {
  346. Error(exception, FailureSite.Test);
  347. }
  348. /// <summary>
  349. /// Marks the result as an error due to an exception thrown
  350. /// from the indicated FailureSite.
  351. /// </summary>
  352. /// <param name="exception">The exception that was caught</param>
  353. /// <param name="failureSite">The site from which it was thrown</param>
  354. public void Error(Exception exception, FailureSite failureSite)
  355. {
  356. SetResult(ResultState.Error, exception, failureSite);
  357. //string message = BuildMessage(exception);
  358. //string stackTrace = BuildStackTrace(exception);
  359. //if (failureSite == FailureSite.TearDown)
  360. //{
  361. // message = "TearDown : " + message;
  362. // stackTrace = "--TearDown" + Environment.NewLine + stackTrace;
  363. // if (this.message != null)
  364. // message = this.message + Environment.NewLine + message;
  365. // if (this.stackTrace != null)
  366. // stackTrace = this.stackTrace + Environment.NewLine + stackTrace;
  367. //}
  368. //SetResult( ResultState.Error, message, stackTrace );
  369. //this.failureSite = failureSite;
  370. }
  371. /// <summary>
  372. /// Add a child result
  373. /// </summary>
  374. /// <param name="result">The child result to be added</param>
  375. public void AddResult(TestResult result)
  376. {
  377. if ( results == null )
  378. results = new ArrayList();
  379. this.results.Add(result);
  380. switch (result.ResultState)
  381. {
  382. case ResultState.Failure:
  383. case ResultState.Error:
  384. if (!this.IsFailure && !this.IsError)
  385. this.Failure("Child test failed", null, FailureSite.Child);
  386. break;
  387. case ResultState.Success:
  388. if (this.ResultState == ResultState.Inconclusive)
  389. this.Success();
  390. break;
  391. case ResultState.Cancelled:
  392. this.SetResult(ResultState.Cancelled, result.Message, null, FailureSite.Child);
  393. break;
  394. }
  395. }
  396. #endregion
  397. #region Exception Helpers
  398. private static string BuildMessage(Exception exception)
  399. {
  400. StringBuilder sb = new StringBuilder();
  401. sb.AppendFormat( "{0} : {1}", exception.GetType().ToString(), exception.Message );
  402. Exception inner = exception.InnerException;
  403. while( inner != null )
  404. {
  405. sb.Append( Environment.NewLine );
  406. sb.AppendFormat( " ----> {0} : {1}", inner.GetType().ToString(), inner.Message );
  407. inner = inner.InnerException;
  408. }
  409. return sb.ToString();
  410. }
  411. private static string BuildStackTrace(Exception exception)
  412. {
  413. StringBuilder sb = new StringBuilder( GetStackTrace( exception ) );
  414. Exception inner = exception.InnerException;
  415. while( inner != null )
  416. {
  417. sb.Append( Environment.NewLine );
  418. sb.Append( "--" );
  419. sb.Append( inner.GetType().Name );
  420. sb.Append( Environment.NewLine );
  421. sb.Append( GetStackTrace( inner ) );
  422. inner = inner.InnerException;
  423. }
  424. return sb.ToString();
  425. }
  426. private static string GetStackTrace(Exception exception)
  427. {
  428. try
  429. {
  430. return exception.StackTrace;
  431. }
  432. catch( Exception )
  433. {
  434. return "No stack trace available";
  435. }
  436. }
  437. #endregion
  438. }
  439. }