PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Machine.Specifications.Specs/Runner/SpecificationRunnerSpecs.cs

https://github.com/machine/machine.specifications
C# | 541 lines | 434 code | 107 blank | 0 comment | 0 complexity | 568559faab23e14a3aea425c897799a1 MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. using System;
  2. using Example;
  3. using Example.Failing;
  4. using Example.Random;
  5. using FluentAssertions;
  6. using Machine.Specifications.Runner;
  7. using Machine.Specifications.Runner.Impl;
  8. namespace Machine.Specifications.Specs.Runner
  9. {
  10. [Subject("Specification Runner")]
  11. public class when_running_a_context_with_no_specifications
  12. : RunnerSpecs
  13. {
  14. Establish context = () =>
  15. {
  16. TestCleanupAfterEveryContext.AfterContextCleanupRun = false;
  17. context_with_no_specs.ContextEstablished = false;
  18. context_with_no_specs.CleanupOccurred = false;
  19. };
  20. Because of = () =>
  21. Run<context_with_no_specs>();
  22. It should_not_establish_the_context = () =>
  23. context_with_no_specs.ContextEstablished.Should().BeFalse();
  24. It should_not_cleanup = () =>
  25. context_with_no_specs.CleanupOccurred.Should().BeFalse();
  26. It should_not_perform_assembly_wide_cleanup = () =>
  27. TestCleanupAfterEveryContext.AfterContextCleanupRun.Should().BeFalse();
  28. }
  29. [Subject("Specification Runner")]
  30. public class when_running_a_context_with_a_ignored_specifications
  31. : RunnerSpecs
  32. {
  33. Establish context = () =>
  34. {
  35. context_with_no_specs.ContextEstablished = false;
  36. context_with_no_specs.CleanupOccurred = false;
  37. context_with_ignore_on_one_spec.IgnoredSpecRan = false;
  38. TestCleanupAfterEveryContext.AfterContextCleanupRun = false;
  39. };
  40. Because of = () =>
  41. Run<context_with_ignore_on_one_spec>();
  42. It should_not_run_the_spec = () =>
  43. context_with_ignore_on_one_spec.IgnoredSpecRan.Should().BeFalse();
  44. It should_not_establish_the_context = () =>
  45. context_with_ignore_on_one_spec.ContextEstablished.Should().BeFalse();
  46. It should_not_cleanup = () =>
  47. context_with_ignore_on_one_spec.CleanupOccurred.Should().BeFalse();
  48. It should_not_perform_assembly_wide_cleanup = () =>
  49. TestCleanupAfterEveryContext.AfterContextCleanupRun.Should().BeFalse();
  50. }
  51. [Subject("Specification Runner")]
  52. public class when_running_an_ignored_context
  53. : RunnerSpecs
  54. {
  55. Establish context = () =>
  56. {
  57. context_with_ignore.IgnoredSpecRan = false;
  58. TestCleanupAfterEveryContext.AfterContextCleanupRun = false;
  59. };
  60. Because of = () =>
  61. Run<context_with_ignore>();
  62. It should_not_run_the_spec = () =>
  63. context_with_ignore.IgnoredSpecRan.Should().BeFalse();
  64. It should_not_establish_the_context = () =>
  65. context_with_ignore.ContextEstablished.Should().BeFalse();
  66. It should_not_cleanup = () =>
  67. context_with_ignore.CleanupOccurred.Should().BeFalse();
  68. It should_not_perform_assembly_wide_cleanup = () =>
  69. TestCleanupAfterEveryContext.AfterContextCleanupRun.Should().BeFalse();
  70. }
  71. [Subject("Specification Runner")]
  72. public class when_running_a_context_with_multiple_specifications
  73. : RunnerSpecs
  74. {
  75. Establish context = () =>
  76. {
  77. context_with_multiple_specifications.EstablishRunCount = 0;
  78. context_with_multiple_specifications.BecauseClauseRunCount = 0;
  79. TestCleanupAfterEveryContext.Reset();
  80. };
  81. Because of = () =>
  82. Run<context_with_multiple_specifications>();
  83. It should_establish_the_context_once = () =>
  84. context_with_multiple_specifications.EstablishRunCount.Should().Be(1);
  85. It should_invoke_the_because_clause_once = () =>
  86. context_with_multiple_specifications.BecauseClauseRunCount.Should().Be(1);
  87. It should_invoke_the_assembly_wide_cleanup_once = () =>
  88. TestCleanupAfterEveryContext.AfterContextCleanupRunCount.Should().Be(1);
  89. }
  90. [Subject("Specification Runner")]
  91. public class when_running_a_context_with_multiple_specifications_and_setup_once_per_attribute
  92. : RunnerSpecs
  93. {
  94. Establish context = () =>
  95. {
  96. context_with_multiple_specifications_and_setup_for_each.EstablishRunCount = 0;
  97. context_with_multiple_specifications_and_setup_for_each.BecauseClauseRunCount = 0;
  98. TestCleanupAfterEveryContext.Reset();
  99. };
  100. Because of = () =>
  101. Run<context_with_multiple_specifications_and_setup_for_each>();
  102. It should_establish_the_context_for_each_specification = () =>
  103. context_with_multiple_specifications_and_setup_for_each.EstablishRunCount.Should().Be(2);
  104. It should_invoke_the_because_clause_for_each_specification = () =>
  105. context_with_multiple_specifications_and_setup_for_each.BecauseClauseRunCount.Should().Be(2);
  106. It should_invoke_the_assembly_wide_cleanup_once_per_spec = () =>
  107. TestCleanupAfterEveryContext.AfterContextCleanupRunCount.Should().Be(2);
  108. }
  109. [Subject("Specification Runner")]
  110. public class when_running_a_context_with_multiple_establish_clauses
  111. : RunnerSpecs
  112. {
  113. static Exception exception;
  114. Because of =
  115. () =>
  116. {
  117. exception = Catch.Exception(Run<context_with_multiple_establish_clauses>);
  118. };
  119. It should_fail =
  120. () => exception.Should().BeOfType<SpecificationUsageException>();
  121. It should_report_the_reason =
  122. () => exception.Message.Should().StartWith("You cannot have more than one Establish clause in Example.Failing.context_with_multiple_establish_clauses");
  123. }
  124. [Subject("Specification Runner")]
  125. public class when_running_a_context_with_multiple_setup_clauses_and_custom_delegates
  126. : RunnerSpecs
  127. {
  128. static Exception exception;
  129. Because of =
  130. () => exception = Catch.Exception(Run<context_with_multiple_given_clauses>);
  131. It should_fail =
  132. () => exception.Should().BeOfType<SpecificationUsageException>();
  133. It should_report_the_reason =
  134. () => exception.Message.Should().StartWith("You cannot have more than one Given clause in Example.Failing.context_with_multiple_given_clauses");
  135. }
  136. [Subject("Specification Runner")]
  137. public class when_running_a_context_with_failing_establish_clauses
  138. : RunnerSpecs
  139. {
  140. Because of = Run<context_with_failing_establish>;
  141. It should_fail = () =>
  142. testListener.LastResult.Passed.Should().BeFalse();
  143. }
  144. [Subject("Specification Runner")]
  145. public class when_running_a_context_with_failing_because_clauses
  146. : RunnerSpecs
  147. {
  148. Because of = Run<context_with_failing_because>;
  149. It should_fail = () =>
  150. testListener.LastResult.Passed.Should().BeFalse();
  151. }
  152. [Subject("Specification Runner")]
  153. public class when_running_a_context_with_failing_specs
  154. : RunnerSpecs
  155. {
  156. Because of = Run<context_with_failing_specs>;
  157. It should_fail = () =>
  158. testListener.LastResult.Passed.Should().BeFalse();
  159. }
  160. [Subject("Specification Runner")]
  161. public class when_running_an_assembly_with_no_included_contexts
  162. {
  163. static DefaultRunner runner;
  164. Establish context = () =>
  165. {
  166. TestAssemblyContext.OnAssemblyStartRun = false;
  167. TestAssemblyContext.OnAssemblyCompleteRun = false;
  168. runner = new DefaultRunner(new TestListener(), new RunOptions(new[] { "asdfasdf" }, new string[0], new string[0]));
  169. };
  170. Because of = () =>
  171. runner.RunAssembly(typeof(TestAssemblyContext).Assembly);
  172. It should_not_run_assembly_start = () =>
  173. TestAssemblyContext.OnAssemblyStartRun.Should().BeFalse();
  174. It should_not_run_assembly_complete = () =>
  175. TestAssemblyContext.OnAssemblyCompleteRun.Should().BeFalse();
  176. }
  177. [Subject("Specification Runner")]
  178. public class when_running_a_specification_with_console_output
  179. : RunnerSpecs
  180. {
  181. Because of = () =>
  182. Run<context_with_console_output>();
  183. It should_capture_the_standard_output =
  184. () => testListener.LastAssembly.CapturedOutput.Should().Be(String.Format("Console.Out message in establish{0}" +
  185. "Console.Out message in because{0}" +
  186. "Console.Out message in spec{0}" +
  187. "Console.Out message in nth spec{0}" +
  188. "Console.Out message in cleanup{0}",
  189. Environment.NewLine));
  190. }
  191. [Subject("Specification Runner")]
  192. public class when_running_a_specification_with_error_output
  193. : RunnerSpecs
  194. {
  195. Because of = () =>
  196. Run<context_with_console_error_output>();
  197. It should_capture_the_standard_error =
  198. () => testListener.LastAssembly.CapturedOutput.Should().Be(String.Format("Console.Error message in establish{0}" +
  199. "Console.Error message in because{0}" +
  200. "Console.Error message in spec{0}" +
  201. "Console.Error message in nth spec{0}" +
  202. "Console.Error message in cleanup{0}",
  203. Environment.NewLine));
  204. }
  205. [Subject("Specification Runner")]
  206. public class when_running_a_specification_with_debug_output
  207. : RunnerSpecs
  208. {
  209. Because of = () =>
  210. Run<context_with_debug_output>();
  211. It should_capture_the_debug_trace =
  212. () => testListener.LastAssembly.CapturedOutput.Should().Be(String.Format("Debug.WriteLine message in establish{0}" +
  213. "Debug.WriteLine message in because{0}" +
  214. "Debug.WriteLine message in spec{0}" +
  215. "Debug.WriteLine message in nth spec{0}" +
  216. "Debug.WriteLine message in cleanup{0}",
  217. Environment.NewLine));
  218. }
  219. [Subject("Specification Runner")]
  220. public class when_running_a_specification_with_console_output_and_foreach
  221. : RunnerSpecs
  222. {
  223. Because of = () =>
  224. Run<context_with_console_output_and_setup_for_each>();
  225. It should_capture_the_standard_output =
  226. () => testListener.LastAssembly.CapturedOutput.Should().Be(String.Format("Console.Out message in establish{0}" +
  227. "Console.Out message in because{0}" +
  228. "Console.Out message in spec{0}" +
  229. "Console.Out message in cleanup{0}" +
  230. "Console.Out message in establish{0}" +
  231. "Console.Out message in because{0}" +
  232. "Console.Out message in nth spec{0}" +
  233. "Console.Out message in cleanup{0}",
  234. Environment.NewLine));
  235. }
  236. [Subject("Specification Runner")]
  237. public class when_running_a_specification_that_throws_an_exception_with_an_inner_exception
  238. : RunnerSpecs
  239. {
  240. Because of = () =>
  241. Run<context_with_inner_exception>();
  242. It should_include_the_inner_exception_in_the_result = () =>
  243. testListener.LastResult.Exception.ToString().Should().Contain("INNER123");
  244. }
  245. [Subject("Specification Runner")]
  246. public class when_running_a_behavior
  247. : RunnerSpecs
  248. {
  249. Establish context = () =>
  250. {
  251. Behaviors.BehaviorSpecRan = false;
  252. };
  253. Because of = () =>
  254. Run<Behaviors>();
  255. It should_not_run_the_behavior_specs = () =>
  256. Behaviors.BehaviorSpecRan.Should().BeFalse();
  257. }
  258. [Subject("Specification Runner")]
  259. public class when_running_with_tag
  260. {
  261. Establish context = () =>
  262. {
  263. TaggedCleanup.Reset();
  264. UntaggedCleanup.Reset();
  265. TaggedAssemblyContext.Reset();
  266. UntaggedAssemblyContext.Reset();
  267. TaggedResultSupplementer.Reset();
  268. UntaggedResultSupplementer.Reset();
  269. testListener = new TestListener();
  270. var options = new RunOptions(new string[] { "foobar" }, new string[] { }, new string[0]);
  271. runner = new DefaultRunner(testListener, options);
  272. };
  273. Because of = () =>
  274. runner.RunMember(typeof(context_with_multiple_specifications).Assembly,
  275. typeof(context_with_multiple_specifications));
  276. It should_run_untagged_assembly_context = () =>
  277. UntaggedAssemblyContext.OnAssemblyStartRun.Should().BeTrue();
  278. It should_run_tagged_assembly_context = () =>
  279. TaggedAssemblyContext.OnAssemblyStartRun.Should().BeTrue();
  280. It should_run_untagged_assembly_context_complete = () =>
  281. UntaggedAssemblyContext.OnAssemblyCompleteRun.Should().BeTrue();
  282. It should_run_tagged_assembly_context_complete = () =>
  283. TaggedAssemblyContext.OnAssemblyCompleteRun.Should().BeTrue();
  284. It should_run_untagged_global_cleanup = () =>
  285. UntaggedCleanup.AfterContextCleanupRunCount.Should().BeGreaterThan(0);
  286. It should_run_tagged_global_cleanup = () =>
  287. TaggedCleanup.AfterContextCleanupRunCount.Should().BeGreaterThan(0);
  288. It should_run_tagged_result_supplementer = () =>
  289. TaggedResultSupplementer.SupplementResultRun.Should().BeTrue();
  290. It should_run_untagged_result_supplementer = () =>
  291. UntaggedResultSupplementer.SupplementResultRun.Should().BeTrue();
  292. static DefaultRunner runner;
  293. static TestListener testListener;
  294. }
  295. [Subject("Specification Runner")]
  296. public class when_running_with_empty_filters
  297. {
  298. Establish context = () =>
  299. {
  300. testListener = new TestListener();
  301. var options = new RunOptions(new string[] { }, new string[] { }, new string[0]);
  302. runner = new DefaultRunner(testListener, options);
  303. };
  304. Because of = () =>
  305. {
  306. runner.RunMember(typeof(context_with_multiple_specifications).Assembly,
  307. typeof(context_with_multiple_specifications));
  308. runner.RunMember(typeof(context_with_duplicate_tags).Assembly,
  309. typeof(context_with_duplicate_tags));
  310. };
  311. It should_run_everything = () =>
  312. testListener.SpecCount.Should().Be(3);
  313. static DefaultRunner runner;
  314. static TestListener testListener;
  315. }
  316. [Subject("Specification Runner")]
  317. public class when_running_with_context_filters
  318. {
  319. Establish context = () =>
  320. {
  321. testListener = new TestListener();
  322. var options = new RunOptions(new string[] { }, new string[] { }, new[] { "Example.Random.context_with_multiple_specifications" });
  323. runner = new DefaultRunner(testListener, options);
  324. };
  325. Because of = () =>
  326. {
  327. runner.RunMember(typeof(context_with_multiple_specifications).Assembly,
  328. typeof(context_with_multiple_specifications));
  329. runner.RunMember(typeof(context_with_duplicate_tags).Assembly,
  330. typeof(context_with_duplicate_tags));
  331. };
  332. It should_run_included_contexts_only = () =>
  333. testListener.SpecCount.Should().Be(2);
  334. static DefaultRunner runner;
  335. static TestListener testListener;
  336. }
  337. [Subject("Specification Runner")]
  338. public class when_running_with_specification_filters
  339. {
  340. Establish context = () =>
  341. {
  342. testListener = new TestListener();
  343. var options = new RunOptions(new string[] { },
  344. new string[] { },
  345. new[]
  346. {
  347. "Example.Random.context_with_multiple_specifications",
  348. "Example.Random.context_with_duplicate_tags"
  349. });
  350. runner = new DefaultRunner(testListener, options);
  351. };
  352. Because of = () =>
  353. {
  354. runner.RunMember(typeof(context_with_multiple_specifications).Assembly,
  355. typeof(context_with_multiple_specifications));
  356. runner.RunMember(typeof(context_with_duplicate_tags).Assembly,
  357. typeof(context_with_duplicate_tags));
  358. };
  359. It should_run_included_specifications_only = () =>
  360. testListener.SpecCount.Should().Be(3);
  361. static DefaultRunner runner;
  362. static TestListener testListener;
  363. }
  364. [Subject("Specification Runner")]
  365. public class when_running_a_context_with_public_Its
  366. : RunnerSpecs
  367. {
  368. Because of = Run<context_with_public_It_field>;
  369. It should_succeed =
  370. () => testListener.SpecCount.Should().Be(1);
  371. }
  372. [Subject("Specification Runner")]
  373. public class when_running_a_context_with_protected_Its
  374. : RunnerSpecs
  375. {
  376. Because of = Run<context_with_protected_It_field>;
  377. It should_succeed =
  378. () => testListener.SpecCount.Should().Be(1);
  379. }
  380. [Subject("Specification Runner")]
  381. public class when_running_a_context_with_internal_Its
  382. : RunnerSpecs
  383. {
  384. Because of = Run<context_with_internal_It_field>;
  385. It should_succeed =
  386. () => testListener.SpecCount.Should().Be(1);
  387. }
  388. [Subject("Specification Runner")]
  389. public class when_running_a_context_with_public_Behaves_like
  390. : RunnerSpecs
  391. {
  392. Because of = Run<context_with_public_Behaves_like_field>;
  393. It should_succeed =
  394. () => testListener.SpecCount.Should().Be(1);
  395. }
  396. [Subject("Specification Runner")]
  397. public class when_running_a_context_with_nonprivate_framework_fields
  398. : RunnerSpecs
  399. {
  400. Because of = Run<context_with_nonprivate_framework_fields>;
  401. It should_succeed =
  402. () => testListener.LastResult.Passed.Should().BeTrue();
  403. }
  404. [Subject("Specification Runner")]
  405. public class when_running_a_context_inside_a_static_class
  406. : RunnerSpecs
  407. {
  408. Because of = Run<StaticContainer.when_a_context_is_nested_inside_a_static_class>;
  409. It should_succeed =
  410. () => testListener.LastResult.Passed.Should().BeTrue();
  411. }
  412. [Subject("Specification Runner")]
  413. public class when_running_a_context_inside_a_static_class_that_is_nested_in_a_nonstatic_class
  414. : RunnerSpecs
  415. {
  416. Because of = Run<NonStaticContainer.StaticContainer.when_a_context_is_nested_inside_a_static_class_that_is_nested_inside_a_class>;
  417. It should_succeed =
  418. () => testListener.LastResult.Passed.Should().BeTrue();
  419. }
  420. public class RunnerSpecs
  421. {
  422. static DefaultRunner runner;
  423. protected static TestListener testListener;
  424. Establish context = () =>
  425. {
  426. testListener = new TestListener();
  427. runner = new DefaultRunner(testListener, RunOptions.Default);
  428. };
  429. public static void Run<T>()
  430. {
  431. runner.RunMember(typeof(T).Assembly, typeof(T));
  432. }
  433. }
  434. }