testrail-report 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. -e --encoding: Character encoding of arguments. Defaults to UTF-8.
  23. See L<Encode::Supported> for supported encodings.
  24. Test plans can have runs with the same name, but different configurations, which is understandably confusing.
  25. 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.
  26. So, try not to do that if you want to use this tool, and want sanity in your Test management system.
  27. The way around this is to specify what plan and configuration you want to set results for.
  28. This should provide sufficient uniqueness to get any run using names.
  29. =head3 OPTIONAL PARAMETERS
  30. --spawn [testsuite_id] : Attempt to create a run based on the provided testsuite ID. Uses the name provided with --run.
  31. If plans/configurations are supplied, it will attempt to create it as a child of the provided plan, and with the supplied configurations.
  32. If the specified run already exists, the program will simply use the existing run, and disregard the supplied testsuite_id.
  33. If the specified plan does not exist, it too will be created for you.
  34. --section [section_name] : When spawning, restrict the cases used in the provided testsuite ID to these sections.
  35. Option may be passed multiple times to specify multiple sections.
  36. =head3 CONFIG OPTIONS
  37. 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
  38. syntax separated by newlines. Valid Keys are: apiurl,user,password
  39. =head3 CONFIG OVERRIDES
  40. These override the config, if present. If neither are used, you will be prompted.
  41. --apiurl [url] : full URL to get to TestRail index document
  42. --password [key] : Your TestRail Password or a valid API key (TestRail 4.2 and above).
  43. --user [name] : Your TestRail User Name.
  44. =head3 BEHAVIOR
  45. --case-ok : Whether to consider each OK to correspond to a test in TestRail
  46. --step-results [name] : 'System Name' of a 'step_results' type field to set for your tests.
  47. These options are mutually exclusive. If neither is set, the
  48. overall result of the test will be used as the pass/fail for the test.
  49. =head3 RESULT OPTIONS
  50. --version : String describing the version of the system under test.
  51. --autoclose : If there are no more tests in 'untested' or 'retest' status for the specified run/plan, close it.
  52. =head2 PROVE PLUGIN:
  53. passing -PTestRail='key=value,...' to prove will
  54. automatically upload your test results while the test is running if
  55. real-time results are desired.
  56. See L<App::Prove::Plugin::TestRail> for more information.
  57. =head2 REQUIREMENTS:
  58. Your TestRail install must have 3 custom statuses with the internal
  59. names 'skip', 'todo_pass', and 'todo_fail', to represent those
  60. states which TAP can have.
  61. Also, be sure your tests don't output non-TAP (unknown) lines ending in dots (.)
  62. This will cause the preceding characters to be interpreted as a test name, which may lead to unexpected results.
  63. =cut
  64. use strict;
  65. use warnings;
  66. use TestRail::Utils;
  67. use Getopt::Long;
  68. use Test::Rail::Parser;
  69. use File::HomeDir qw{my_home};
  70. print "testrail-report\n----------------------\n";
  71. #Main loop------------
  72. my %opts;
  73. #parse switches
  74. GetOptions(
  75. 'run=s' => \$opts{run},
  76. 'apiurl=s' => \$opts{apiurl},
  77. 'password=s' => \$opts{password},
  78. 'user=s' => \$opts{user},
  79. 'project=s' => \$opts{project},
  80. 'case-ok' => \$opts{case_per_ok},
  81. 'step-results=s' => \$opts{step_results},
  82. 'config=s@' => \$opts{configs},
  83. 'plan=s' => \$opts{plan},
  84. 'version=s' => \$opts{version},
  85. 'spawn=i' => \$opts{spawn},
  86. 'section=s@' => \$opts{sections},
  87. 'autoclose' => \$opts{autoclose},
  88. 'e|encoding=s' => \$opts{encoding},
  89. 'help' => \$opts{help}
  90. );
  91. if ($opts{help}) { TestRail::Utils::help(); }
  92. #Parse config file if we are missing api url/key or user
  93. my $homedir = my_home() || '.';
  94. if (-e $homedir . '/.testrailrc' && (!$opts{apiurl} || !$opts{password} || !$opts{user}) ) {
  95. ($opts{apiurl},$opts{password},$opts{user}) = TestRail::Utils::parseConfig($homedir,1);
  96. }
  97. #If argument is passed use it instead of stdin
  98. my $file = $ARGV[0];
  99. die "No Such File $file" if ($file && !-e $file);
  100. die "ERROR: Interactive mode not allowed when piping input. See --help for options." if ( !$opts{run} || !$opts{apiurl} || !$opts{password} || !$opts{user} || !$opts{project} );
  101. my @files = TestRail::Utils::TAP2TestFiles($file);
  102. TestRail::Utils::interrogateUser(\%opts,qw{apiurl user password project run});
  103. $opts{result_options} = {'version' => $opts{version}} if $opts{version};
  104. my $tap;
  105. foreach my $phil (@files) {
  106. $tap = Test::Rail::Parser->new({
  107. 'tap' => $phil,
  108. 'apiurl' => $opts{apiurl},
  109. 'user' => $opts{user},
  110. 'pass' => $opts{password},
  111. 'run' => $opts{run},
  112. 'project' => $opts{project},
  113. 'case_per_ok' => $opts{case_per_ok},
  114. 'step_results' => $opts{step_results},
  115. 'debug' => $opts{debug},
  116. 'browser' => $opts{browser},
  117. 'plan' => $opts{plan},
  118. 'configs' => $opts{configs},
  119. 'result_options' => $opts{result_options},
  120. 'spawn' => $opts{spawn},
  121. 'sections' => $opts{sections},
  122. 'autoclose' => $opts{autoclose},
  123. 'encoding' => $opts{encoding},
  124. 'merge' => 1
  125. });
  126. $tap->run();
  127. }
  128. print "Done.\n";
  129. #all done
  130. 0;
  131. __END__
  132. =head1 SEE ALSO
  133. L<TestRail::API>
  134. L<App::Prove::Plugin::TestRail>
  135. L<TAP::Parser>
  136. L<File::HomeDir> for the finding of .testrailrc
  137. =head1 SPECIAL THANKS
  138. Thanks to cPanel Inc, for graciously funding the creation of this distribution.