testrail-report 13 KB

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