Переглянути джерело

Fix #107: provide defect metrics in testrail-results & defect filtering

George S. Baugh 9 роки тому
батько
коміт
5cebd2278d
4 змінених файлів з 51 додано та 4 видалено
  1. 1 0
      Changes
  2. 7 0
      bin/testrail-results
  3. 31 3
      lib/TestRail/Utils/Find.pm
  4. 12 1
      t/testrail-results.t

+ 1 - 0
Changes

@@ -8,6 +8,7 @@ Revision history for Perl module TestRail::API
     - Fix issue where non-standard status overrides were not possible
     - Add finder callback to TestRail::Utils::FindTests
     - Add testsuite_id filter to TestRail::API::getChildRunByName
+    - Add defect metrics to testrail-results
 
 0.037 2016-08-10 TEODESIAN
     - Fix incorrect POD for TestRail::API::createRunInPlan

+ 7 - 0
bin/testrail-results

@@ -126,6 +126,7 @@ sub run {
         'r|run=s@'        => \$opts->{'runs'},
         'e|encoding=s'    => \$opts->{'encoding'},
         'g|grep=s'        => \$opts->{'pattern'},
+        'd|defect=s@'     => \$opts->{'defects'},
         'c|cachefile=s'   => \$opts->{'cachefile'},
         'json'            => \$opts->{'json'},
         'h|help'          => \$opts->{'help'},
@@ -173,6 +174,7 @@ sub run {
         $out .= "#############################\n";
         my $num_runs = 0;
         my $casetotals = {};
+        my $defects = [];
         my $total_elapsed = 0;
         my $avg_elapsed = 0;
         my $median_runtime = 0;
@@ -185,10 +187,12 @@ sub run {
             #$out .= "Found case '$case' in run $casedef->{run_id}\n";
             foreach my $result (@{$casedef->{results}}) {
                 $casetotals->{$result->{status_id}}++;
+                push(@$defects, @{$result->{defects}}) if $result->{defects};
                 push(@$elapsetotals,_elapsed2secs($result->{'elapsed'}));
             }
         }
         @$seen_runs = uniq(@$seen_runs);
+        @$defects   = uniq(@$defects);
 
         my $pattern_output = '';
         $out_json->{$case}->{search_string} = $opts->{'pattern'};
@@ -222,6 +226,9 @@ sub run {
             $out .= "$status_map{$status}: $casetotals->{$status}\n";
             $out_json->{$case}->{$status_map{$status}} = $casetotals->{$status};
         }
+
+        $out .= "\nDefects related to case:\n".join(',',@$defects)."\n" if @$defects;
+
     }
 
     if ($opts->{'json'}) {

+ 31 - 3
lib/TestRail/Utils/Find.pm

@@ -8,6 +8,7 @@ use warnings;
 
 use Carp qw{confess cluck};
 use Scalar::Util qw{blessed};
+use List::Util qw{any};
 use List::MoreUtils qw{uniq};
 
 use File::Find;
@@ -330,6 +331,22 @@ Probably should have called this findResults, but we all prefer to get results r
 
 Returns ARRAYREF of results, and an ARRAYREF of seen plan IDs
 
+Valid Options:
+
+=over 4
+
+=item B<plans> - ARRAYREF of plan names to check.
+
+=item B<plan_ids> - ARRAYREF of plan IDs to check.
+
+=item B<runs> - ARRAYREF of runs names to check.
+
+=item B<pattern> - Pattern to filter case results on.
+
+=item B<defects> - ARRAYREF of defects of which at least one must be present in a result.
+
+=back
+
 =cut
 
 sub getResults {
@@ -351,12 +368,12 @@ sub getResults {
 
         #Filter out plans which do not match our filters to prevent a call to getPlanByID
         if ($opts->{'plans'}) {
-            @$plans = grep { my $p = $_; grep { $p->{'name'} eq $_} @{$opts->{'plans'}} } @$plans;
+            @$plans = grep { my $p = $_; any { $p->{'name'} eq $_ } @{$opts->{'plans'}} } @$plans;
         }
 
         #Filter out prior plans
         if ($opts->{'plan_ids'}) {
-            @$plans = grep { my $p = $_; grep { $p->{'id'} eq $_} @{$opts->{'plan_ids'}} } @$plans;
+            @$plans = grep { my $p = $_; any { $p->{'id'} eq $_ } @{$opts->{'plan_ids'}} } @$plans;
         }
 
         $opts->{'runs'} //= [];
@@ -365,10 +382,10 @@ sub getResults {
             my $plan_runs = $tr->getChildRuns($plan);
             push(@$runs,@$plan_runs) if $plan_runs;
         }
-
         foreach my $run (@$runs) {
             next if scalar(@{$opts->{runs}}) && !( grep { $_ eq $run->{'name'} } @{$opts->{'runs'}} );
             next if grep { $run->{id} eq $_ } @$prior_runs;
+
             foreach my $case (@cases) {
                 my $c = $tr->getTestByName($run->{'id'},basename($case));
                 next unless ref $c eq 'HASH';
@@ -382,6 +399,17 @@ sub getResults {
                     @{$c->{results}} = grep { my $comment = $_->{comment} || ''; $comment =~ m/$pattern/i } @{$c->{results}};
                 }
 
+                #Filter by the provided case IDs, if any
+                if (ref($opts->{'defects'}) eq 'ARRAY' && scalar(@{$opts->{defects}})) {
+                    @{$c->{results}} = grep {
+                        my $defects = $_->{defects};
+                        any {
+                            my $df_case = $_;
+                            any { $df_case eq $_ } @{$opts->{defects}};
+                        } @$defects
+                    } @{$c->{results}};
+                }
+
                 push(@{$res->{$case}}, $c) if scalar(@{$c->{results}}); #Make sure they weren't filtered out
             }
         }

+ 12 - 1
t/testrail-results.t

@@ -9,7 +9,7 @@ require 'testrail-results';
 use lib $FindBin::Bin.'/lib';
 use Test::LWP::UserAgent::TestRailMock;
 
-use Test::More 'tests' => 22;
+use Test::More 'tests' => 24;
 use Capture::Tiny qw{capture_merged};
 use List::MoreUtils qw{uniq};
 
@@ -94,6 +94,17 @@ unlike($out,qr/Failed: 515/,"Gets correct # & status of runs with test inside it
 is($code, 0, "Exit code OK looking for results of fake.test in json mode");
 like($out,qr/num_runs/,"Gets # of runs with test inside it in json mode");
 
+#check defect filters
+{
+    no warnings qw{redefine once};
+    local *TestRail::API::getTestByName = sub { return { 'id' => '666', 'run_id' => 123, 'elapsed' => 1 } };
+    local *TestRail::API::getTestResults = sub { return [{ 'defects' => ['YOLO-666'], 'status_id' => 2  }, { 'defects' => undef, 'status_id' => 1 }] };
+    @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --defect YOLO-666 t/skip.test };
+    ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
+    is($code, 0, "Exit code OK looking for defects of skip.test");
+    like($out,qr/YOLO-666/,"Gets # of runs with defects inside it");
+}
+
 #For making the test data to test the caching
 #open(my $fh, '>', "t/data/faketest_cache.json");
 #print $fh $out;