testrail-report 13 KB

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