testrail-report 12 KB

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