testrail-report 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #! /usr/bin/env perl
  2. # ABSTRACT: Upload your TAP results to TestRail after they've finished
  3. # PODNAME: testrail-report
  4. use strict;
  5. use warnings;
  6. use Getopt::Long;
  7. use Term::ANSIColor qw(colorstrip);
  8. use Test::Rail::Parser;
  9. print "testrail-report\n----------------------\n";
  10. sub help {
  11. print "testrail-report - report raw TAP results to a TestRail install
  12. USAGE:
  13. testrail-report [OPTIONS] tapfile
  14. prove -v sometest.t > results.tap && testrail-report [OPTIONS] \\
  15. results.tap
  16. prove -v sometest.t | testrail-report [OPTIONS]
  17. prove -PTestRail='http://some.testlink.install/','someUser',\\
  18. 'somePassword' sometest.t
  19. PARAMETERS:
  20. [MANDATORY PARAMETERS]
  21. --project [someproject] : associate results (if any) with the
  22. provided project name.
  23. --run [somerun] : associates results (if any) with the provided run
  24. name.
  25. IF none of these options are provided, you will be asked to type
  26. these in as needed, supposing you are not redirecting input
  27. (such as piping into this command).
  28. [OPTIONS]
  29. [CONFIG OVERRIDES]
  30. In your \$HOME, put a file called .testrailrc with key=value
  31. syntax separated by newlines. Valid Keys are: apiurl,user,password
  32. [CONFIG OPTIONS] - These override the config, if present.
  33. If neither are used, you will be prompted.
  34. --apiurl [url] : full URL to get to TestRail index document
  35. --password [key] : Your TestRail Password.
  36. --user [name] : Your TestRail User Name.
  37. [BEHAVIOR]
  38. --case-ok : Whether to consider each OK to correspond to
  39. a test in TestRail
  40. --step-results [name] : 'System Name' of a 'step_results' type field
  41. to set for your tests.
  42. These options are mutually exclusive. If neither is set, the
  43. overall result of the test will be used as the pass/fail for the test.
  44. PROVE PLUGIN:
  45. passing -PTestRail=apiurl,user,pass,project,run to prove will
  46. automatically upload your test results while the test is running if
  47. real-time results are desired.
  48. REQUIREMENTS:
  49. Your TestRail install must have 3 custom statuses with the internal
  50. names 'skip', 'todo_pass', and 'todo_fail', to represent those
  51. states which TAP can have.
  52. ";
  53. exit 0;
  54. }
  55. sub userInput {
  56. $| = 1;
  57. my $rt = <STDIN>;
  58. chomp $rt;
  59. return $rt;
  60. }
  61. sub parseConfig {
  62. my $results = {};
  63. my $arr =[];
  64. open(my $fh, '<', $ENV{"HOME"} . '/.testrailrc') or return (undef,undef,undef);#couldn't open!
  65. while (<$fh>) {
  66. chomp;
  67. @$arr = split(/=/,$_);
  68. if (scalar(@$arr) != 2) {
  69. warn("Could not parse $_ in tlreport config\n");
  70. next;
  71. }
  72. $results->{lc($arr->[0])} = $arr->[1];
  73. }
  74. close($fh);
  75. return ($results->{'apiurl'},$results->{'password'},$results->{'user'});
  76. }
  77. #Main loop------------
  78. my ($help,$apiurl,$user,$password,$project,$run,$case_per_ok,$step_results);
  79. #parse switches
  80. GetOptions(
  81. 'run=s' => \$run,
  82. 'apiurl=s' => \$apiurl,
  83. 'password=s' => \$password,
  84. 'user=s' => \$user,
  85. 'project=s' => \$project,
  86. 'case-ok' => \$case_per_ok,
  87. 'step-results=s' => \$step_results,
  88. 'help' => \$help
  89. );
  90. if ($help) { help(); }
  91. #Parse config file if we are missing api url/key or user
  92. if (-e $ENV{"HOME"} . '/.testrailrc' && (!$apiurl || !$password || !$user) ) {
  93. ($apiurl,$password,$user) = parseConfig();
  94. }
  95. #If argument is passed use it instead of stdin
  96. my $file = $ARGV[0];
  97. die "No Such File $file" if ($file && !-e $file);
  98. my ($fh,$fcontents);
  99. if ($file) {
  100. open($fh,'<',$file);
  101. while (<$fh>) {
  102. $_ = colorstrip($_); #strip prove brain damage
  103. s/^\s*//g; #Fix more brain damage
  104. $fcontents .= $_;
  105. }
  106. close($fh);
  107. } else {
  108. #Just read STDIN, print help if no file was passed
  109. if (-t STDIN) { help(); }
  110. if ( !$run || !$apiurl || !$password || !$user || !$project ) { print "ERROR: Interactive mode not allowed when piping input. See --help for options.\n"; exit 0;};
  111. while (<>) {
  112. $_ = colorstrip($_); #strip prove brain damage
  113. s/^\s*//g; #Fix prove brain damage
  114. $fcontents .= $_;
  115. }
  116. }
  117. #Interrogate user if they didn't provide info
  118. if (!$apiurl) {
  119. print "Type the API endpoint url for your testLink install below:\n";
  120. $apiurl = userInput();
  121. }
  122. if (!$user) {
  123. print "Type your testLink user name below:\n";
  124. $user = userInput();
  125. }
  126. if (!$password) {
  127. print "Type the password for your testLink user below:\n";
  128. $password = userInput();
  129. }
  130. if (!$apiurl || !$password || !$user) {
  131. print "ERROR: api url, username and password cannot be blank.\n";
  132. exit 1;
  133. }
  134. #Interrogate user if they didn't provide info
  135. if (!$project) {
  136. print "Type the name of the project you are testing under:\n";
  137. $project = userInput();
  138. }
  139. # Interrogate user if options were not passed
  140. if (!$run) {
  141. print "Type the name of the existing run you would like to run against:\n";
  142. $run = userInput();
  143. }
  144. my $tap = Test::Rail::Parser->new({
  145. 'tap' => $fcontents,
  146. 'apiurl' => $apiurl,
  147. 'user' => $user,
  148. 'pass' => $password,
  149. 'run' => $run,
  150. 'project' => $project,
  151. 'case_per_ok' => $case_per_ok,
  152. 'step_results' => $step_results,
  153. 'merge' => 1
  154. });
  155. $tap->run();
  156. print "Done.\n";
  157. #all done
  158. 0;