testrail-report 12 KB

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