TestRail-Utils-Find.t 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. use strict;
  2. use warnings;
  3. use FindBin;
  4. use lib "$FindBin::Bin/lib";
  5. use Test::More 'tests' => 53;
  6. use Test::Fatal;
  7. use Test::Deep;
  8. use File::Basename qw{dirname};
  9. use TestRail::Utils;
  10. use TestRail::Utils::Lock;
  11. use Test::LWP::UserAgent::TestRailMock;
  12. use File::Basename qw{basename};
  13. #FindRuns tests
  14. my $opts = {
  15. 'project' => 'TestProject'
  16. };
  17. my ($apiurl,$login,$pw) = ('http://testrail.local','bogus','bogus');
  18. my $tr = new TestRail::API($apiurl,$login,$pw,undef,1);
  19. #Mock if necesary
  20. $tr->{'debug'} = 0;
  21. $tr->{'browser'} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  22. my $runs = TestRail::Utils::Find::findRuns($opts,$tr);
  23. is(ref $runs, 'ARRAY', "FindRuns returns ARRAYREF");
  24. is(scalar(@$runs),5,"All runs for project found when no other options are passed");
  25. @$runs = map {$_->{'name'}} @$runs;
  26. my @expected = qw{OtherOtherSuite TestingSuite FinalRun lockRun ClosedRun};
  27. cmp_deeply($runs,\@expected,"Tests ordered FIFO by creation date correctly");
  28. $opts->{'lifo'} = 1;
  29. $runs = TestRail::Utils::Find::findRuns($opts,$tr);
  30. @$runs = map {$_->{'name'}} @$runs;
  31. @expected = qw{lockRun ClosedRun TestingSuite FinalRun OtherOtherSuite};
  32. cmp_deeply($runs,\@expected,"Tests ordered LIFO by creation date correctly");
  33. $opts->{'milesort'} = 1;
  34. $runs = TestRail::Utils::Find::findRuns($opts,$tr);
  35. @$runs = map {$_->{'name'}} @$runs;
  36. @expected = qw{OtherOtherSuite TestingSuite FinalRun lockRun ClosedRun};
  37. cmp_deeply($runs,\@expected,"Tests ordered LIFO by milestone date correctly");
  38. delete $opts->{'lifo'};
  39. $runs = TestRail::Utils::Find::findRuns($opts,$tr);
  40. @$runs = map {$_->{'name'}} @$runs;
  41. @expected = qw{TestingSuite FinalRun lockRun ClosedRun OtherOtherSuite};
  42. cmp_deeply($runs,\@expected,"Tests ordered LIFO by milestone date correctly");
  43. delete $opts->{'milesort'};
  44. $opts->{'configs'} = ['eee', 'testConfig'];
  45. $runs = TestRail::Utils::Find::findRuns($opts,$tr);
  46. @$runs = map {$_->{'name'}} @$runs;
  47. is(scalar(@$runs),0,"Filtering runs by configurations works");
  48. $opts->{'configs'} = ['testConfig'];
  49. $runs = TestRail::Utils::Find::findRuns($opts,$tr);
  50. @$runs = map {$_->{'name'}} @$runs;
  51. is(scalar(@$runs),3,"Filtering runs by configurations works");
  52. delete $opts->{'configs'};
  53. $opts->{'statuses'} = ['passed'];
  54. $runs = TestRail::Utils::Find::findRuns($opts,$tr);
  55. is(scalar(@$runs),0,"No passing runs can be found in projects without them");
  56. $opts->{'statuses'} = ['retest'];
  57. $runs = TestRail::Utils::Find::findRuns($opts,$tr);
  58. is(scalar(@$runs),2,"Failed runs can be found in projects with them");
  59. #Test testrail-tests
  60. $opts = {
  61. 'project' => 'TestProject',
  62. 'plan' => 'GosPlan',
  63. 'run' => 'Executing the great plan',
  64. 'match' => $FindBin::Bin,
  65. 'configs' => ['testConfig'],
  66. 'no-recurse' => 1,
  67. 'names-only' => 1
  68. };
  69. my ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  70. my @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  71. @expected = ("$FindBin::Bin/fake.test","$FindBin::Bin/skip.test","$FindBin::Bin/skipall.test");
  72. cmp_deeply(\@tests,\@expected,"findTests: match, no-recurse, plan mode, names-only");
  73. delete $opts->{'names-only'};
  74. @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  75. @tests = map {$_->{'full_title'}} @tests;
  76. cmp_deeply(\@tests,\@expected,"findTests: match, no-recurse, plan mode");
  77. delete $opts->{'match'};
  78. $opts->{'no-match'} = $FindBin::Bin;
  79. $opts->{'names-only'} = 1;
  80. $opts->{'extension'} = '.test';
  81. ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  82. @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  83. is(scalar(grep {$_ eq 'skipall.test'} @tests),0,"Tests in tree are not returned in no-match mode");
  84. is(scalar(grep {$_ eq 'NOT SO SEARED AFTER ALL'} @tests),0,"Tests not in tree that do exist are not returned in no-match mode");
  85. is(scalar(grep {$_ eq $FindBin::Bin.'/faker.test'} @tests),1,"Orphan Tests in tree ARE returned in no-match mode");
  86. is(scalar(@tests),5,"Correct number of non-existant cases shown (no-match, names-only)");
  87. $opts->{'configs'} = ['testPlatform1'];
  88. isnt(exception { TestRail::Utils::Find::getTests($opts,$tr) } , undef,"Correct number of non-existant cases shown (no-match, names-only)");
  89. $opts->{'configs'} = ['testConfig'];
  90. delete $opts->{'names-only'};
  91. @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  92. my @filtered_tests = grep {defined $_} map {$_->{'full_title'}} @tests;
  93. is(scalar(@filtered_tests),0,"Full titles not returned in no-match mode");
  94. is(scalar(@tests),5,"Correct number of nonexistant cases shown in no-match mode");
  95. delete $opts->{'no-recurse'};
  96. $opts->{'names-only'} = 1;
  97. ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  98. @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  99. is(scalar(@tests),9,"Correct number of non-existant cases shown (no-match, names-only, recurse)");
  100. #mutual excl
  101. $opts->{'match'} = $FindBin::Bin;
  102. ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  103. isnt(exception {TestRail::Utils::Find::findTests($opts,@$cases)},undef,"match and no-match are mutually exclusive");
  104. delete $opts->{'no-match'};
  105. $opts->{'orphans'} = $FindBin::Bin;
  106. ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  107. isnt(exception {TestRail::Utils::Find::findTests($opts,@$cases)},undef,"match and orphans are mutually exclusive");
  108. delete $opts->{'match'};
  109. $opts->{'no-match'} = $FindBin::Bin;
  110. ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  111. isnt(exception {TestRail::Utils::Find::findTests($opts,@$cases)},undef,"orphans and no-match are mutually exclusive");
  112. delete $opts->{'orphans'};
  113. delete $opts->{'no-match'};
  114. $opts->{'match'} = $FindBin::Bin;
  115. delete $opts->{'plan'};
  116. $opts->{'run'} = 'TestingSuite';
  117. ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  118. @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  119. is(scalar(@tests),3,"Correct number of non-existant cases shown (match, plain run)");
  120. is(scalar(grep {$_ eq "$FindBin::Bin/skipall.test"} @tests),1,"Tests in tree are returned in match, plain run mode");
  121. #Now that we've made sure configs are ignored...
  122. $opts->{'plan'} = 'GosPlan';
  123. $opts->{'run'} = 'Executing the great plan';
  124. $opts->{'users'} = ['teodesian'];
  125. ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  126. @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  127. is(scalar(@tests),1,"Correct number of cases shown (match, plan run, assignedto pos)");
  128. is(scalar(grep {$_ eq "$FindBin::Bin/skipall.test"} @tests),1,"Tests in tree are returned filtered by assignee");
  129. $opts->{'users'} = ['billy'];
  130. ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  131. @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  132. is(scalar(@tests),0,"Correct number of cases shown (match, plan run, assignedto neg)");
  133. delete $opts->{'users'};
  134. $opts->{'statuses'} = ['passed'];
  135. ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  136. @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  137. is(scalar(@tests),1,"Correct number of cases shown (match, plan run, passed)");
  138. $opts->{'statuses'} = ['failed'];
  139. delete $opts->{'match'};
  140. ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  141. @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  142. is(scalar(@tests),0,"Correct number of cases shown (match, plan run, failed)");
  143. #Test FindTests' finder sub
  144. like(exception { TestRail::Utils::Find::findTests({'match' => '.', 'finder' => sub { return die('got here') } }) }, qr/got here/i, "FindTests callback can fire correctly");
  145. $opts = {
  146. 'project' => 'TestProject',
  147. 'testsuite' => 'HAMBURGER-IZE HUMANITY',
  148. 'directory' => $FindBin::Bin,
  149. 'extension' => '.test'
  150. };
  151. #Test getCases
  152. $cases = TestRail::Utils::Find::getCases($opts,$tr);
  153. is(scalar(@$cases),2,'Case search returns correct number of cases');
  154. #Test findCases
  155. $opts->{'no-missing'} = 1;
  156. my $output = TestRail::Utils::Find::findCases($opts,@$cases);
  157. is($output->{'testsuite_id'},9,'Correct testsuite_id returned by findCases');
  158. is($output->{'missing'},undef,'No missing cases returned');
  159. is($output->{'orphans'},undef,'No orphan cases returned');
  160. is($output->{'update'},undef,'No update cases returned');
  161. delete $opts->{'no-missing'};
  162. $output = TestRail::Utils::Find::findCases($opts,@$cases);
  163. is(scalar(@{$output->{'missing'}}),11,'Correct number of missing cases returned');
  164. is($output->{'orphans'},undef,'No orphan cases returned');
  165. is($output->{'update'},undef,'No update cases returned');
  166. $opts->{'no-missing'} = 1;
  167. $opts->{'orphans'} = 1;
  168. $output = TestRail::Utils::Find::findCases($opts,@$cases);
  169. is($output->{'missing'},undef,'No missing cases returned');
  170. is(scalar(@{$output->{'orphans'}}),1,'1 orphan case returned');
  171. is($output->{'orphans'}->[0]->{'title'},'nothere.test',"Correct orphan case return");
  172. is($output->{'update'},undef,'No update cases returned');
  173. delete $opts->{'orphans'};
  174. $opts->{'update'} = 1;
  175. $output = TestRail::Utils::Find::findCases($opts,@$cases);
  176. is($output->{'missing'},undef,'No missing cases returned');
  177. is($output->{'orphans'},undef,'No orphan cases returned');
  178. is(scalar(@{$output->{'update'}}),1,'1 update case returned');
  179. is($output->{'update'}->[0]->{'title'},'fake.test',"Correct update case return");
  180. delete $opts->{'no-missing'};
  181. $opts->{'orphans'} = 1;
  182. $output = TestRail::Utils::Find::findCases($opts,@$cases);
  183. is(scalar(@{$output->{'missing'}}),11,'Correct number of missing cases returned');
  184. is(scalar(@{$output->{'orphans'}}),1,'1 orphan case returned');
  185. is(scalar(@{$output->{'update'}}),1,'1 update case returned');
  186. delete $opts->{'testsuite_id'};
  187. like(exception {TestRail::Utils::Find::findCases($opts,@$cases)},qr/testsuite_id parameter mandatory/i,"No testsuite_id being passed results in error");
  188. $opts->{'testsuite_id'} = 9;
  189. delete $opts->{'directory'};
  190. like(exception {TestRail::Utils::Find::findCases($opts,@$cases)},qr/Directory parameter mandatory/i,"No directory being passed results in error");
  191. $opts->{'directory'} = 'bogoDir/';
  192. like(exception {TestRail::Utils::Find::findCases($opts,@$cases)},qr/No such directory/i,"Bad directory being passed results in error");
  193. #XXX Deliberately omitting the tests for getResults. It's adequately covered (for now) by testrail-results unit test
  194. #Test synchronize