testrail-tests 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/env perl
  2. # ABSTRACT: List tests in a TestRail run matching the provided filters
  3. # PODNAME: testrail-tests
  4. =head1 SYNOPSIS
  5. testrail-tests [OPTIONS] | xargs prove -PTestrail=...
  6. =head1 DESCRIPTION
  7. testrail-tests - list tests in a run matching the provided filters.
  8. =head2 PARAMETERS:
  9. =head3 MANDATORY PARAMETERS
  10. -j --project [project]: desired project name.
  11. -r --run [run]: desired run name.
  12. =head3 SEMI-OPTIONAL PARAMETERS
  13. -p --plan [plan]: desired plan name. Required if the run passed is a child of a plan.
  14. -m --match [dir]: attempt to find filenames matching the test names in the provided dir.
  15. -n --no-recurse: if match passed, do not recurse subdirectories.
  16. =head3 OPTIONAL PARAMETERS
  17. -c --config [config]: configuration name to filter plans in run. Can be passed multiple times.
  18. -s --status [status]: only list tests marked as [status] in testrail. Can be passed multiple times.
  19. -a --assignedto [user]: only list tests assigned to user. Can be passed multiple times.
  20. =head3 CONFIG OPTIONS
  21. In your \$HOME, (or the current directory, if your system has no
  22. concept of a home directory) put a file called .testrailrc with
  23. key=value syntax separated by newlines.
  24. Valid Keys are: apiurl,user,password
  25. =head3 CONFIG OVERRIDES
  26. These override the config, if present.
  27. If neither are used, you will be prompted.
  28. --apiurl [url] : full URL to get to TestRail index document
  29. --password [key] : Your TestRail Password.
  30. --user [name] : Your TestRail User Name.
  31. =head2 TESTING OPTIONS:
  32. --mock: don't do any real HTTP requests.
  33. --help: show this output
  34. =cut
  35. use strict;
  36. use warnings;
  37. use utf8;
  38. use TestRail::API;
  39. use TestRail::Utils;
  40. use Getopt::Long;
  41. use File::HomeDir qw{my_home};
  42. use File::Find;
  43. use Cwd qw{abs_path};
  44. use File::Basename qw{basename};
  45. sub help {
  46. print("
  47. testrail-tests - list tests in a run matching the provided filters.
  48. USAGE:
  49. testrail-tests [OPTIONS] | xargs prove -PTestrail=...
  50. PARAMETERS:
  51. [MANDATORY PARAMETERS]
  52. -j --project [project]: desired project name.
  53. -r --run [run]: desired run name.
  54. [SEMI-OPTIONAL PARAMETERS]
  55. -p --plan [plan]: desired plan name. Required if the run passed is a child of a plan.
  56. -m --match [dir]: attempt to find filenames matching the test names in the provided dir.
  57. -n --no-recurse: if match passed, do not recurse subdirectories.
  58. [OPTIONAL PARAMETERS]
  59. -c --config [config]: configuration name to filter plans in run. Can be passed multiple times.
  60. -s --status [status]: only list tests marked as [status] in testrail. Can be passed multiple times.
  61. -a --assignedto [user]: only list tests assigned to user. Can be passed multiple times.
  62. [CONFIG OPTIONS]
  63. In your \$HOME, (or the current directory, if your system has no
  64. concept of a home directory) put a file called .testrailrc with
  65. key=value syntax separated by newlines.
  66. Valid Keys are: apiurl,user,password
  67. [CONFIG OVERRIDES]
  68. These override the config, if present.
  69. If neither are used, you will be prompted.
  70. --apiurl [url] : full URL to get to TestRail index document
  71. --password [key] : Your TestRail Password.
  72. --user [name] : Your TestRail User Name.
  73. TESTING OPTIONS:
  74. --mock: don't do any real HTTP requests.
  75. --help: show this output
  76. ");
  77. exit 0;
  78. }
  79. my %opts;
  80. GetOptions(
  81. 'apiurl' => \$opts{'apiurl'},
  82. 'password' => \$opts{'pass'},
  83. 'user' => \$opts{'user'},
  84. 'j|project=s' => \$opts{'project'},
  85. 'p|plan=s' => \$opts{'plan'},
  86. 'r|run=s' => \$opts{'run'},
  87. 'c|config=s@' => \$opts{'configs'},
  88. 's|status=s@' => \$opts{'statuses'},
  89. 'a|assignedto=s@' => \$opts{'users'},
  90. 'mock' => \$opts{'mock'},
  91. 'm|match=s' => \$opts{'match'},
  92. 'n|no-recurse' => \$opts{'no-recurse'}
  93. );
  94. if ($opts{help}) { help(); }
  95. #Parse config file if we are missing api url/key or user
  96. my $homedir = my_home() || '.';
  97. if (-e $homedir . '/.testrailrc' && (!$opts{apiurl} || !$opts{pass} || !$opts{user}) ) {
  98. ($opts{apiurl},$opts{pass},$opts{user}) = TestRail::Utils::parseConfig($homedir);
  99. }
  100. #Interrogate user if they didn't provide info
  101. if (!$opts{apiurl}) {
  102. print "Type the API endpoint url for your testLink install below:\n";
  103. $opts{apiurl} = TestRail::Utils::userInput();
  104. }
  105. if (!$opts{user}) {
  106. print "Type your testLink user name below:\n";
  107. $opts{user} = TestRail::Utils::userInput();
  108. }
  109. if (!$opts{pass}) {
  110. print "Type the password for your testLink user below:\n";
  111. $opts{pass} = TestRail::Utils::userInput();
  112. }
  113. if (!$opts{apiurl} || !$opts{pass} || !$opts{user}) {
  114. print "ERROR: api url, username and password cannot be blank.\n";
  115. exit 1;
  116. }
  117. #Interrogate user if they didn't provide info
  118. if (!$opts{project}) {
  119. print "Type the name of the project you are testing under:\n";
  120. $opts{project} = TestRail::Utils::userInput();
  121. }
  122. # Interrogate user if options were not passed
  123. if (!$opts{run}) {
  124. print "Type the name of the existing run you would like to run against:\n";
  125. $opts{run} = TestRail::Utils::userInput();
  126. }
  127. if ($opts{mock}) {
  128. use Test::LWP::UserAgent::TestRailMock;
  129. $opts{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  130. $opts{debug} = 1;
  131. }
  132. my $tr = TestRail::API->new($opts{apiurl},$opts{user},$opts{pass},$opts{'debug'});
  133. $tr->{'browser'} = $opts{'browser'} if $opts{'browser'};
  134. $tr->{'debug'} = 0;
  135. my $project = $tr->getProjectByName($opts{'project'});
  136. if (!$project) {
  137. print "No such project '$opts{project}'.\n";
  138. exit 6;
  139. }
  140. my ($run,$plan);
  141. if ($opts{'plan'}) {
  142. $plan = $tr->getPlanByName($project->{'id'},$opts{'plan'});
  143. if (!$plan) {
  144. print "No such plan '$opts{plan}'!\n";
  145. exit 1;
  146. }
  147. $run = $tr->getChildRunByName($plan,$opts{'run'}, $opts{'configs'});
  148. } else {
  149. $run = $tr->getRunByName($project->{'id'},$opts{'run'});
  150. }
  151. if (!$run) {
  152. print "No such run '$opts{run}' matching the provided configs (if any).\n";
  153. exit 2;
  154. }
  155. my ($status_ids,$user_ids);
  156. #Process statuses
  157. if ($opts{'statuses'}) {
  158. eval { @$status_ids = $tr->statusNamesToIds(@{$opts{'statuses'}}); };
  159. if ($@) {
  160. print "$@\n";
  161. exit 4;
  162. }
  163. }
  164. #Process assignedto ids
  165. if ($opts{'users'}) {
  166. eval { @$user_ids = $tr->userNamesToIds(@{$opts{'users'}}); };
  167. if ($@) {
  168. print "$@\n";
  169. exit 5;
  170. }
  171. }
  172. my $cases = $tr->getTests($run->{'id'},$status_ids,$user_ids);
  173. if (!$cases) {
  174. print "No cases in TestRail!\n";
  175. exit 3;
  176. }
  177. my @tests = map {$_->{'title'}} @$cases;
  178. my @realtests;
  179. if ($opts{'match'}) {
  180. if (!$opts{'no-recurse'}) {
  181. File::Find::find( sub { push(@realtests,$File::Find::name) if -f }, $opts{'match'} );
  182. @tests = grep {my $real = $_; grep { basename($real) eq $_ } @tests} @realtests; #XXX if you have dups in your tree, be-ware
  183. } else {
  184. @tests = grep {my $fname = $_; grep { basename($_) eq $fname} glob($opts{'match'}."/*") } @tests;
  185. }
  186. }
  187. @tests = map { abs_path($_) } @tests;
  188. print join("\n",@tests)."\n" if scalar(@tests);
  189. exit 0;
  190. __END__
  191. L<TestRail::API>
  192. L<File::HomeDir> for the finding of .testrailrc
  193. =head1 SPECIAL THANKS
  194. Thanks to cPanel Inc, for graciously funding the creation of this module.