Parser.pm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. # ABSTRACT: Upload your TAP results to TestRail
  2. # PODNAME: Test::Rail::Parser
  3. package Test::Rail::Parser;
  4. use strict;
  5. use warnings;
  6. use utf8;
  7. use parent qw/TAP::Parser/;
  8. use Carp qw{cluck confess};
  9. use POSIX qw{floor};
  10. use TestRail::API;
  11. use Scalar::Util qw{reftype};
  12. use File::Basename qw{basename};
  13. our $self;
  14. =head1 DESCRIPTION
  15. A TAP parser which will upload your test results to a TestRail install.
  16. Has several options as to how you might want to upload said results.
  17. Subclass of L<TAP::Parser>, see that for usage past the constructor.
  18. You should probably use L<App::Prove::Plugin::TestRail> or the bundled program testrail-report for day-to-day usage...
  19. unless you need to subclass this. In that case a couple of options have been exposed for your convenience.
  20. =cut
  21. =head1 CONSTRUCTOR
  22. =head2 B<new(OPTIONS)>
  23. Get the TAP Parser ready to talk to TestRail, and register a bunch of callbacks to upload test results.
  24. =over 4
  25. =item B<OPTIONS> - HASHREF -- Keys are as follows:
  26. =over 4
  27. =item B<apiurl> - STRING: Full URI to your TestRail installation.
  28. =item B<user> - STRING: Name of your TestRail user.
  29. =item B<pass> - STRING: Said user's password, or one of their valid API keys (TestRail 4.2 and above).
  30. =item B<debug> - BOOLEAN: Print a bunch of extra messages
  31. =item B<browser> - OBJECT: Something like an LWP::UserAgent. Useful for mocking with L<Test::LWP::UserAgent::TestRailMock>.
  32. =item B<run> - STRING (semi-optional): name of desired run. Required if run_id not passed.
  33. =item B<run_id> - INTEGER (semi-optional): ID of desired run. Required if run not passed.
  34. =item B<plan> - STRING (semi-optional): Name of test plan to use, if your run provided is a child of said plan. Only relevant when run_id not passed.
  35. =item B<configs> - ARRAYREF (optional): Configurations to filter runs in plan by. Runs can have the same name, yet with differing configurations in a plan; this handles that odd case.
  36. =item B<project> - STRING (optional): name of project containing your desired run. Required if project_id not passed.
  37. =item B<project_id> - INTEGER (optional): ID of project containing your desired run. Required if project not passed.
  38. =item B<step_results> - STRING (optional): 'internal name' of the 'step_results' type field available for your project. Mutually exclusive with case_per_ok
  39. =item B<case_per_ok> - BOOLEAN (optional): Consider test files to correspond to section names, and test steps (OKs) to correspond to tests in TestRail. Mutually exclusive with step_results.
  40. =item B<result_options> - HASHREF (optional): Extra options to set with your result. See L<TestRail::API>'s createTestResults function for more information.
  41. =item B<custom_options> - HASHREF (optional): Custom options to set with your result. See L<TestRail::API>'s createTestResults function for more information. step_results will be set here, if the option is passed.
  42. =item B<spawn> - INTEGER (optional): Attempt to create a run based on the provided testsuite identified by the ID passed here. If plan/configs is passed, create it as a child of said plan with the listed configs. If the run exists, use it and disregard the provided testsuite ID. If the plan does not exist, create it too.
  43. =item B<sections> - ARRAYREF (optional): Restrict a spawned run to cases in these particular sections.
  44. =back
  45. =back
  46. It is worth noting that if neither step_results or case_per_ok is passed, that the test will be passed if it has no problems of any sort, failed otherwise.
  47. In both this mode and step_results, the file name of the test is expected to correspond to the test name in TestRail.
  48. This module also attempts to calculate the elapsed time to run each test if it is run by a prove plugin rather than on raw TAP.
  49. The constructor will terminate if the statuses 'pass', 'fail', 'retest', 'skip', 'todo_pass', and 'todo_fail' are not registered as result internal names in your TestRail install.
  50. =cut
  51. sub new {
  52. my ($class,$opts) = @_;
  53. our $self;
  54. #Load our callbacks
  55. $opts->{'callbacks'} = {
  56. 'test' => \&testCallback,
  57. 'comment' => \&commentCallback,
  58. 'unknown' => \&unknownCallback,
  59. 'EOF' => \&EOFCallback
  60. };
  61. my $tropts = {
  62. 'apiurl' => delete $opts->{'apiurl'},
  63. 'user' => delete $opts->{'user'},
  64. 'pass' => delete $opts->{'pass'},
  65. 'debug' => delete $opts->{'debug'},
  66. 'browser' => delete $opts->{'browser'},
  67. 'run' => delete $opts->{'run'},
  68. 'run_id' => delete $opts->{'run_id'},
  69. 'project' => delete $opts->{'project'},
  70. 'project_id' => delete $opts->{'project_id'},
  71. 'step_results' => delete $opts->{'step_results'},
  72. 'case_per_ok' => delete $opts->{'case_per_ok'},
  73. 'plan' => delete $opts->{'plan'},
  74. 'configs' => delete $opts->{'configs'} // [],
  75. 'spawn' => delete $opts->{'spawn'},
  76. 'sections' => delete $opts->{'sections'},
  77. #Stubs for extension by subclassers
  78. 'result_options' => delete $opts->{'result_options'},
  79. 'result_custom_options' => delete $opts->{'result_custom_options'}
  80. };
  81. confess("case_per_ok and step_results options are mutually exclusive") if ($tropts->{'case_per_ok'} && $tropts->{'step_results'});
  82. #Allow natural confessing from constructor
  83. my $tr = TestRail::API->new($tropts->{'apiurl'},$tropts->{'user'},$tropts->{'pass'},$tropts->{'debug'});
  84. $tropts->{'testrail'} = $tr;
  85. $tr->{'browser'} = $tropts->{'browser'} if defined($tropts->{'browser'}); #allow mocks
  86. $tr->{'debug'} = 0; #Always suppress in production
  87. #Get project ID from name, if not provided
  88. if (!defined($tropts->{'project_id'})) {
  89. my $pname = $tropts->{'project'};
  90. $tropts->{'project'} = $tr->getProjectByName($pname);
  91. confess("Could not list projects! Shutting down.") if ($tropts->{'project'} == -500);
  92. if (!$tropts->{'project'}) {
  93. confess("No project (or project_id) provided, or that which was provided was invalid!");
  94. }
  95. } else {
  96. $tropts->{'project'} = $tr->getProjectByID($tropts->{'project_id'});
  97. confess("No such project with ID $tropts->{project_id}!") if !$tropts->{'project'};
  98. }
  99. $tropts->{'project_id'} = $tropts->{'project'}->{'id'};
  100. #Discover possible test statuses
  101. $tropts->{'statuses'} = $tr->getPossibleTestStatuses();
  102. my @ok = grep {$_->{'name'} eq 'passed'} @{$tropts->{'statuses'}};
  103. my @not_ok = grep {$_->{'name'} eq 'failed'} @{$tropts->{'statuses'}};
  104. my @skip = grep {$_->{'name'} eq 'skip'} @{$tropts->{'statuses'}};
  105. my @todof = grep {$_->{'name'} eq 'todo_fail'} @{$tropts->{'statuses'}};
  106. my @todop = grep {$_->{'name'} eq 'todo_pass'} @{$tropts->{'statuses'}};
  107. my @retest = grep {$_->{'name'} eq 'retest'} @{$tropts->{'statuses'}};
  108. confess("No status with internal name 'passed' in TestRail!") unless scalar(@ok);
  109. confess("No status with internal name 'failed' in TestRail!") unless scalar(@not_ok);
  110. confess("No status with internal name 'skip' in TestRail!") unless scalar(@skip);
  111. confess("No status with internal name 'todo_fail' in TestRail!") unless scalar(@todof);
  112. confess("No status with internal name 'todo_pass' in TestRail!") unless scalar(@todop);
  113. confess("No status with internal name 'retest' in TestRail!") unless scalar(@retest);
  114. $tropts->{'ok'} = $ok[0];
  115. $tropts->{'not_ok'} = $not_ok[0];
  116. $tropts->{'skip'} = $skip[0];
  117. $tropts->{'todo_fail'} = $todof[0];
  118. $tropts->{'todo_pass'} = $todop[0];
  119. $tropts->{'retest'} = $retest[0];
  120. #Grab run
  121. my $run_id = $tropts->{'run_id'};
  122. my ($run,$plan,$config_ids);
  123. #check if configs passed are defined for project. If we can't get all the IDs, something's hinky
  124. $config_ids = $tr->translateConfigNamesToIds($tropts->{'project_id'},$tropts->{'configs'});
  125. confess("Could not retrieve list of valid configurations for your project.") unless (reftype($config_ids) || 'undef') eq 'ARRAY';
  126. my @bogus_configs = grep {!defined($_)} @$config_ids;
  127. my $num_bogus = scalar(@bogus_configs);
  128. confess("Detected $num_bogus bad config names passed. Check available configurations for your project.") if $num_bogus;
  129. if ($tropts->{'run'}) {
  130. if ($tropts->{'plan'}) {
  131. #Attempt to find run, filtered by configurations
  132. $plan = $tr->getPlanByName($tropts->{'project_id'},$tropts->{'plan'});
  133. if ($plan) {
  134. $tropts->{'plan'} = $plan;
  135. $run = $tr->getChildRunByName($plan,$tropts->{'run'},$tropts->{'configs'}); #Find plan filtered by configs
  136. if (defined($run) && (reftype($run) || 'undef') eq 'HASH') {
  137. $tropts->{'run'} = $run;
  138. $tropts->{'run_id'} = $run->{'id'};
  139. }
  140. } else {
  141. #Try to make it if spawn is passed
  142. $tropts->{'plan'} = $tr->createPlan($tropts->{'project_id'},$tropts->{'plan'},"Test plan created by TestRail::API");
  143. confess("Could not find plan ".$tropts->{'plan'}." in provided project, and spawning failed!") if !$tropts->{'plan'};
  144. }
  145. } else {
  146. $run = $tr->getRunByName($tropts->{'project_id'},$tropts->{'run'});
  147. if (defined($run) && (reftype($run) || 'undef') eq 'HASH') {
  148. $tropts->{'run'} = $run;
  149. $tropts->{'run_id'} = $run->{'id'};
  150. }
  151. }
  152. } else {
  153. $tropts->{'run'} = $tr->getRunByID($run_id);
  154. }
  155. #If spawn was passed and we don't have a Run ID yet, go ahead and make it
  156. if ($tropts->{'spawn'} && !$tropts->{'run_id'}) {
  157. print "# Spawning run\n";
  158. my $cases = [];
  159. if ($tropts->{'sections'}) {
  160. print "# with specified sections\n";
  161. #Then translate the sections into an array of case IDs.
  162. confess("Sections passed to spawn must be ARRAYREF") unless (reftype($tropts->{'sections'}) || 'undef') eq 'ARRAY';
  163. @{$tropts->{'sections'}} = $tr->sectionNamesToIds($tropts->{'project_id'},$tropts->{'spawn'},@{$tropts->{'sections'}});
  164. foreach my $section (@{$tropts->{'sections'}}) {
  165. my $cases = $tr->getCases($tropts->{'project_id'},$tropts->{'spawn'},$section);
  166. push(@$cases,@$cases) if (reftype($cases) || 'undef') eq 'ARRAY';
  167. }
  168. }
  169. if (scalar(@$cases)) {
  170. @$cases = map {$_->{'id'}} @$cases;
  171. } else {
  172. $cases = undef;
  173. }
  174. if ($tropts->{'plan'}) {
  175. print "# inside of plan\n";
  176. $plan = $tr->createRunInPlan( $tropts->{'plan'}->{'id'}, $tropts->{'spawn'}, $tropts->{'run'}, undef, $config_ids, $cases );
  177. $run = $plan->{'runs'}->[0] if exists($plan->{'runs'}) && (reftype($plan->{'runs'}) || 'undef') eq 'ARRAY' && scalar(@{$plan->{'runs'}});
  178. if (defined($run) && (reftype($run) || 'undef') eq 'HASH') {
  179. $tropts->{'run'} = $run;
  180. $tropts->{'run_id'} = $run->{'id'};
  181. }
  182. } else {
  183. $run = $tr->createRun( $tropts->{'project_id'}, $tropts->{'spawn'}, $tropts->{'run'}, "Automatically created Run from TestRail::API", undef, undef, $cases );
  184. if (defined($run) && (reftype($run) || 'undef') eq 'HASH') {
  185. $tropts->{'run'} = $run;
  186. $tropts->{'run_id'} = $run->{'id'};
  187. }
  188. }
  189. confess("Could not spawn run with requested parameters!") if !$tropts->{'run_id'};
  190. print "# Success!\n"
  191. }
  192. confess("No run ID provided, and no run with specified name exists in provided project/plan!") if !$tropts->{'run_id'};
  193. $self = $class->SUPER::new($opts);
  194. if (defined($self->{'_iterator'}->{'command'}) && reftype($self->{'_iterator'}->{'command'}) eq 'ARRAY' ) {
  195. $self->{'file'} = $self->{'_iterator'}->{'command'}->[-1];
  196. print "# PROCESSING RESULTS FROM TEST FILE: $self->{'file'}\n";
  197. $self->{'track_time'} = 1;
  198. } else {
  199. #Not running inside of prove in real-time, don't bother with tracking elapsed times.
  200. $self->{'track_time'} = 0;
  201. }
  202. #Make sure the step results field passed exists on the system
  203. $tropts->{'step_results'} = $tr->getTestResultFieldByName($tropts->{'step_results'},$tropts->{'project_id'}) if defined $tropts->{'step_results'};
  204. $self->{'tr_opts'} = $tropts;
  205. $self->{'errors'} = 0;
  206. #Start the shot clock
  207. $self->{'starttime'} = time();
  208. $self->{'raw_output'} = "";
  209. return $self;
  210. }
  211. =head1 PARSER CALLBACKS
  212. =head2 unknownCallback
  213. Called whenever we encounter an unknown line in TAP. Only useful for prove output, as we might pick a filename out of there.
  214. Stores said filename for future use if encountered.
  215. =cut
  216. # Look for file boundaries, etc.
  217. sub unknownCallback {
  218. my (@args) = @_;
  219. our $self;
  220. my $line = $args[0]->as_string;
  221. $self->{'raw_output'} .= "$line\n" if !$self->{'tr_opts'}->{'step_results'};
  222. #try to pick out the filename if we are running this on TAP in files
  223. #old prove
  224. if ($line =~ /^Running\s(.*)/) {
  225. #TODO figure out which testsuite this implies
  226. $self->{'file'} = $1;
  227. print "# PROCESSING RESULTS FROM TEST FILE: $self->{'file'}\n";
  228. }
  229. #RAW tap #XXX this regex could be improved
  230. if ($line =~ /(.*)\s\.\.\s*$/) {
  231. $self->{'file'} = $1 unless $line =~ /^[ok|not ok] - /; #a little more careful
  232. }
  233. print "# $line\n" if ($line =~ /^error/i);
  234. }
  235. =head2 commentCallback
  236. Grabs comments preceding a test so that we can include that as the test's notes.
  237. Especially useful when merge=1 is passed to the constructor.
  238. =cut
  239. # Register the current suite or test desc for use by test callback, if the line begins with the special magic words
  240. sub commentCallback {
  241. my (@args) = @_;
  242. our $self;
  243. my $line = $args[0]->as_string;
  244. $self->{'raw_output'} .= "$line\n" if !$self->{'tr_opts'}->{'step_results'};
  245. if ($line =~ m/^#TESTDESC:\s*/) {
  246. $self->{'tr_opts'}->{'test_desc'} = $line;
  247. $self->{'tr_opts'}->{'test_desc'} =~ s/^#TESTDESC:\s*//g;
  248. return;
  249. }
  250. #keep all comments before a test that aren't these special directives to save in NOTES field of reportTCResult
  251. $self->{'tr_opts'}->{'test_notes'} .= "$line\n";
  252. }
  253. =head2 testCallback
  254. If we are using step_results, append it to the step results array for use at EOF.
  255. If we are using case_per_ok, update TestRail per case.
  256. Otherwise, do nothing.
  257. =cut
  258. sub testCallback {
  259. my (@args) = @_;
  260. my $test = $args[0];
  261. our $self;
  262. if ( $self->{'track_time'} ) {
  263. #Test done. Record elapsed time.
  264. $self->{'tr_opts'}->{'result_options'}->{'elapsed'} = _compute_elapsed($self->{'starttime'},time());
  265. }
  266. #Don't do anything if we don't want to map TR case => ok or use step-by-step results
  267. if ( !($self->{'tr_opts'}->{'step_results'} || $self->{'tr_opts'}->{'case_per_ok'}) ) {
  268. print "# Neither step_results of case_per_ok set. No action to be taken, except on a whole test basis.\n" if $self->{'tr_opts'}->{'debug'};
  269. return 1;
  270. }
  271. if ($self->{'tr_opts'}->{'step_results'} && $self->{'tr_opts'}->{'case_per_ok'}) {
  272. cluck("ERROR: step_options and case_per_ok options are mutually exclusive!");
  273. $self->{'errors'}++;
  274. return 0;
  275. }
  276. #Fail on unplanned tests
  277. if ($test->is_unplanned()) {
  278. cluck("ERROR: Unplanned test detected. Will not attempt to upload results.");
  279. $self->{'errors'}++;
  280. return 0;
  281. }
  282. #Default assumption is that case name is step text (case_per_ok), unless...
  283. my $line = $test->as_string;
  284. $self->{'raw_output'} .= "$line\n" if !$self->{'tr_opts'}->{'step_results'};
  285. $line =~ s/^(ok|not ok)\s[0-9]*\s-\s//g;
  286. my $test_name = $line;
  287. my $run_id = $self->{'tr_opts'}->{'run_id'};
  288. print "# Assuming test name is '$test_name'...\n" if $self->{'tr_opts'}->{'debug'} && !$self->{'tr_opts'}->{'step_results'};
  289. my $todo_reason;
  290. #Setup args to pass to function
  291. my $status = $self->{'tr_opts'}->{'not_ok'}->{'id'};
  292. if ($test->is_actual_ok()) {
  293. $status = $self->{'tr_opts'}->{'ok'}->{'id'};
  294. if ($test->has_skip()) {
  295. $status = $self->{'tr_opts'}->{'skip'}->{'id'};
  296. $test_name =~ s/^(ok|not ok)\s[0-9]*\s//g;
  297. $test_name =~ s/^# skip //gi;
  298. }
  299. if ($test->has_todo()) {
  300. $status = $self->{'tr_opts'}->{'todo_pass'}->{'id'};
  301. $test_name =~ s/^(ok|not ok)\s[0-9]*\s//g;
  302. $test_name =~ s/(^# todo & skip )//gi; #handle todo_skip
  303. $test_name =~ s/ # todo\s(.*)$//gi;
  304. $todo_reason = $1;
  305. }
  306. } else {
  307. if ($test->has_todo()) {
  308. $status = $self->{'tr_opts'}->{'todo_pass'}->{'id'};
  309. $test_name =~ s/^(ok|not ok)\s[0-9]*\s//g;
  310. $test_name =~ s/^# todo & skip //gi; #handle todo_skip
  311. $test_name =~ s/# todo\s(.*)$//gi;
  312. $todo_reason = $1;
  313. }
  314. }
  315. #If this is a TODO, set the reason in the notes
  316. $self->{'tr_opts'}->{'test_notes'} .= "\nTODO reason: $todo_reason\n" if $todo_reason;
  317. #Setup step options and exit if that's the mode we be rollin'
  318. if ($self->{'tr_opts'}->{'step_results'}) {
  319. $self->{'tr_opts'}->{'result_custom_options'} = {} if !defined $self->{'tr_opts'}->{'result_custom_options'};
  320. $self->{'tr_opts'}->{'result_custom_options'}->{'step_results'} = [] if !defined $self->{'tr_opts'}->{'result_custom_options'}->{'step_results'};
  321. #XXX Obviously getting the 'expected' and 'actual' from the tap DIAGs would be ideal
  322. push(
  323. @{$self->{'tr_opts'}->{'result_custom_options'}->{'step_results'}},
  324. TestRail::API::buildStepResults($line,"Good result","Bad Result",$status)
  325. );
  326. print "# Appended step results.\n" if $self->{'tr_opts'}->{'debug'};
  327. return 1;
  328. }
  329. #Optional args
  330. my $notes = $self->{'tr_opts'}->{'test_notes'};
  331. my $options = $self->{'tr_opts'}->{'result_options'};
  332. my $custom_options = $self->{'tr_opts'}->{'result_custom_options'};
  333. _set_result($run_id,$test_name,$status,$notes,$options,$custom_options);
  334. #Re-start the shot clock
  335. $self->{'starttime'} = time();
  336. #Blank out test description in anticipation of next test
  337. # also blank out notes
  338. $self->{'tr_opts'}->{'test_notes'} = undef;
  339. $self->{'tr_opts'}->{'test_desc'} = undef;
  340. }
  341. =head2 EOFCallback
  342. If we are running in step_results mode, send over all the step results to TestRail.
  343. If we are running in case_per_ok mode, do nothing.
  344. Otherwise, upload the overall results of the test to TestRail.
  345. =cut
  346. sub EOFCallback {
  347. our $self;
  348. if ( $self->{'track_time'} ) {
  349. #Test done. Record elapsed time.
  350. $self->{'tr_opts'}->{'result_options'}->{'elapsed'} = _compute_elapsed($self->{'starttime'},time());
  351. }
  352. if ($self->{'tr_opts'}->{'case_per_ok'}) {
  353. print "# Nothing left to do.\n";
  354. undef $self->{'tr_opts'} unless $self->{'tr_opts'}->{'debug'};
  355. return 1;
  356. }
  357. #Fail if the file is not set
  358. if (!defined($self->{'file'})) {
  359. cluck("ERROR: Cannot detect filename, will not be able to find a Test Case with that name");
  360. $self->{'errors'}++;
  361. return 0;
  362. }
  363. my $run_id = $self->{'tr_opts'}->{'run_id'};
  364. my $test_name = basename($self->{'file'});
  365. my $status = $self->{'tr_opts'}->{'ok'}->{'id'};
  366. $status = $self->{'tr_opts'}->{'not_ok'}->{'id'} if $self->has_problems();
  367. $status = $self->{'tr_opts'}->{'retest'}->{'id'} if !$self->tests_run(); #No tests were run, env fail
  368. $status = $self->{'tr_opts'}->{'skip'}->{'id'} if $self->skip_all();
  369. #Optional args
  370. my $notes = $self->{'tr_opts'}->{'test_notes'};
  371. $notes = $self->{'raw_output'} if !$self->{'tr_opts'}->{'step_results'};
  372. my $options = $self->{'tr_opts'}->{'result_options'};
  373. my $custom_options = $self->{'tr_opts'}->{'result_custom_options'};
  374. print "# Setting results...\n";
  375. my $cres = _set_result($run_id,$test_name,$status,$notes,$options,$custom_options);
  376. $self->{'global_status'} = $status;
  377. undef $self->{'tr_opts'} unless $self->{'tr_opts'}->{'debug'};
  378. return $cres;
  379. }
  380. sub _set_result {
  381. my ($run_id,$test_name,$status,$notes,$options,$custom_options) = @_;
  382. our $self;
  383. my $tc;
  384. print "# Test elapsed: ".$options->{'elapsed'}."\n" if $options->{'elapsed'};
  385. print "# Attempting to find case by title '".$test_name." in run $run_id'...\n";
  386. $tc = $self->{'tr_opts'}->{'testrail'}->getTestByName($run_id,$test_name);
  387. if (!defined($tc) || (reftype($tc) || 'undef') ne 'HASH') {
  388. cluck("ERROR: Could not find test case: $tc");
  389. $self->{'errors'}++;
  390. return 0;
  391. }
  392. my $xid = $tc ? $tc->{'id'} : '???';
  393. my $cres;
  394. #Set test result
  395. if ($tc) {
  396. print "# Reporting result of case $xid in run $self->{'tr_opts'}->{'run_id'} as status '$status'...";
  397. # createTestResults(test_id,status_id,comment,options,custom_options)
  398. $cres = $self->{'tr_opts'}->{'testrail'}->createTestResults($tc->{'id'},$status, $notes, $options, $custom_options);
  399. print "# OK! (set to $status)\n" if (reftype($cres) || 'undef') eq 'HASH';
  400. }
  401. if (!$tc || ((reftype($cres) || 'undef') ne 'HASH') ) {
  402. print "# Failed!\n";
  403. print "# No Such test case in TestRail ($xid).\n";
  404. $self->{'errors'}++;
  405. }
  406. }
  407. #Compute the expected testrail date interval from 2 unix timestamps.
  408. sub _compute_elapsed {
  409. my ($begin,$end) = @_;
  410. my $secs_elapsed = $end - $begin;
  411. my $mins_elapsed = floor($secs_elapsed / 60);
  412. my $secs_remain = $secs_elapsed % 60;
  413. my $hours_elapsed = floor($mins_elapsed / 60);
  414. my $mins_remain = $mins_elapsed % 60;
  415. my $datestr = "";
  416. #You have bigger problems if your test takes days
  417. if ($hours_elapsed) {
  418. $datestr .= "$hours_elapsed"."h $mins_remain"."m";
  419. } else {
  420. $datestr .= "$mins_elapsed"."m";
  421. }
  422. if ($mins_elapsed) {
  423. $datestr .= " $secs_remain"."s";
  424. } else {
  425. $datestr .= " $secs_elapsed"."s";
  426. }
  427. undef $datestr if $datestr eq "0m 0s";
  428. return $datestr;
  429. }
  430. 1;
  431. __END__
  432. =head1 NOTES
  433. When using SKIP: {} (or TODO skip) blocks, you may want to consider naming your skip reasons the same as your test names when running in test_per_ok mode.
  434. =head1 SEE ALSO
  435. L<TestRail::API>
  436. L<TAP::Parser>
  437. =head1 SPECIAL THANKS
  438. Thanks to cPanel Inc, for graciously funding the creation of this module.