testrail-bulk-mark-results 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/env perl
  2. # ABSTRACT: Bulk mark entire runs/plans (or groups of tests therein) as the provided status.
  3. # PODNAME: testrail-bulk-mark-results
  4. =head1 USAGE
  5. testrail-bulk-mark-results [OPTIONS] status [reason]
  6. =head1 DESCRIPTION
  7. Sometimes it is useful to mark entire runs of tests when, for example, a prerequisite test in a sequence invalidates all further tests.
  8. For example, if a binary produced for test fails to run at all, more detailed testing will be impossible;
  9. it would save time to just mark everything as blocked.
  10. =head1 PARAMETERS:
  11. =head2 MANDATORY PARAMETERS
  12. =over 4
  13. --apiurl : full URL to get to TestRail index document
  14. --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
  15. --user : Your TestRail User Name.
  16. -j --project : desired project name.
  17. -r --run : desired run name.
  18. =back
  19. All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
  20. =head2 SEMI-OPTIONAL PARAMETERS
  21. =over 4
  22. -p --plan : desired plan name. Required if the run passed is a child of a plan.
  23. -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L<Encode::Supported> for supported encodings.
  24. =back
  25. =head2 OPTIONAL PARAMETERS
  26. =over 4
  27. -c --config : configuration name to filter plans in run. Can be passed multiple times.
  28. -a --assignedto : only mark tests assigned to user. Can be passed multiple times.
  29. =back
  30. =head1 CONFIGURATION FILE
  31. 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.
  32. Valid Keys are the same as documented by L<App::Prove::Plugin::TestRail>.
  33. All options specified thereby are overridden by passing the command-line switches above.
  34. =head1 MISCELLANEOUS OPTIONS:
  35. =over 4
  36. --mock : don't do any real HTTP requests.
  37. --help : show this output
  38. =back
  39. =cut
  40. use strict;
  41. use warnings;
  42. use utf8;
  43. use TestRail::API;
  44. use TestRail::Utils;
  45. use Getopt::Long;
  46. Getopt::Long::Configure('pass_through');
  47. use File::HomeDir qw{my_home};
  48. my $opts = {};
  49. # Parse config file
  50. my $homedir = my_home() || '.';
  51. if (-e $homedir . '/.testrailrc') {
  52. $opts = TestRail::Utils::parseConfig($homedir);
  53. }
  54. # Override configuration with switches
  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. 'a|assignedto=s@' => \$opts->{'users'},
  64. 'mock' => \$opts->{'mock'},
  65. 'e|encoding=s' => \$opts->{'encoding'},
  66. 'h|help' => \$opts->{'help'}
  67. );
  68. if ($opts->{help}) { TestRail::Utils::help(); }
  69. my $status = $ARGV[0];
  70. my $reason = $ARGV[1];
  71. die("No status to set provided.") unless $status;
  72. TestRail::Utils::interrogateUser($opts,qw{apiurl user password project run});
  73. if ($opts->{mock}) {
  74. use Test::LWP::UserAgent::TestRailMock;
  75. $opts->{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  76. $opts->{debug} = 1;
  77. }
  78. my $tr = TestRail::API->new($opts->{apiurl},$opts->{user},$opts->{password},$opts->{'encoding'},$opts->{'debug'});
  79. $tr->{'browser'} = $opts->{'browser'} if $opts->{'browser'};
  80. $tr->{'debug'} = 0;
  81. my $project = $tr->getProjectByName($opts->{'project'});
  82. if (!$project) {
  83. print "No such project '$opts->{project}'.\n";
  84. exit 6;
  85. }
  86. my ($run,$plan);
  87. if ($opts->{'plan'}) {
  88. $plan = $tr->getPlanByName($project->{'id'},$opts->{'plan'});
  89. if (!$plan) {
  90. print "No such plan '$opts->{plan}'!\n";
  91. exit 1;
  92. }
  93. $run = $tr->getChildRunByName($plan,$opts->{'run'}, $opts->{'configs'});
  94. } else {
  95. $run = $tr->getRunByName($project->{'id'},$opts->{'run'});
  96. }
  97. if (!$run) {
  98. print "No such run '$opts->{run}' matching the provided configs (if any).\n";
  99. exit 2;
  100. }
  101. my $user_ids;
  102. #Process assignedto ids
  103. if ($opts->{'users'}) {
  104. eval { @$user_ids = $tr->userNamesToIds(@{$opts->{'users'}}); };
  105. if ($@) {
  106. print "$@\n";
  107. exit 5;
  108. }
  109. }
  110. my $cases = $tr->getTests($run->{'id'},undef,$user_ids);
  111. if (!$cases) {
  112. print "No cases in TestRail to mark!\n";
  113. exit 3;
  114. }
  115. my ($status_id) = $tr->statusNamesToIds($status);
  116. @$cases = map {
  117. {
  118. 'test_id' => $_->{'id'},
  119. 'status_id' => $status_id,
  120. 'comment' => $reason,
  121. 'version' => $opts->{'version'}
  122. }
  123. } @$cases;
  124. my $results = $tr->bulkAddResults($run->{'id'},$cases);
  125. print "Successfully set the status of ".scalar(@$results)." cases to $status.\n";
  126. exit 0;
  127. __END__
  128. L<TestRail::API>
  129. L<File::HomeDir> for the finding of .testrailrc
  130. =head1 SPECIAL THANKS
  131. Thanks to cPanel Inc, for graciously funding the creation of this distribution.