Test-Rail-Parser.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use FindBin;
  5. use lib "$FindBin::Bin/lib";
  6. use Scalar::Util qw{reftype};
  7. use TestRail::API;
  8. use Test::LWP::UserAgent::TestRailMock;
  9. use Test::Rail::Parser;
  10. use Test::More 'tests' => 83;
  11. use Test::Fatal qw{exception};
  12. #Same song and dance as in TestRail-API.t
  13. my $apiurl = $ENV{'TESTRAIL_API_URL'};
  14. my $login = $ENV{'TESTRAIL_USER'};
  15. my $pw = $ENV{'TESTRAIL_PASSWORD'};
  16. my $is_mock = (!$apiurl && !$login && !$pw);
  17. ($apiurl,$login,$pw) = ('http://testrail.local','teodesian@cpan.org','fake') if $is_mock;
  18. my ($debug,$browser);
  19. $debug = 1;
  20. if ($is_mock) {
  21. $browser = $Test::LWP::UserAgent::TestRailMock::mockObject;
  22. }
  23. #test exceptions...
  24. #TODO
  25. #case_per_ok mode
  26. my $fcontents = "
  27. fake.test ..
  28. 1..2
  29. ok 1 - STORAGE TANKS SEARED
  30. #goo
  31. not ok 2 - NOT SO SEARED AFTER ARR
  32. ";
  33. my $tap;
  34. my $opts = {
  35. 'tap' => $fcontents,
  36. 'apiurl' => $apiurl,
  37. 'user' => $login,
  38. 'pass' => $pw,
  39. 'debug' => $debug,
  40. 'browser' => $browser,
  41. 'run' => 'TestingSuite',
  42. 'project' => 'TestProject',
  43. 'merge' => 1,
  44. 'case_per_ok' => 1
  45. };
  46. my $res = exception { $tap = Test::Rail::Parser->new($opts) };
  47. is($res,undef,"TR Parser doesn't explode on instantiation");
  48. isa_ok($tap,"Test::Rail::Parser");
  49. if (!$res) {
  50. $tap->run();
  51. is($tap->{'errors'},0,"No errors encountered uploading case results");
  52. }
  53. undef $tap;
  54. delete $opts->{'tap'};
  55. $opts->{'source'} = 't/fake.test';
  56. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  57. is($res,undef,"TR Parser doesn't explode on instantiation");
  58. isa_ok($tap,"Test::Rail::Parser");
  59. if (!$res) {
  60. $tap->run();
  61. is($tap->{'errors'},0,"No errors encountered uploading case results");
  62. }
  63. $fcontents = "ok 1 - STORAGE TANKS SEARED
  64. # whee
  65. not ok 2 - NOT SO SEARED AFTER ARR
  66. # Failed test 'NOT SO SEARED AFTER ARR'
  67. # at t/fake.test line 10.
  68. # Looks like you failed 1 test of 2.
  69. ";
  70. is($tap->{'raw_output'},$fcontents,"Full raw content uploaded in non step results mode");
  71. #Check that time run is being uploaded
  72. my $timeResults = $tap->{'tr_opts'}->{'testrail'}->getTestResults(1);
  73. if ( ( reftype($timeResults) || 'undef') eq 'ARRAY') {
  74. is( $timeResults->[0]->{'elapsed'}, '2s', "Plugin correctly sets elapsed time");
  75. } else {
  76. fail("Could not get test results to check elapsed time!");
  77. }
  78. #Check the time formatting routine.
  79. is(Test::Rail::Parser::_compute_elapsed(0,0),undef,"Elapsed computation correct at second boundary");
  80. is(Test::Rail::Parser::_compute_elapsed(0,61),'1m 1s',"Elapsed computation correct at minute boundary");
  81. is(Test::Rail::Parser::_compute_elapsed(0,3661),'1h 1m 1s',"Elapsed computation correct at hour boundary");
  82. is(Test::Rail::Parser::_compute_elapsed(0,86461),'24h 1m 1s',"Elapsed computation correct at day boundary");
  83. #Time for non case_per_ok mode
  84. undef $tap;
  85. $opts->{'source'} = 't/faker.test';
  86. $opts->{'run'} = 'OtherOtherSuite';
  87. delete $opts->{'case_per_ok'};
  88. $opts->{'step_results'} = 'step_results';
  89. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  90. is($res,undef,"TR Parser doesn't explode on instantiation");
  91. isa_ok($tap,"Test::Rail::Parser");
  92. if (!$res) {
  93. $tap->run();
  94. is($tap->{'errors'},0,"No errors encountered uploading case results");
  95. is($tap->{'global_status'},5, "Test global result is FAIL when one subtest fails even if there are TODO passes");
  96. }
  97. #Default mode
  98. undef $tap;
  99. delete $opts->{'step_results'};
  100. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  101. is($res,undef,"TR Parser doesn't explode on instantiation");
  102. isa_ok($tap,"Test::Rail::Parser");
  103. if (!$res) {
  104. $tap->run();
  105. is($tap->{'errors'},0,"No errors encountered uploading case results");
  106. }
  107. #Default mode
  108. undef $tap;
  109. $fcontents = "
  110. fake.test ..
  111. 1..2
  112. ok 1 - STORAGE TANKS SEARED
  113. #Subtest NOT SO SEARED AFTER ARR
  114. ok 1 - STROGGIFY POPULATION CENTERS
  115. not ok 2 - STROGGIFY POPULATION CENTERS
  116. #goo
  117. not ok 2 - NOT SO SEARED AFTER ARR
  118. ";
  119. $opts->{'tap'} = $fcontents;
  120. delete $opts->{'source'};
  121. delete $opts->{'step_results'};
  122. $opts->{'run'} = 'TestingSuite';
  123. $opts->{'case_per_ok'} = 1;
  124. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  125. is($res,undef,"TR Parser doesn't explode on instantiation");
  126. isa_ok($tap,"Test::Rail::Parser");
  127. if (!$res) {
  128. $tap->run();
  129. is($tap->{'errors'},0,"No errors encountered uploading case results");
  130. }
  131. #skip/todo in case_per_ok
  132. undef $tap;
  133. delete $opts->{'tap'};
  134. $opts->{'source'} = 't/skip.test';
  135. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  136. is($res,undef,"TR Parser doesn't explode on instantiation");
  137. isa_ok($tap,"Test::Rail::Parser");
  138. if (!$res) {
  139. $tap->run();
  140. is($tap->{'errors'},0,"No errors encountered uploading case results");
  141. }
  142. #Default mode skip (skip_all)
  143. undef $tap;
  144. $opts->{'source'} = 't/skipall.test';
  145. delete $opts->{'case_per_ok'};
  146. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  147. is($res,undef,"TR Parser doesn't explode on instantiation");
  148. isa_ok($tap,"Test::Rail::Parser");
  149. if (!$res) {
  150. $tap->run();
  151. is($tap->{'errors'},0,"No errors encountered uploading case results");
  152. is($tap->{'global_status'},6, "Test global result is SKIP on skip all");
  153. }
  154. #Ok, let's test the plan, config, and spawn bits.
  155. undef $tap;
  156. $opts->{'run'} = 'hoo hoo I do not exist';
  157. $opts->{'plan'} = 'mah dubz plan';
  158. $opts->{'configs'} = ['testPlatform1'];
  159. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  160. isnt($res,undef,"TR Parser explodes on instantiation when asking for run not in plan");
  161. undef $tap;
  162. $opts->{'run'} = 'TestingSuite';
  163. $opts->{'configs'} = ['testConfig'];
  164. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  165. is($res,undef,"TR Parser doesn't explode on instantiation looking for existing run in plan");
  166. isa_ok($tap,"Test::Rail::Parser");
  167. if (!$res) {
  168. $tap->run();
  169. is($tap->{'errors'},0,"No errors encountered uploading case results");
  170. }
  171. #Now, test spawning.
  172. undef $tap;
  173. $opts->{'run'} = 'TestingSuite2';
  174. $opts->{'configs'} = ['testPlatform1'];
  175. $opts->{'testsuite_id'} = 9;
  176. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  177. is($res,undef,"TR Parser doesn't explode on instantiation when spawning run in plan");
  178. isa_ok($tap,"Test::Rail::Parser");
  179. if (!$res) {
  180. $tap->run();
  181. is($tap->{'errors'},0,"No errors encountered uploading case results");
  182. }
  183. #Test spawning of builds not in plans.
  184. #Now, test spawning.
  185. undef $tap;
  186. delete $opts->{'testsuite_id'};
  187. delete $opts->{'plan'};
  188. delete $opts->{'configs'};
  189. $opts->{'testsuite'} = 'HAMBURGER-IZE HUMANITY';
  190. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  191. is($res,undef,"TR Parser doesn't explode on instantiation when spawning run in plan");
  192. isa_ok($tap,"Test::Rail::Parser");
  193. if (!$res) {
  194. $tap->run();
  195. is($tap->{'errors'},0,"No errors encountered uploading case results");
  196. }
  197. #Test spawning of plans and runs.
  198. undef $tap;
  199. $opts->{'run'} = 'BogoRun';
  200. $opts->{'plan'} = 'BogoPlan';
  201. $opts->{'testsuite_id'} = 9;
  202. delete $opts->{'testsuite'};
  203. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  204. is($res,undef,"TR Parser doesn't explode on instantiation when spawning run in plan");
  205. isa_ok($tap,"Test::Rail::Parser");
  206. if (!$res) {
  207. $tap->run();
  208. is($tap->{'errors'},0,"No errors encountered uploading case results");
  209. }
  210. #Verify that case_per_ok and step_results are mutually exclusive, and die.
  211. undef $tap;
  212. $opts->{'case_per_ok'} = 1;
  213. $opts->{'step_results'} = 'sr_step_results';
  214. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  215. isnt($res,undef,"TR Parser explodes on instantiation when mutually exclusive options are passed");
  216. #Check that per-section spawn works
  217. undef $tap;
  218. $opts->{'source'} = 't/fake.test';
  219. delete $opts->{'plan'};
  220. $opts->{'sections'} = ['fake.test'];
  221. delete $opts->{'step_results'};
  222. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  223. is($res,undef,"TR Parser doesn't explode on instantiation");
  224. isa_ok($tap,"Test::Rail::Parser");
  225. if (!$res) {
  226. $tap->run();
  227. is($tap->{'errors'},0,"No errors encountered uploading case results");
  228. }
  229. #Check that per-section spawn works
  230. undef $tap;
  231. $opts->{'plan'} = 'BogoPlan';
  232. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  233. is($res,undef,"TR Parser doesn't explode on instantiation");
  234. isa_ok($tap,"Test::Rail::Parser");
  235. if (!$res) {
  236. $tap->run();
  237. is($tap->{'errors'},0,"No errors encountered uploading case results");
  238. }
  239. undef $tap;
  240. $opts->{'sections'} = ['potzrebie'];
  241. delete $opts->{'plan'};
  242. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  243. isnt($res,undef,"TR Parser explodes on instantiation with invalid section");
  244. undef $tap;
  245. $opts->{'source'} = 't/notests.test';
  246. delete $opts->{'sections'};
  247. delete $opts->{'case_per_ok'};
  248. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  249. is($res,undef,"TR Parser doesn't explode on instantiation");
  250. isa_ok($tap,"Test::Rail::Parser");
  251. if (!$res) {
  252. $tap->run();
  253. is($tap->{'errors'},0,"No errors encountered uploading case results");
  254. is($tap->{'global_status'},4, "Test global result is RETEST on env fail");
  255. }
  256. undef $tap;
  257. $opts->{'source'} = 't/pass.test';
  258. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  259. is($res,undef,"TR Parser doesn't explode on instantiation");
  260. isa_ok($tap,"Test::Rail::Parser");
  261. if (!$res) {
  262. $tap->run();
  263. is($tap->{'errors'},0,"No errors encountered uploading case results");
  264. is($tap->{'global_status'},1, "Test global result is PASS on ok test");
  265. }
  266. undef $tap;
  267. $opts->{'source'} = 't/todo_pass.test';
  268. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  269. is($res,undef,"TR Parser doesn't explode on instantiation");
  270. isa_ok($tap,"Test::Rail::Parser");
  271. if (!$res) {
  272. $tap->run();
  273. is($tap->{'errors'},0,"No errors encountered uploading case results");
  274. is($tap->{'global_status'},8, "Test global result is TODO PASS on todo pass test");
  275. }
  276. #Check autoclose functionality against Run with all tests in run status.
  277. undef $tap;
  278. $opts->{'source'} = 't/skip.test';
  279. $opts->{'run'} = 'FinalRun';
  280. $opts->{'autoclose'} = 1;
  281. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  282. is($res,undef,"TR Parser doesn't explode on instantiation");
  283. isa_ok($tap,"Test::Rail::Parser");
  284. if (!$res) {
  285. $tap->run();
  286. is($tap->{'errors'},0,"No errors encountered uploading case results");
  287. is($tap->{'run_closed'},1, "Run closed by parser when all tests done");
  288. }
  289. #Check autoclose functionality against Run with not all tests in run status.
  290. undef $tap;
  291. $opts->{'source'} = 't/todo_pass.test';
  292. $opts->{'run'} = 'BogoRun';
  293. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  294. is($res,undef,"TR Parser doesn't explode on instantiation");
  295. isa_ok($tap,"Test::Rail::Parser");
  296. if (!$res) {
  297. $tap->run();
  298. is($tap->{'errors'},0,"No errors encountered uploading case results");
  299. is($tap->{'run_closed'},undef, "Run not closed by parser when results are outstanding");
  300. }
  301. #Check that autoclose works against plan wiht all tests in run status
  302. undef $tap;
  303. $opts->{'source'} = 't/fake.test';
  304. $opts->{'run'} = 'FinalRun';
  305. $opts->{'plan'} = 'FinalPlan';
  306. $opts->{'configs'} = ['testConfig'];
  307. $opts->{'case_per_ok'} = 1;
  308. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  309. is($res,undef,"TR Parser doesn't explode on instantiation");
  310. isa_ok($tap,"Test::Rail::Parser");
  311. if (!$res) {
  312. $tap->run();
  313. is($tap->{'errors'},0,"No errors encountered uploading case results");
  314. is($tap->{'plan_closed'},1, "Plan closed by parser when all tests done");
  315. }
  316. #Check that autoclose works against plan with all tests not in run status
  317. undef $tap;
  318. $opts->{'run'} = 'BogoRun';
  319. $opts->{'plan'} = 'BogoPlan';
  320. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  321. is($res,undef,"TR Parser doesn't explode on instantiation");
  322. isa_ok($tap,"Test::Rail::Parser");
  323. if (!$res) {
  324. $tap->run();
  325. is($tap->{'errors'},0,"No errors encountered uploading case results");
  326. is($tap->{'plan_closed'},undef, "Plan not closed by parser when results are outstanding");
  327. }
  328. #Plan but no run 'splodes
  329. undef $tap;
  330. $opts->{'plan'} = 'CompletePlan';
  331. delete $opts->{'run'};
  332. delete $opts->{'configs'};
  333. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  334. like($res,qr/but no run passed/i,"TR Parser explodes on instantiation due to passing plan with no run");
  335. #Check that trying without spawn opts, using completed plan fails
  336. undef $tap;
  337. $opts->{'plan'} = 'ClosedPlan';
  338. $opts->{'run'} = 'BogoRun';
  339. delete $opts->{'testsuite_id'};
  340. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  341. like($res,qr/plan provided is completed/i,"TR Parser explodes on instantiation due to passing closed plan");
  342. #Check that the above two will just spawn a new plan in these cases
  343. $opts->{'testsuite_id'} = 9;
  344. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  345. is($res,undef,"TR Parser runs all the way through on completed run when spawning");
  346. #Check that trying without spawn opts, using completed run fails
  347. undef $tap;
  348. delete $opts->{'testsuite_id'};
  349. delete $opts->{'plan'};
  350. $opts->{'run'} = 'ClosedRun';
  351. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  352. like($res,qr/run provided is completed/i,"TR Parser explodes on instantiation due to passing closed run");
  353. #Check that the above two will just spawn a new run in these cases
  354. $opts->{'testsuite_id'} = 9;
  355. $res = exception { $tap = Test::Rail::Parser->new($opts) };
  356. is($res,undef,"TR Parser runs all the way through on completed run when spawning");