testrail-tests 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 directory.
  22. --no-match : attempt to find filenames that do not match test names in the provided directory.
  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,$plan,$run) = TestRail::Utils::getRunInformation($tr,$opts);
  83. my ($status_ids,$user_ids);
  84. #Process statuses
  85. if ($opts->{'statuses'}) {
  86. eval { @$status_ids = $tr->statusNamesToIds(@{$opts->{'statuses'}}); };
  87. if ($@) {
  88. print "$@\n";
  89. exit 4;
  90. }
  91. }
  92. #Process assignedto ids
  93. if ($opts->{'users'}) {
  94. eval { @$user_ids = $tr->userNamesToIds(@{$opts->{'users'}}); };
  95. if ($@) {
  96. print "$@\n";
  97. exit 5;
  98. }
  99. }
  100. my $cases = $tr->getTests($run->{'id'},$status_ids,$user_ids);
  101. if (!$cases) {
  102. print "No cases in TestRail!\n";
  103. exit 3;
  104. }
  105. $opts->{'names-only'} = 1;
  106. my @tests = TestRail::Utils::findTests($opts,@$cases);
  107. print join("\n",@tests)."\n" if scalar(@tests);
  108. exit 0;
  109. __END__
  110. L<TestRail::API>
  111. L<File::HomeDir> for the finding of .testrailrc
  112. =head1 SPECIAL THANKS
  113. Thanks to cPanel Inc, for graciously funding the creation of this distribution.