testrail-bulk-mark-results 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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,$plan,$run) = TestRail::Utils::getRunInformation($tr,$opts);
  82. my $user_ids;
  83. #Process assignedto ids
  84. if ($opts->{'users'}) {
  85. eval { @$user_ids = $tr->userNamesToIds(@{$opts->{'users'}}); };
  86. if ($@) {
  87. print "$@\n";
  88. exit 5;
  89. }
  90. }
  91. my $cases = $tr->getTests($run->{'id'},undef,$user_ids);
  92. if (!$cases) {
  93. print "No cases in TestRail to mark!\n";
  94. exit 3;
  95. }
  96. my ($status_id) = $tr->statusNamesToIds($status);
  97. @$cases = map {
  98. {
  99. 'test_id' => $_->{'id'},
  100. 'status_id' => $status_id,
  101. 'comment' => $reason,
  102. 'version' => $opts->{'version'}
  103. }
  104. } @$cases;
  105. my $results = $tr->bulkAddResults($run->{'id'},$cases);
  106. print "Successfully set the status of ".scalar(@$results)." cases to $status.\n";
  107. exit 0;
  108. __END__
  109. L<TestRail::API>
  110. L<File::HomeDir> for the finding of .testrailrc
  111. =head1 SPECIAL THANKS
  112. Thanks to cPanel Inc, for graciously funding the creation of this distribution.