testrail-bulk-mark-results 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. =head2 PARAMETERS:
  11. =head3 MANDATORY PARAMETERS
  12. -j --project [project]: desired project name.
  13. -r --run [run]: desired run name.
  14. =head3 SEMI-OPTIONAL PARAMETERS
  15. -p --plan [plan]: desired plan name. Required if the run passed is a child of a plan.
  16. -e --encoding: Character encoding of arguments. Defaults to UTF-8.
  17. See L<Encode::Supported> for supported encodings.
  18. =head3 OPTIONAL PARAMETERS
  19. -c --config [config]: configuration name to filter plans in run. Can be passed multiple times.
  20. -a --assignedto [user]: only mark tests assigned to user. Can be passed multiple times.
  21. =head3 CONFIG OPTIONS
  22. In your \$HOME, (or the current directory, if your system has no
  23. concept of a home directory) put a file called .testrailrc with
  24. key=value syntax separated by newlines.
  25. Valid Keys are: apiurl,user,password
  26. =head3 CONFIG OVERRIDES
  27. These override the config, if present.
  28. If neither are used, you will be prompted.
  29. --apiurl [url] : full URL to get to TestRail index document
  30. --password [key] : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
  31. --user [name] : Your TestRail User Name.
  32. =head2 TESTING OPTIONS:
  33. --mock: don't do any real HTTP requests.
  34. --help: show this output
  35. =cut
  36. use strict;
  37. use warnings;
  38. use utf8;
  39. use TestRail::API;
  40. use TestRail::Utils;
  41. use Getopt::Long;
  42. Getopt::Long::Configure('pass_through');
  43. use File::HomeDir qw{my_home};
  44. use File::Find;
  45. use Cwd qw{abs_path};
  46. use File::Basename qw{basename};
  47. use Pod::Perldoc 3.10;
  48. sub help {
  49. @ARGV = ($0);
  50. Pod::Perldoc->run();
  51. exit 0;
  52. }
  53. my %opts;
  54. GetOptions(
  55. 'apiurl=s' => \$opts{'apiurl'},
  56. 'password=s' => \$opts{'pass'},
  57. 'user=s' => \$opts{'user'},
  58. 'j|project=s' => \$opts{'project'},
  59. 'p|plan=s' => \$opts{'plan'},
  60. 'r|run=s' => \$opts{'run'},
  61. 'c|config=s@' => \$opts{'configs'},
  62. 'a|assignedto=s@' => \$opts{'users'},
  63. 'mock' => \$opts{'mock'},
  64. 'e|encoding=s' => \$opts{'encoding'},
  65. 'h|help' => \$opts{'help'}
  66. );
  67. if ($opts{help}) { help(); }
  68. my $status = $ARGV[0];
  69. my $reason = $ARGV[1];
  70. die("No status to set provided.") unless $status;
  71. #Parse config file if we are missing api url/key or user
  72. my $homedir = my_home() || '.';
  73. if (-e $homedir . '/.testrailrc' && (!$opts{apiurl} || !$opts{pass} || !$opts{user}) ) {
  74. ($opts{apiurl},$opts{pass},$opts{user}) = TestRail::Utils::parseConfig($homedir,1);
  75. }
  76. TestRail::Utils::interrogateUser(\%opts,qw{apiurl user pass project run});
  77. if ($opts{mock}) {
  78. use Test::LWP::UserAgent::TestRailMock;
  79. $opts{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  80. $opts{debug} = 1;
  81. }
  82. my $tr = TestRail::API->new($opts{apiurl},$opts{user},$opts{pass},$opts{'encoding'},$opts{'debug'});
  83. $tr->{'browser'} = $opts{'browser'} if $opts{'browser'};
  84. $tr->{'debug'} = 0;
  85. my $project = $tr->getProjectByName($opts{'project'});
  86. if (!$project) {
  87. print "No such project '$opts{project}'.\n";
  88. exit 6;
  89. }
  90. my ($run,$plan);
  91. if ($opts{'plan'}) {
  92. $plan = $tr->getPlanByName($project->{'id'},$opts{'plan'});
  93. if (!$plan) {
  94. print "No such plan '$opts{plan}'!\n";
  95. exit 1;
  96. }
  97. $run = $tr->getChildRunByName($plan,$opts{'run'}, $opts{'configs'});
  98. } else {
  99. $run = $tr->getRunByName($project->{'id'},$opts{'run'});
  100. }
  101. if (!$run) {
  102. print "No such run '$opts{run}' matching the provided configs (if any).\n";
  103. exit 2;
  104. }
  105. my $user_ids;
  106. #Process assignedto ids
  107. if ($opts{'users'}) {
  108. eval { @$user_ids = $tr->userNamesToIds(@{$opts{'users'}}); };
  109. if ($@) {
  110. print "$@\n";
  111. exit 5;
  112. }
  113. }
  114. my $cases = $tr->getTests($run->{'id'},undef,$user_ids);
  115. if (!$cases) {
  116. print "No cases in TestRail to mark!\n";
  117. exit 3;
  118. }
  119. my ($status_id) = $tr->statusNamesToIds($status);
  120. @$cases = map {
  121. {
  122. 'test_id' => $_->{'id'},
  123. 'status_id' => $status_id,
  124. 'comment' => $reason,
  125. 'version' => $opts{'version'}
  126. }
  127. } @$cases;
  128. my $results = $tr->bulkAddResults($run->{'id'},$cases);
  129. print "Successfully set the status of ".scalar(@$results)." cases to $status.\n";
  130. exit 0;
  131. __END__
  132. L<TestRail::API>
  133. L<File::HomeDir> for the finding of .testrailrc
  134. =head1 SPECIAL THANKS
  135. Thanks to cPanel Inc, for graciously funding the creation of this distribution.