testrail-report 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #! /usr/bin/env perl
  2. # ABSTRACT: Upload your TAP results to TestRail after they've finished
  3. # PODNAME: testrail-report
  4. =head1 SYNOPSIS
  5. testrail-report [OPTIONS] tapfile
  6. prove -v sometest.t > results.tap && testrail-report [OPTIONS] results.tap
  7. prove -v sometest.t | testrail-report [OPTIONS]
  8. prove -PTestRail='http://some.testlink.install/,someUser,somePassword,someProject,someRun,0,step_results' sometest.t
  9. =head1 DESCRIPTION
  10. testrail-report - report raw TAP results to a TestRail install
  11. USAGE:
  12. =head2 PARAMETERS:
  13. =head3 MANDATORY PARAMETERS
  14. --project [someproject] : associate results (if any) with theprovided project name.
  15. --run [somerun] : associates results (if any) with the provided run name.
  16. IF none of these options are provided, you will be asked to type
  17. these in as needed, supposing you are not redirecting input
  18. (such as piping into this command).
  19. =head3 SEMI-OPTIONAL PARAMETERS
  20. --plan [someplan] : look for the provided run name within the provided plan.
  21. --config [someconfig] : filter run by the provided configuration.
  22. This option can be passed multiple times for detailed filtering.
  23. Test plans can have runs with the same name, but different configurations, which is understandably confusing.
  24. You can do the same outside of plans, and without configurations; but doing so is ill advised, and the only option from there is to use IDs.
  25. So, try not to do that if you want to use this tool, and want sanity in your Test management system.
  26. The way around this is to specify what plan and configuration you want to set results for.
  27. This should provide sufficient uniqueness to get to any run using names.
  28. =head3 CONFIG OVERRIDES
  29. In your $HOME, put a file called .testrailrc with key=value
  30. syntax separated by newlines. Valid Keys are: apiurl,user,password
  31. =head3 CONFIG OPTIONS
  32. These override the config, if present. If neither are used, you will be prompted.
  33. --apiurl [url] : full URL to get to TestRail index document
  34. --password [key] : Your TestRail Password.
  35. --user [name] : Your TestRail User Name.
  36. =head3 BEHAVIOR
  37. --case-ok : Whether to consider each OK to correspond to a test in TestRail
  38. --step-results [name] : 'System Name' of a 'step_results' type field to set for your tests.
  39. These options are mutually exclusive. If neither is set, the
  40. overall result of the test will be used as the pass/fail for the test.
  41. =head3 RESULT OPTIONS
  42. --version : String describing the version of the system under test.
  43. =head2 PROVE PLUGIN:
  44. passing -PTestRail=apiurl,user,pass,project,run to prove will
  45. automatically upload your test results while the test is running if
  46. real-time results are desired.
  47. See L<App::Prove::Plugin::TestRail> for more information.
  48. =head2 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. =head2 TESTING OPTIONS:
  53. --mock don't do any real HTTP requests.
  54. =cut
  55. use strict;
  56. use warnings;
  57. use Getopt::Long;
  58. use Term::ANSIColor qw(colorstrip);
  59. use Test::Rail::Parser;
  60. use IO::Interactive::Tiny ();
  61. print "testrail-report\n----------------------\n";
  62. sub help {
  63. print "testrail-report - report raw TAP results to a TestRail install
  64. USAGE:
  65. testrail-report [OPTIONS] tapfile
  66. prove -v sometest.t > results.tap && testrail-report [OPTIONS] \\
  67. results.tap
  68. prove -v sometest.t | testrail-report [OPTIONS]
  69. prove -PTestRail='http://some.testlink.install/','someUser',\\
  70. 'somePassword' sometest.t
  71. PARAMETERS:
  72. [MANDATORY PARAMETERS]
  73. --project [someproject] : associate results (if any) with the
  74. provided project name.
  75. --run [somerun] : associates results (if any) with the provided run
  76. name.
  77. IF none of these options are provided, you will be asked to type
  78. these in as needed, supposing you are not redirecting input
  79. (such as piping into this command).
  80. [SEMI-OPTIONAL PARAMETERS]
  81. --plan [someplan] : look for the provided run name within
  82. the provided plan.
  83. --configs [someconfigs] : filter run by the provided configuration.
  84. This option can be passed multiple times for detailed filtering.
  85. Test plans can have runs with the same name, but different
  86. configurations, which is understandably confusing. You can do the
  87. same outside of plans, and without configurations; but doing so is
  88. ill-advised, and the only option from there is to use IDs. So, try
  89. not to do that if you want to use this tool, and want sanity in your
  90. Test Management System.
  91. The way around this is to specify what plan and configuration
  92. you want to set results for. This should provide sufficient
  93. uniqueness to get to any run using words.
  94. [CONFIG OVERRIDES]
  95. In your \$HOME, put a file called .testrailrc with key=value
  96. syntax separated by newlines. Valid Keys are: apiurl,user,password
  97. [CONFIG OPTIONS] - These override the config, if present.
  98. If neither are used, you will be prompted.
  99. --apiurl [url] : full URL to get to TestRail index document
  100. --password [key] : Your TestRail Password.
  101. --user [name] : Your TestRail User Name.
  102. [BEHAVIOR]
  103. --case-ok : Whether to consider each OK to correspond to
  104. a test in TestRail
  105. --step-results [name] : 'System Name' of a 'step_results' type field
  106. to set for your tests.
  107. These options are mutually exclusive. If neither is set, the
  108. overall result of the test will be used as the pass/fail for the test.
  109. [RESULT OPTIONS]
  110. --version : String describing the version of the system under test.
  111. PROVE PLUGIN:
  112. passing -PTestRail=apiurl,user,pass,project,run to prove will
  113. automatically upload your test results while the test is running if
  114. real-time results are desired.
  115. See App::Prove::Plugin::TestRail for more information.
  116. REQUIREMENTS:
  117. Your TestRail install must have 3 custom statuses with the internal
  118. names 'skip', 'todo_pass', and 'todo_fail', to represent those
  119. states which TAP can have.
  120. TESTING OPTIONS:
  121. --mock don't do any real HTTP requests.
  122. ";
  123. exit 0;
  124. }
  125. sub userInput {
  126. $| = 1;
  127. my $rt = <STDIN>;
  128. chomp $rt;
  129. return $rt;
  130. }
  131. sub parseConfig {
  132. my $results = {};
  133. my $arr =[];
  134. open(my $fh, '<', $ENV{"HOME"} . '/.testrailrc') or return (undef,undef,undef);#couldn't open!
  135. while (<$fh>) {
  136. chomp;
  137. @$arr = split(/=/,$_);
  138. if (scalar(@$arr) != 2) {
  139. warn("Could not parse $_ in tlreport config\n");
  140. next;
  141. }
  142. $results->{lc($arr->[0])} = $arr->[1];
  143. }
  144. close($fh);
  145. return ($results->{'apiurl'},$results->{'password'},$results->{'user'});
  146. }
  147. #Main loop------------
  148. my ($help,$apiurl,$user,$password,$project,$run,$case_per_ok,$step_results,$mock,$configs,$plan,$version);
  149. #parse switches
  150. GetOptions(
  151. 'run=s' => \$run,
  152. 'apiurl=s' => \$apiurl,
  153. 'password=s' => \$password,
  154. 'user=s' => \$user,
  155. 'project=s' => \$project,
  156. 'case-ok' => \$case_per_ok,
  157. 'step-results=s' => \$step_results,
  158. 'mock' => \$mock,
  159. 'config=s@' => \$configs,
  160. 'plan=s' => \$plan,
  161. 'version=s' => \$version,
  162. 'help' => \$help
  163. );
  164. if ($help) { help(); }
  165. #Parse config file if we are missing api url/key or user
  166. if (-e $ENV{"HOME"} . '/.testrailrc' && (!$apiurl || !$password || !$user) ) {
  167. ($apiurl,$password,$user) = parseConfig();
  168. }
  169. #XXX not even close to optimized, don't slurp in the future
  170. #If argument is passed use it instead of stdin
  171. my $file = $ARGV[0];
  172. die "No Such File $file" if ($file && !-e $file);
  173. my ($fh,$fcontents,@files);
  174. if ($file) {
  175. open($fh,'<',$file);
  176. while (<$fh>) {
  177. $_ = colorstrip($_); #strip prove brain damage
  178. if ($_ =~ /(.*)\s\.\.$/) {
  179. #File marker from default prove
  180. unless ($_ =~ /^[ok|not ok] - /) {
  181. push(@files,$fcontents) if $fcontents;
  182. $fcontents = '';
  183. }
  184. }
  185. $fcontents .= $_;
  186. }
  187. close($fh);
  188. push(@files,$fcontents) if $fcontents;
  189. } else {
  190. #Just read STDIN, print help if no file was passed
  191. if (IO::Interactive::Tiny::is_interactive()) {
  192. print "ERROR: no file passed, and no data piped in! See --help for usage.\n";
  193. exit(1);
  194. }
  195. if ( !$run || !$apiurl || !$password || !$user || !$project ) {
  196. print "ERROR: Interactive mode not allowed when piping input. See --help for options.\n";
  197. exit(1);
  198. }
  199. while (<>) {
  200. $_ = colorstrip($_); #strip prove brain damage
  201. if ($_ =~ /(.*)\s\.\.$/) {
  202. #File marker from default prove
  203. unless ($_ =~ /^[ok|not ok] - /) {
  204. push(@files,$fcontents) if $fcontents;
  205. $fcontents = '';
  206. }
  207. }
  208. $fcontents .= $_;
  209. }
  210. help() if !$fcontents; #Nothing passed to stdin!
  211. push(@files,$fcontents) if $fcontents;
  212. }
  213. #Interrogate user if they didn't provide info
  214. if (!$apiurl) {
  215. print "Type the API endpoint url for your testLink install below:\n";
  216. $apiurl = userInput();
  217. }
  218. if (!$user) {
  219. print "Type your testLink user name below:\n";
  220. $user = userInput();
  221. }
  222. if (!$password) {
  223. print "Type the password for your testLink user below:\n";
  224. $password = userInput();
  225. }
  226. if (!$apiurl || !$password || !$user) {
  227. print "ERROR: api url, username and password cannot be blank.\n";
  228. exit 1;
  229. }
  230. #Interrogate user if they didn't provide info
  231. if (!$project) {
  232. print "Type the name of the project you are testing under:\n";
  233. $project = userInput();
  234. }
  235. # Interrogate user if options were not passed
  236. if (!$run) {
  237. print "Type the name of the existing run you would like to run against:\n";
  238. $run = userInput();
  239. }
  240. my $debug = 0;
  241. my $browser;
  242. if ($mock) {
  243. use Test::LWP::UserAgent::TestRailMock;
  244. $browser = $Test::LWP::UserAgent::TestRailMock::mockObject;
  245. $debug = 1;
  246. }
  247. my $result_options = undef;
  248. $result_options = {'version' => $version} if $version;
  249. my $tap;
  250. foreach my $phil (@files) {
  251. print "Processing $phil...\n";
  252. $tap = Test::Rail::Parser->new({
  253. 'tap' => $phil,
  254. 'apiurl' => $apiurl,
  255. 'user' => $user,
  256. 'pass' => $password,
  257. 'run' => $run,
  258. 'project' => $project,
  259. 'case_per_ok' => $case_per_ok,
  260. 'step_results' => $step_results,
  261. 'debug' => $debug,
  262. 'browser' => $browser,
  263. 'version' => $version,
  264. 'plan' => $plan,
  265. 'configs' => $configs,
  266. 'result_options' => $result_options,
  267. 'merge' => 1
  268. });
  269. $tap->run();
  270. }
  271. print "Done.\n";
  272. #all done
  273. 0;
  274. __END__
  275. =head1 SEE ALSO
  276. L<TestRail::API>
  277. L<App::Prove::Plugin::TestRail>
  278. L<TAP::Parser>
  279. =head1 SPECIAL THANKS
  280. Thanks to cPanel Inc, for graciously funding the creation of this module.