testrail-results.t 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. use strict;
  2. use warnings;
  3. use FindBin;
  4. use lib $FindBin::Bin.'/../bin';
  5. require 'testrail-results';
  6. use lib $FindBin::Bin.'/lib';
  7. use Test::LWP::UserAgent::TestRailMock;
  8. use Test::More 'tests' => 33;
  9. use Capture::Tiny qw{capture_merged};
  10. use List::MoreUtils qw{uniq};
  11. use Test::Fatal;
  12. use File::Temp qw{tempdir};
  13. no warnings qw{redefine once};
  14. *TestRail::API::getTests = sub {
  15. my ($self,$run_id) = @_;
  16. return [
  17. {
  18. 'id' => 666,
  19. 'title' => 'fake.test',
  20. 'run_id' => $run_id
  21. }
  22. ];
  23. };
  24. *TestRail::API::getTestResults = sub {
  25. return [
  26. {
  27. 'elapsed' => '1s',
  28. 'status_id' => 5
  29. },
  30. {
  31. 'elapsed' => '2s',
  32. 'status_id' => 4,
  33. 'comment' => 'zippy'
  34. }
  35. ];
  36. };
  37. *TestRail::API::getPlanByID = sub {
  38. return {
  39. 'id' => 40000,
  40. 'name' => 'mah dubz plan',
  41. 'entries' => [{
  42. 'runs' => [
  43. {
  44. 'name' => 'planrun',
  45. 'id' => '999',
  46. 'plan_id' => 40000
  47. }
  48. ]
  49. }]
  50. };
  51. };
  52. use warnings;
  53. #check doing things over all projects/plans/runs
  54. my @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake t/fake.test };
  55. my ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  56. is($code, 0, "Exit code OK looking for results of fake.test");
  57. like($out,qr/fake\.test was present in 509 runs/,"Gets correct # of runs with test inside it");
  58. #check project filters
  59. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --project TestProject t/fake.test };
  60. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  61. is($code, 0, "Exit code OK looking for results of fake.test");
  62. like($out,qr/fake\.test was present in 10 runs/,"Gets correct # of runs with test inside it when filtering by project name");
  63. #check plan filters
  64. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --plan };
  65. push(@args,'mah dubz plan', 't/fake.test');
  66. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  67. is($code, 0, "Exit code OK looking for results of fake.test");
  68. like($out,qr/fake\.test was present in 253 runs/,"Gets correct # of runs with test inside it when filtering by plan name");
  69. #check run filters
  70. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --run FinalRun t/fake.test};
  71. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  72. is($code, 0, "Exit code OK looking for results of fake.test");
  73. like($out,qr/fake\.test was present in 1 runs/,"Gets correct # of runs with test inside it when filtering by run name");
  74. #check pattern filters
  75. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --grep zippy t/fake.test};
  76. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  77. is($code, 0, "Exit code OK looking for results of fake.test");
  78. like($out,qr/Retest: 509/,"Gets correct # & status of runs with test inside it when grepping");
  79. unlike($out,qr/Failed: 515/,"Gets correct # & status of runs with test inside it when grepping");
  80. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --json t/fake.test };
  81. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  82. is($code, 0, "Exit code OK looking for results of fake.test in json mode");
  83. like($out,qr/num_runs/,"Gets # of runs with test inside it in json mode");
  84. #For making the test data to test the caching
  85. #open(my $fh, '>', "t/data/faketest_cache.json");
  86. #print $fh $out;
  87. #close($fh);
  88. #Check caching
  89. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --json --cachefile t/data/faketest_cache.json t/fake.test };
  90. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  91. is($code, 0, "Exit code OK looking for results of fake.test in json mode");
  92. chomp $out;
  93. is($out,"{}","Caching mode works");
  94. #Check merged mode
  95. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --json --merged --cachefile t/data/faketest_cache.json t/fake.test };
  96. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  97. is($code, 0, "Exit code OK looking for results of fake.test in json mode");
  98. chomp $out;
  99. like($out,qr/fake.test/,"Caching mode includes prior data in output with --merged mode");
  100. #check pertest mode
  101. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --json --perfile bogus.dir t/fake.test };
  102. like( exception { TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args) }, qr/no such dir/i, "Bad pertest dir throws");
  103. my $dir = tempdir( CLEANUP => 1);
  104. @args = (qw{--apiurl http://testrail.local --user test@fake.fake --password fake --json --perfile}, $dir, 't/fake.test');
  105. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  106. is($code, 0, "Exit code OK looking for results of fake.test in json mode");
  107. chomp $out;
  108. ok(-f "$dir/fake.test.json","pertest write works");
  109. #check defect & version filters
  110. {
  111. no warnings qw{redefine once};
  112. local *TestRail::API::getTestByName = sub { return { 'id' => '666', 'run_id' => 123, 'elapsed' => 1 } };
  113. local *TestRail::API::getTestResults = sub { return [{ 'defects' => ['YOLO-666'], 'status_id' => 2, 'version' => 666 }, { 'defects' => undef, 'status_id' => 1, 'version' => 333 }] };
  114. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --defect YOLO-666 t/skip.test };
  115. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  116. is($code, 0, "Exit code OK looking for defects of skip.test");
  117. like($out,qr/YOLO-666/,"Gets # of runs with defects inside it");
  118. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --version 333 t/skip.test };
  119. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  120. is($code, 0, "Exit code OK looking for version 333 for skip.test");
  121. like($out,qr/333/,"Gets # of runs with version 333 inside it");
  122. }
  123. #check fast mode
  124. {
  125. no warnings qw{redefine once};
  126. local *TestRail::API::getTestByName = sub { return { 'id' => '666', 'run_id' => 123, 'elapsed' => 1 } };
  127. local *TestRail::API::getRunResults = sub {
  128. return [
  129. { 'test_id' => 666, 'status_id' => 2, 'defects' => ['YOLO-666'], version => 666},
  130. { 'test_id' => 666, 'defects' => undef, 'status_id' => 1, 'version' => 333}
  131. ];
  132. };
  133. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --defect YOLO-666 --fast t/skip.test };
  134. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  135. is($code, 0, "Exit code OK looking for defects of skip.test -- fastmode");
  136. like($out,qr/YOLO-666/,"Gets # of runs with defects inside it -- fastmode");
  137. }
  138. #Check time parser
  139. is(TestRail::Bin::Results::_elapsed2secs('1s'),1,"elapsed2secs works : seconds");
  140. is(TestRail::Bin::Results::_elapsed2secs('1m'),60,"elapsed2secs works : minutes");
  141. is(TestRail::Bin::Results::_elapsed2secs('1h'),3600,"elapsed2secs works : hours");
  142. is(TestRail::Bin::Results::_elapsed2secs('1s1m1h'),3661,"elapsed2secs works :smh");
  143. #Check help output
  144. @args = qw{--help};
  145. $0 = $FindBin::Bin.'/../bin/testrail-runs';
  146. ($out,(undef,$code)) = capture_merged {TestRail::Bin::Results::run('args' => \@args)};
  147. is($code, 0, "Exit code OK asking for help");
  148. like($out,qr/encoding of arguments/i,"Help output OK");
  149. #Make sure that the binary itself processes args correctly
  150. $out = `$^X $0 --help`;
  151. like($out,qr/encoding of arguments/i,"Appears we can run binary successfully");