testrail-report 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/perl
  2. # ABSTRACT: Upload your TAP results to TestRail after they've finished
  3. # PODNAME: TestRail::Bin::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.testrail.install/,user=someUser,password=somePassword,project=TestProject,run=TestRun,plan=TestPlan,configs=Config1:Config2:Config3,version=0.014' sometest.t
  9. require `which testrail-report`;
  10. TestRail::Bin::Report::run('args' => @args);
  11. =head1 DESCRIPTION
  12. testrail-report - report raw TAP results to a TestRail install
  13. Can be used as the modulino TestRail::Bin::Report.
  14. Has a single 'run' function which accepts a hash with the 'args' parameter being the array of arguments.
  15. =head2 PARAMETERS:
  16. =head3 MANDATORY PARAMETERS
  17. --project [someproject] : associate results (if any) with theprovided project name.
  18. --run [somerun] : associates results (if any) with the provided run name.
  19. IF none of these options are provided, you will be asked to type
  20. these in as needed, supposing you are not redirecting input
  21. (such as piping into this command).
  22. =head3 SEMI-OPTIONAL PARAMETERS
  23. --plan [someplan] : look for the provided run name within the provided plan.
  24. --config [someconfig] : filter run by the provided configuration.
  25. This option can be passed multiple times for detailed filtering.
  26. -e --encoding: Character encoding of arguments. Defaults to UTF-8.
  27. See L<Encode::Supported> for supported encodings.
  28. Test plans can have runs with the same name, but different configurations, which is understandably confusing.
  29. 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.
  30. So, try not to do that if you want to use this tool, and want sanity in your Test management system.
  31. The way around this is to specify what plan and configuration you want to set results for.
  32. This should provide sufficient uniqueness to get any run using names.
  33. =head3 OPTIONAL PARAMETERS
  34. --testsuite_id [testsuite_id] : Attempt to create a run based on the provided testsuite ID. Uses the name provided with --run.
  35. If plans/configurations are supplied, it will attempt to create it as a child of the provided plan, and with the supplied configurations.
  36. If the specified run already exists, the program will simply use the existing run, and disregard the supplied testsuite_id.
  37. If the specified plan does not exist, it too will be created for you.
  38. --testsuite [testsuite name] : Pretty much the same as the testsuite_id, but a name instead. Mutually exclusive with testsuite_id.
  39. --section [section_name] : When spawning, restrict the cases used in the provided testsuite ID to these sections.
  40. Option may be passed multiple times to specify multiple sections.
  41. --num_tries [X] : Number of times to attempt an API request. Default 1.
  42. =head3 CONFIG OPTIONS
  43. 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
  44. syntax separated by newlines. Valid Keys are: apiurl,user,password
  45. =head3 CONFIG OVERRIDES
  46. These override the config, if present. If neither are used, you will be prompted.
  47. --apiurl [url] : full URL to get to TestRail index document
  48. --password [key] : Your TestRail Password or a valid API key (TestRail 4.2 and above).
  49. --user [name] : Your TestRail User Name.
  50. =head3 BEHAVIOR
  51. --case-ok : Whether to consider each OK to correspond to a test in TestRail
  52. --step-results [name] : 'System Name' of a 'step_results' type field to set for your tests.
  53. These options are mutually exclusive. If neither is set, the
  54. overall result of the test will be used as the pass/fail for the test.
  55. =head3 RESULT OPTIONS
  56. --version : String describing the version of the system under test.
  57. --autoclose : If there are no more tests in 'untested' or 'retest' status for the specified run/plan, close it.
  58. =head2 PROVE PLUGIN:
  59. passing -PTestRail='key=value,...' to prove will
  60. automatically upload your test results while the test is running if
  61. real-time results are desired.
  62. See L<App::Prove::Plugin::TestRail> for more information.
  63. =head2 REQUIREMENTS:
  64. Your TestRail install must have 3 custom statuses with the internal
  65. names 'skip', 'todo_pass', and 'todo_fail', to represent those
  66. states which TAP can have.
  67. Also, be sure your tests don't output non-TAP (unknown) lines ending in dots (.)
  68. This will cause the preceding characters to be interpreted as a test name, which may lead to unexpected results.
  69. =cut
  70. package TestRail::Bin::Report;
  71. use strict;
  72. use warnings;
  73. use TestRail::Utils;
  74. use Getopt::Long qw{GetOptionsFromArray};
  75. use Test::Rail::Parser;
  76. use File::HomeDir qw{my_home};
  77. if (!caller()) {
  78. my ($out,$code) = run('args' => \@ARGV);
  79. print $out;
  80. exit $code;
  81. }
  82. #Main loop------------
  83. sub run {
  84. my %params = @_;
  85. my %opts;
  86. #parse switches
  87. GetOptionsFromArray($params{'args'},
  88. 'run=s' => \$opts{run},
  89. 'apiurl=s' => \$opts{apiurl},
  90. 'password=s' => \$opts{password},
  91. 'user=s' => \$opts{user},
  92. 'project=s' => \$opts{project},
  93. 'step-results=s' => \$opts{step_results},
  94. 'config=s@' => \$opts{configs},
  95. 'plan=s' => \$opts{plan},
  96. 'version=s' => \$opts{version},
  97. 'testsuite_id=i' => \$opts{testsuite_id},
  98. 'testsuite=s' => \$opts{testsuite},
  99. 'section=s@' => \$opts{sections},
  100. 'autoclose' => \$opts{autoclose},
  101. 'e|encoding=s' => \$opts{encoding},
  102. 'max_tries=i' => \$opts{max_tries},
  103. 'help' => \$opts{help},
  104. );
  105. if ($opts{help}) { return ('',TestRail::Utils::help()); }
  106. $opts{'browser'} = $params{'browser'};
  107. #Parse config file if we are missing api url/key or user
  108. my $homedir = my_home() || '.';
  109. if (-e $homedir . '/.testrailrc' && (!$opts{apiurl} || !$opts{password} || !$opts{user}) ) {
  110. ($opts{apiurl},$opts{password},$opts{user}) = TestRail::Utils::parseConfig($homedir,1);
  111. }
  112. #If argument is passed use it instead of stdin
  113. my $file = $params{'args'}->[0];
  114. die "No Such File $file" if ($file && !-e $file);
  115. die "ERROR: Interactive mode not allowed when piping input. See --help for options." if ( !$opts{run} || !$opts{apiurl} || !$opts{password} || !$opts{user} || !$opts{project} );
  116. print "testrail-report\n----------------------\n";
  117. my @files = TestRail::Utils::TAP2TestFiles($file);
  118. TestRail::Utils::interrogateUser(\%opts,qw{apiurl user password project run});
  119. $opts{result_options} = {'version' => $opts{version}} if $opts{version};
  120. $opts{'debug'} = 1 if $opts{'browser'};
  121. my $tap;
  122. foreach my $phil (@files) {
  123. $tap = Test::Rail::Parser->new({
  124. 'tap' => $phil,
  125. 'apiurl' => $opts{apiurl},
  126. 'user' => $opts{user},
  127. 'pass' => $opts{password},
  128. 'run' => $opts{run},
  129. 'project' => $opts{project},
  130. 'step_results' => $opts{step_results},
  131. 'debug' => $opts{debug},
  132. 'browser' => $opts{browser},
  133. 'plan' => $opts{plan},
  134. 'configs' => $opts{configs},
  135. 'result_options' => $opts{result_options},
  136. 'testsuite_id' => $opts{testsuite_id},
  137. 'testsuite' => $opts{testsuite},
  138. 'sections' => $opts{sections},
  139. 'autoclose' => $opts{autoclose},
  140. 'encoding' => $opts{encoding},
  141. 'max_tries' => $opts{max_tries},
  142. 'merge' => 1
  143. });
  144. $tap->run();
  145. }
  146. #all done
  147. return ("Done.\n",0);
  148. }
  149. 1;
  150. __END__
  151. =head1 SEE ALSO
  152. L<TestRail::API>
  153. L<App::Prove::Plugin::TestRail>
  154. L<TAP::Parser>
  155. L<File::HomeDir> for the finding of .testrailrc
  156. =head1 SPECIAL THANKS
  157. Thanks to cPanel Inc, for graciously funding the creation of this distribution.