Эх сурвалжийг харах

Add unit tests for TestRail::Utils::getRunInformation

George S. Baugh 10 жил өмнө
parent
commit
cc35b77229

+ 3 - 3
lib/Test/LWP/UserAgent/TestRailMock.pm

@@ -606,7 +606,7 @@ $VAR4 = bless( {
                }, 'HTTP::Headers' );
 $VAR5 = '[
     {"id":1,"suite_id":9,"name":"TestingSuite","description":"ACQUIRE CLOTHES, BOOTS AND MOTORCYCLE","milestone_id":null,"assignedto_id":null,"include_all":true,"is_completed":false,"completed_on":null,"config":null,"config_ids":[],"passed_count":0,"blocked_count":0,"untested_count":1,"retest_count":0,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":9,"plan_id":null,"created_on":1419364929,"created_by":1,"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/runs\\/view\\/22"},
-    {"id":2,"suite_id":9,"name":"OtherOtherSuite","description":"bah","completed_on":null},
+    {"id":2,"suite_id":9,"name":"OtherOtherSuite","description":"bah","completed_on":null,"milestone_id":8},
     {"id":3,"suite_id":9,"name":"FinalRun","description":"Tests finality","milestone_id":null,"assignedto_id":null,"include_all":true,"is_completed":false,"completed_on":null,"config":null,"config_ids":[],"passed_count":0,"blocked_count":0,"untested_count":0,"retest_count":0,"failed_count":0,"custom_status1_count":1,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":9,"plan_id":null,"created_on":1419364929,"created_by":1,"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/runs\\/view\\/22"},{"id":1099,"suite_id":5,"name":"lockRun","description":"Locky tests","milestone_id":null,"assignedto_id":null,"include_all":true,"is_completed":false,"completed_on":null,"config":null,"config_ids":[],"passed_count":0,"blocked_count":0,"untested_count":0,"retest_count":5,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":1,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":2,"plan_id":null,"created_on":1437073290,"created_by":1,"url":"http:\\/\\/testrail.local\\/index.php?\\/runs\\/view\\/1099"}
 ]';
 $mockObject->map_response(qr/\Q$VAR1\E/,HTTP::Response->new($VAR2, $VAR3, $VAR4, $VAR5));
@@ -908,7 +908,7 @@ $VAR4 = bless( {
                  'server' => 'Apache/2.4.7 (Ubuntu)'
                }, 'HTTP::Headers' );
 $VAR5 = '[{"id":23,"name":"GosPlan","description":"Soviet 5-year agriculture plan to liquidate Kulaks","milestone_id":8,"assignedto_id":null,"is_completed":false,"completed_on":null,"passed_count":0,"blocked_count":0,"untested_count":1,"retest_count":0,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":9,"created_on":1419364930,"created_by":1,"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/plans\\/view\\/23"},
-{"id":24,"name":"mah dubz plan","description":"bogozone","milestone_id":8,"assignedto_id":null,"is_completed":false,"completed_on":null,"passed_count":0,"blocked_count":0,"untested_count":1,"retest_count":0,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":9,"created_on":1419364930,"created_by":1,"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/plans\\/view\\/24"},
+{"id":24,"name":"mah dubz plan","description":"bogozone","milestone_id":null,"assignedto_id":null,"is_completed":false,"completed_on":null,"passed_count":0,"blocked_count":0,"untested_count":1,"retest_count":0,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":9,"created_on":1419364930,"created_by":1,"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/plans\\/view\\/24"},
 {"id":1094,"name":"HooHaaPlan","description":"zippy","milestone_id":null,"assignedto_id":null,"is_completed":false,"completed_on":null,"passed_count":0,"blocked_count":0,"untested_count":4,"retest_count":0,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":2,"created_on":1429586939,"created_by":1,"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/plans\\/view\\/1094"},
 {"id":1096,"name":"FinalPlan","description":"zippy","milestone_id":null,"assignedto_id":null,"is_completed":false,"completed_on":null,"passed_count":4,"blocked_count":0,"untested_count":0,"retest_count":0,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":2,"created_on":1429586939,"created_by":1,"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/plans\\/view\\/1096"}]';
 $mockObject->map_response(qr/\Q$VAR1\E/,HTTP::Response->new($VAR2, $VAR3, $VAR4, $VAR5));
@@ -1045,7 +1045,7 @@ $VAR4 = bless( {
                  'content-type' => 'application/json; charset=utf-8',
                  'server' => 'Apache/2.4.7 (Ubuntu)'
                }, 'HTTP::Headers' );
-$VAR5 = '{"id":24,"name":"mah dubz plan","description":"bogoplan","milestone_id":8,"assignedto_id":null,"is_completed":false,"completed_on":null,"passed_count":0,"blocked_count":0,"untested_count":1,"retest_count":0,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":10,"created_on":1419364930,"created_by":1,"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/plans\\/view\\/24","entries":[{"id":"271443a5-aacf-467e-8993-b4f7001195cf","suite_id":9,"name":"Executing the great plan","runs":[{"id":1,"suite_id":9,"name":"TestingSuite","description":null,"milestone_id":8,"assignedto_id":null,"include_all":true,"is_completed":false,"completed_on":null,"passed_count":0,"blocked_count":0,"untested_count":1,"retest_count":0,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":9,"plan_id":23,"entry_index":1,"entry_id":"271443a5-aacf-467e-8993-b4f7001195cf","config":"testConfig","config_ids":[4],"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/runs\\/view\\/24"}]}]}';
+$VAR5 = '{"id":24,"name":"mah dubz plan","description":"bogoplan","milestone_id":null,"assignedto_id":null,"is_completed":false,"completed_on":null,"passed_count":0,"blocked_count":0,"untested_count":1,"retest_count":0,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":10,"created_on":1419364930,"created_by":1,"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/plans\\/view\\/24","entries":[{"id":"271443a5-aacf-467e-8993-b4f7001195cf","suite_id":9,"name":"Executing the great plan","runs":[{"id":1,"suite_id":9,"name":"TestingSuite","description":null,"milestone_id":8,"assignedto_id":null,"include_all":true,"is_completed":false,"completed_on":null,"passed_count":0,"blocked_count":0,"untested_count":1,"retest_count":0,"failed_count":0,"custom_status1_count":0,"custom_status2_count":0,"custom_status3_count":0,"custom_status4_count":0,"custom_status5_count":0,"custom_status6_count":0,"custom_status7_count":0,"project_id":9,"plan_id":23,"entry_index":1,"entry_id":"271443a5-aacf-467e-8993-b4f7001195cf","config":"testConfig","config_ids":[4],"url":"http:\\/\\/testrail.local\\/\\/index.php?\\/runs\\/view\\/24"}]}]}';
 $mockObject->map_response(qr/\Q$VAR1\E/,HTTP::Response->new($VAR2, $VAR3, $VAR4, $VAR5));
 
 }

+ 11 - 3
lib/TestRail/Utils.pm

@@ -133,10 +133,9 @@ sub getFilenameFromTapLine {
     return 0;
 }
 
-
 =head2 getRunInformation
 
-Return the relevant project definition, plan and run definition HASHREFs for the provided options.
+Return the relevant project definition, plan, run and milestone definition HASHREFs for the provided options.
 
 Dies in the event the project/plan/run could not be found.
 
@@ -160,7 +159,16 @@ sub getRunInformation {
     }
 
     confess "No such run '$opts->{run}' matching the provided configs (if any).\n" if !$run;
-    return ($project,$plan,$run);
+
+    #If the run/plan has a milestone set, then return it too
+    my $milestone;
+    my $mid = $plan ? $plan->{'milestone_id'} : $run->{'milestone_id'};
+    if ($mid) {
+        $milestone = $tr->getMilestoneByID($mid);
+        confess "Could not fetch run milestone!" unless $milestone; #hope this doesn't happen
+    }
+
+    return ($project, $plan, $run, $milestone);
 }
 
 =head2 findTests(opts,case1,...,caseN)

+ 6 - 0
lib/TestRail/Utils/Lock.pm

@@ -169,3 +169,9 @@ sub lockTest {
 }
 
 1;
+
+__END__
+
+=head1 SPECIAL THANKS
+
+Thanks to cPanel Inc, for graciously funding the creation of this module.

+ 64 - 1
t/TestRail-Utils.t

@@ -1,9 +1,13 @@
 use strict;
 use warnings;
 
-use Test::More 'tests' => 10;
+use Test::More 'tests' => 23;
 use Test::Fatal;
+
+use TestRail::API;
 use TestRail::Utils;
+use Test::LWP::UserAgent::TestRailMock;
+
 use File::Basename qw{dirname};
 
 my ($apiurl,$user,$password);
@@ -50,5 +54,64 @@ close($fh);
 push(@files,$fcontents);
 is(scalar(@files),7,"Detects # of filenames correctly in TAP");
 
+#Test getRunInformation
+my ($apiurl,$login,$pw) = ('http://testrail.local','teodesian@cpan.org','fake');
+my $debug = 1;
+my $browser = $Test::LWP::UserAgent::TestRailMock::mockObject;
+
+my $tr = TestRail::API->new($apiurl,$login,$pw,undef,1);
+$tr->{'browser'} = $browser;
+$tr->{'debug'} = 0;
+
+#Plan mode, no milestone
+my $opts = {
+    'run'     => 'TestingSuite',
+    'plan'    => 'mah dubz plan',
+    'configs' => ['testConfig'],
+    'project' => 'TestProject'
+};
+my ($project, $plan, $run, $milestone) = TestRail::Utils::getRunInformation($tr,$opts);
+is($project->{'id'}, 10, "getRunInformation gets project correctly");
+is($plan->{'id'},    24, "getRunInformation gets plan correctly");
+is($run->{'id'},     1, "getRunInformation gets run correctly");
+is($milestone,   undef, "getRunInformation returns undef when no milestone set for plan");
+
+#Plan mode, no such run
+$opts->{'run'} = 'hoo hoo I do not exist';
+like(exception { TestRail::Utils::getRunInformation($tr,$opts) }, qr/no such run/i, "Attempt to find nonexistant run in plan is fatal");
+
+#Plan mode, no such plan
+$opts->{'plan'} = 'hoo hoo I do not exist';
+like(exception { TestRail::Utils::getRunInformation($tr,$opts) }, qr/no such plan/i, "Attempt to find nonexistant plan is fatal");
+
+#No such project
+$opts->{'project'} = 'hoo hoo I do not exist';
+like(exception { TestRail::Utils::getRunInformation($tr,$opts) }, qr/no such project/i, "Attempt to find nonexistant project is fatal");
+
+#Run mode, no milestone
+$opts->{'run'} = 'TestingSuite';
+$opts->{'configs'} = undef;
+$opts->{'plan'} = undef;
+$opts->{'project'} = 'TestProject';
+
+($project, $plan, $run, $milestone) = TestRail::Utils::getRunInformation($tr,$opts);
+is($project->{'id'}, 10, "getRunInformation gets project correctly [run mode]");
+is($plan->{'id'}, undef, "getRunInformation gets plan correctly [run mode]");
+is($run->{'id'},      1, "getRunInformation gets run correctly [run mode]");
+is($milestone,    undef, "getRunInformation returns undef when no milestone set for run");
+
+#Run mode, milestone
+$opts->{'run'} = 'OtherOtherSuite';
+($project, $plan, $run, $milestone) = TestRail::Utils::getRunInformation($tr,$opts);
+is($milestone->{'id'},8,"Milestone acquired correctly in run mode");
+
+#plan mode, milestone
+$opts->{'project'} = "TestProject";
+$opts->{'plan'} = 'GosPlan';
+$opts->{'run'} = "Executing the great plan";
+$opts->{'configs'} = ["testConfig"];
+
+($project, $plan, $run, $milestone) = TestRail::Utils::getRunInformation($tr,$opts);
+is($milestone->{'id'},8,"Milestone acquired correctly in plan mode");
 
 #Regrettably, I have yet to find a way to print to stdin without eval, so userInput will remain untested.