testrail-report 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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='apiurl=http://some.testlink.install/,user=someUser,password=somePassword,project=TestProject,run=TestRun,plan=TestPlan,configs=Config1:Config2:Config3,version=0.014' sometest.t
  9. =head1 DESCRIPTION
  10. testrail-report - report raw TAP results to a TestRail install
  11. =head2 PARAMETERS:
  12. =head3 MANDATORY PARAMETERS
  13. --project [someproject] : associate results (if any) with theprovided project name.
  14. --run [somerun] : associates results (if any) with the provided run name.
  15. IF none of these options are provided, you will be asked to type
  16. these in as needed, supposing you are not redirecting input
  17. (such as piping into this command).
  18. =head3 SEMI-OPTIONAL PARAMETERS
  19. --plan [someplan] : look for the provided run name within the provided plan.
  20. --config [someconfig] : filter run by the provided configuration.
  21. This option can be passed multiple times for detailed filtering.
  22. Test plans can have runs with the same name, but different configurations, which is understandably confusing.
  23. 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.
  24. So, try not to do that if you want to use this tool, and want sanity in your Test management system.
  25. The way around this is to specify what plan and configuration you want to set results for.
  26. This should provide sufficient uniqueness to get any run using names.
  27. =head3 OPTIONAL PARAMETERS
  28. --spawn [testsuite_id] : Attempt to create a run based on the provided testsuite ID. Uses the name provided with --run.
  29. If plans/configurations are supplied, it will attempt to create it as a child of the provided plan, and with the supplied configurations.
  30. If the specified run already exists, the program will simply use the existing run, and disregard the supplied testsuite_id.
  31. =head3 CONFIG OPTIONS
  32. 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
  33. syntax separated by newlines. Valid Keys are: apiurl,user,password
  34. =head3 CONFIG OVERRIDES
  35. These override the config, if present. If neither are used, you will be prompted.
  36. --apiurl [url] : full URL to get to TestRail index document
  37. --password [key] : Your TestRail Password.
  38. --user [name] : Your TestRail User Name.
  39. =head3 BEHAVIOR
  40. --case-ok : Whether to consider each OK to correspond to a test in TestRail
  41. --step-results [name] : 'System Name' of a 'step_results' type field 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. =head3 RESULT OPTIONS
  45. --version : String describing the version of the system under test.
  46. =head2 PROVE PLUGIN:
  47. passing -PTestRail='key=value,...' to prove will
  48. automatically upload your test results while the test is running if
  49. real-time results are desired.
  50. See L<App::Prove::Plugin::TestRail> for more information.
  51. =head2 REQUIREMENTS:
  52. Your TestRail install must have 3 custom statuses with the internal
  53. names 'skip', 'todo_pass', and 'todo_fail', to represent those
  54. states which TAP can have.
  55. =head2 TESTING OPTIONS:
  56. --mock don't do any real HTTP requests.
  57. =cut
  58. use strict;
  59. use warnings;
  60. use TestRail::Utils;
  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. #Main loop------------
  142. my ($help,$apiurl,$user,$password,$project,$run,$case_per_ok,$step_results,$mock,$configs,$plan,$version,$spawn);
  143. #parse switches
  144. GetOptions(
  145. 'run=s' => \$run,
  146. 'apiurl=s' => \$apiurl,
  147. 'password=s' => \$password,
  148. 'user=s' => \$user,
  149. 'project=s' => \$project,
  150. 'case-ok' => \$case_per_ok,
  151. 'step-results=s' => \$step_results,
  152. 'mock' => \$mock,
  153. 'config=s@' => \$configs,
  154. 'plan=s' => \$plan,
  155. 'version=s' => \$version,
  156. 'spawn=i' => \$spawn,
  157. 'help' => \$help
  158. );
  159. if ($help) { help(); }
  160. #Parse config file if we are missing api url/key or user
  161. my $homedir = my_home() || '.';
  162. if (-e $homedir . '/.testrailrc' && (!$apiurl || !$password || !$user) ) {
  163. ($apiurl,$password,$user) = TestRail::Utils::parseConfig($homedir);
  164. }
  165. #XXX not even close to optimized, don't slurp in the future
  166. #If argument is passed use it instead of stdin
  167. my $file = $ARGV[0];
  168. die "No Such File $file" if ($file && !-e $file);
  169. my ($fh,$fcontents,@files);
  170. if ($file) {
  171. open($fh,'<',$file);
  172. while (<$fh>) {
  173. $_ = colorstrip($_); #strip prove brain damage
  174. if ($_ =~ /(.*)\s\.\.$/) {
  175. #File marker from default prove
  176. unless ($_ =~ /^[ok|not ok] - /) {
  177. push(@files,$fcontents) if $fcontents;
  178. $fcontents = '';
  179. }
  180. }
  181. $fcontents .= $_;
  182. }
  183. close($fh);
  184. push(@files,$fcontents) if $fcontents;
  185. } else {
  186. #Just read STDIN, print help if no file was passed
  187. if (IO::Interactive::Tiny::is_interactive()) {
  188. print "ERROR: no file passed, and no data piped in! See --help for usage.\n";
  189. exit(1);
  190. }
  191. if ( !$run || !$apiurl || !$password || !$user || !$project ) {
  192. print "ERROR: Interactive mode not allowed when piping input. See --help for options.\n";
  193. exit(1);
  194. }
  195. while (<>) {
  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. help() if !$fcontents; #Nothing passed to stdin!
  207. push(@files,$fcontents) if $fcontents;
  208. }
  209. #Interrogate user if they didn't provide info
  210. if (!$apiurl) {
  211. print "Type the API endpoint url for your testLink install below:\n";
  212. $apiurl = TestRail::Utils::userInput();
  213. }
  214. if (!$user) {
  215. print "Type your testLink user name below:\n";
  216. $user = TestRail::Utils::userInput();
  217. }
  218. if (!$password) {
  219. print "Type the password for your testLink user below:\n";
  220. $password = TestRail::Utils::userInput();
  221. }
  222. if (!$apiurl || !$password || !$user) {
  223. print "ERROR: api url, username and password cannot be blank.\n";
  224. exit 1;
  225. }
  226. #Interrogate user if they didn't provide info
  227. if (!$project) {
  228. print "Type the name of the project you are testing under:\n";
  229. $project = TestRail::Utils::userInput();
  230. }
  231. # Interrogate user if options were not passed
  232. if (!$run) {
  233. print "Type the name of the existing run you would like to run against:\n";
  234. $run = TestRail::Utils::userInput();
  235. }
  236. my $debug = 0;
  237. my $browser;
  238. if ($mock) {
  239. use Test::LWP::UserAgent::TestRailMock;
  240. $browser = $Test::LWP::UserAgent::TestRailMock::mockObject;
  241. $debug = 1;
  242. }
  243. my $result_options = undef;
  244. $result_options = {'version' => $version} if $version;
  245. my $tap;
  246. foreach my $phil (@files) {
  247. print "Processing $phil...\n";
  248. $tap = Test::Rail::Parser->new({
  249. 'tap' => $phil,
  250. 'apiurl' => $apiurl,
  251. 'user' => $user,
  252. 'pass' => $password,
  253. 'run' => $run,
  254. 'project' => $project,
  255. 'case_per_ok' => $case_per_ok,
  256. 'step_results' => $step_results,
  257. 'debug' => $debug,
  258. 'browser' => $browser,
  259. 'plan' => $plan,
  260. 'configs' => $configs,
  261. 'result_options' => $result_options,
  262. 'spawn' => $spawn,
  263. 'merge' => 1
  264. });
  265. $tap->run();
  266. }
  267. print "Done.\n";
  268. #all done
  269. 0;
  270. __END__
  271. =head1 SEE ALSO
  272. L<TestRail::API>
  273. L<App::Prove::Plugin::TestRail>
  274. L<TAP::Parser>
  275. L<File::HomeDir> for the finding of .testrailrc
  276. =head1 SPECIAL THANKS
  277. Thanks to cPanel Inc, for graciously funding the creation of this module.