testrail-tests 6.9 KB

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