testrail-tests 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. =head1 PARAMETERS:
  9. =head2 MANDATORY PARAMETERS
  10. =over 4
  11. --apiurl : full URL to get to TestRail index document
  12. --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
  13. --user : Your TestRail User Name.
  14. -j --project : desired project name.
  15. -r --run : desired run name.
  16. =back
  17. All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
  18. =head2 SEMI-OPTIONAL PARAMETERS
  19. =over 4
  20. -p --plan : desired plan name. Required if the run passed is a child of a plan.
  21. -m --match : attempt to find filenames matching the test names in the provided dir.
  22. --no-match : attempt to find filenames that do not match test names in the provided dir.
  23. -n --no-recurse : if match (or no-match) passed, do not recurse subdirectories.
  24. -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L<Encode::Supported> for supported encodings.
  25. =back
  26. =head2 OPTIONAL PARAMETERS
  27. =over 4
  28. -c --config : configuration name to filter plans in run. Can be passed multiple times.
  29. -s --status : only list tests marked as [status] in testrail. Can be passed multiple times.
  30. -a --assignedto : only list tests assigned to user. Can be passed multiple times.
  31. =back
  32. =head1 CONFIGURATION FILE
  33. In your \$HOME, (or the current directory, if your system has no concept of a home directory) put a file called .testrailrc with key=value syntax separated by newlines.
  34. Valid Keys are the same as documented by L<App::Prove::Plugin::TestRail>.
  35. All options specified thereby are overridden by passing the command-line switches above.
  36. =head1 MISCELLANEOUS OPTIONS:
  37. =over 4
  38. --mock : don't do any real HTTP requests. Used only by tests.
  39. --help : show this output
  40. =back
  41. =cut
  42. use strict;
  43. use warnings;
  44. use utf8;
  45. use TestRail::API;
  46. use TestRail::Utils;
  47. use Getopt::Long;
  48. use File::HomeDir qw{my_home};
  49. my $opts ={};
  50. #Parse config file if we are missing api url/key or user
  51. my $homedir = my_home() || '.';
  52. if (-e $homedir . '/.testrailrc') {
  53. $opts = TestRail::Utils::parseConfig($homedir);
  54. }
  55. GetOptions(
  56. 'apiurl=s' => \$opts->{'apiurl'},
  57. 'password=s' => \$opts->{'password'},
  58. 'user=s' => \$opts->{'user'},
  59. 'j|project=s' => \$opts->{'project'},
  60. 'p|plan=s' => \$opts->{'plan'},
  61. 'r|run=s' => \$opts->{'run'},
  62. 'c|config=s@' => \$opts->{'configs'},
  63. 's|status=s@' => \$opts->{'statuses'},
  64. 'a|assignedto=s@' => \$opts->{'users'},
  65. 'mock' => \$opts->{'mock'},
  66. 'm|match=s' => \$opts->{'match'},
  67. 'no-match=s' => \$opts->{'no-match'},
  68. 'n|no-recurse' => \$opts->{'no-recurse'},
  69. 'e|encoding=s' => \$opts->{'encoding'},
  70. 'h|help' => \$opts->{'help'}
  71. );
  72. if ($opts->{help}) { TestRail::Utils::help(); }
  73. TestRail::Utils::interrogateUser($opts,qw{apiurl user password project run});
  74. if ($opts->{mock}) {
  75. use Test::LWP::UserAgent::TestRailMock;
  76. $opts->{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  77. $opts->{debug} = 1;
  78. }
  79. my $tr = TestRail::API->new($opts->{apiurl},$opts->{user},$opts->{password},$opts->{'encoding'},$opts->{'debug'});
  80. $tr->{'browser'} = $opts->{'browser'} if $opts->{'browser'};
  81. $tr->{'debug'} = 0;
  82. my $project = $tr->getProjectByName($opts->{'project'});
  83. if (!$project) {
  84. print "No such project '$opts->{project}'.\n";
  85. exit 6;
  86. }
  87. my ($run,$plan);
  88. if ($opts->{'plan'}) {
  89. $plan = $tr->getPlanByName($project->{'id'},$opts->{'plan'});
  90. if (!$plan) {
  91. print "No such plan '$opts->{plan}'!\n";
  92. exit 1;
  93. }
  94. $run = $tr->getChildRunByName($plan,$opts->{'run'}, $opts->{'configs'});
  95. } else {
  96. $run = $tr->getRunByName($project->{'id'},$opts->{'run'});
  97. }
  98. if (!$run) {
  99. print "No such run '$opts->{run}' matching the provided configs (if any).\n";
  100. exit 2;
  101. }
  102. my ($status_ids,$user_ids);
  103. #Process statuses
  104. if ($opts->{'statuses'}) {
  105. eval { @$status_ids = $tr->statusNamesToIds(@{$opts->{'statuses'}}); };
  106. if ($@) {
  107. print "$@\n";
  108. exit 4;
  109. }
  110. }
  111. #Process assignedto ids
  112. if ($opts->{'users'}) {
  113. eval { @$user_ids = $tr->userNamesToIds(@{$opts->{'users'}}); };
  114. if ($@) {
  115. print "$@\n";
  116. exit 5;
  117. }
  118. }
  119. my $cases = $tr->getTests($run->{'id'},$status_ids,$user_ids);
  120. if (!$cases) {
  121. print "No cases in TestRail!\n";
  122. exit 3;
  123. }
  124. $opts->{'names-only'} = 1;
  125. my @tests = TestRail::Utils::findTests($opts,@$cases);
  126. print join("\n",@tests)."\n" if scalar(@tests);
  127. exit 0;
  128. __END__
  129. L<TestRail::API>
  130. L<File::HomeDir> for the finding of .testrailrc
  131. =head1 SPECIAL THANKS
  132. Thanks to cPanel Inc, for graciously funding the creation of this distribution.