testrail-results.t 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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' => 24;
  9. use Capture::Tiny qw{capture_merged};
  10. use List::MoreUtils qw{uniq};
  11. no warnings qw{redefine once};
  12. *TestRail::API::getTests = sub {
  13. my ($self,$run_id) = @_;
  14. return [
  15. {
  16. 'id' => 666,
  17. 'title' => 'fake.test',
  18. 'run_id' => $run_id
  19. }
  20. ];
  21. };
  22. *TestRail::API::getTestResults = sub {
  23. return [
  24. {
  25. 'elapsed' => '1s',
  26. 'status_id' => 5
  27. },
  28. {
  29. 'elapsed' => '2s',
  30. 'status_id' => 4,
  31. 'comment' => 'zippy'
  32. }
  33. ];
  34. };
  35. *TestRail::API::getPlanByID = sub {
  36. return {
  37. 'id' => 40000,
  38. 'name' => 'mah dubz plan',
  39. 'entries' => [{
  40. 'runs' => [
  41. {
  42. 'name' => 'planrun',
  43. 'id' => '999',
  44. 'plan_id' => 40000
  45. }
  46. ]
  47. }]
  48. };
  49. };
  50. use warnings;
  51. #check doing things over all projects/plans/runs
  52. my @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake t/fake.test };
  53. my ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  54. is($code, 0, "Exit code OK looking for results of fake.test");
  55. like($out,qr/fake\.test was present in 515 runs/,"Gets correct # of runs with test inside it");
  56. #check project filters
  57. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --project TestProject t/fake.test };
  58. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  59. is($code, 0, "Exit code OK looking for results of fake.test");
  60. like($out,qr/fake\.test was present in 10 runs/,"Gets correct # of runs with test inside it when filtering by project name");
  61. #check plan filters
  62. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --plan };
  63. push(@args,'mah dubz plan', 't/fake.test');
  64. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  65. is($code, 0, "Exit code OK looking for results of fake.test");
  66. like($out,qr/fake\.test was present in 259 runs/,"Gets correct # of runs with test inside it when filtering by plan name");
  67. #check run filters
  68. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --run FinalRun t/fake.test};
  69. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  70. is($code, 0, "Exit code OK looking for results of fake.test");
  71. like($out,qr/fake\.test was present in 1 runs/,"Gets correct # of runs with test inside it when filtering by run name");
  72. #check pattern filters
  73. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --grep zippy t/fake.test};
  74. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  75. is($code, 0, "Exit code OK looking for results of fake.test");
  76. like($out,qr/Retest: 515/,"Gets correct # & status of runs with test inside it when grepping");
  77. unlike($out,qr/Failed: 515/,"Gets correct # & status of runs with test inside it when grepping");
  78. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --json t/fake.test };
  79. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  80. is($code, 0, "Exit code OK looking for results of fake.test in json mode");
  81. like($out,qr/num_runs/,"Gets # of runs with test inside it in json mode");
  82. #check defect filters
  83. {
  84. no warnings qw{redefine once};
  85. local *TestRail::API::getTestByName = sub { return { 'id' => '666', 'run_id' => 123, 'elapsed' => 1 } };
  86. local *TestRail::API::getTestResults = sub { return [{ 'defects' => ['YOLO-666'], 'status_id' => 2 }, { 'defects' => undef, 'status_id' => 1 }] };
  87. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --defect YOLO-666 t/skip.test };
  88. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  89. is($code, 0, "Exit code OK looking for defects of skip.test");
  90. like($out,qr/YOLO-666/,"Gets # of runs with defects inside it");
  91. }
  92. #For making the test data to test the caching
  93. #open(my $fh, '>', "t/data/faketest_cache.json");
  94. #print $fh $out;
  95. #close($fh);
  96. #Check caching
  97. @args = qw{--apiurl http://testrail.local --user test@fake.fake --password fake --json --cachefile t/data/faketest_cache.json t/fake.test };
  98. ($out,$code) = TestRail::Bin::Results::run('browser' => $Test::LWP::UserAgent::TestRailMock::mockObject, 'args' => \@args);
  99. is($code, 0, "Exit code OK looking for results of fake.test in json mode");
  100. chomp $out;
  101. is($out,"{}","Caching mode works");
  102. #Check time parser
  103. is(TestRail::Bin::Results::_elapsed2secs('1s'),1,"elapsed2secs works : seconds");
  104. is(TestRail::Bin::Results::_elapsed2secs('1m'),60,"elapsed2secs works : minutes");
  105. is(TestRail::Bin::Results::_elapsed2secs('1h'),3600,"elapsed2secs works : hours");
  106. is(TestRail::Bin::Results::_elapsed2secs('1s1m1h'),3661,"elapsed2secs works :smh");
  107. #Check help output
  108. @args = qw{--help};
  109. $0 = $FindBin::Bin.'/../bin/testrail-runs';
  110. ($out,(undef,$code)) = capture_merged {TestRail::Bin::Results::run('args' => \@args)};
  111. is($code, 0, "Exit code OK asking for help");
  112. like($out,qr/encoding of arguments/i,"Help output OK");
  113. #Make sure that the binary itself processes args correctly
  114. $out = `$^X $0 --help`;
  115. like($out,qr/encoding of arguments/i,"Appears we can run binary successfully");