testrail-report 12 KB

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