testrail-tests 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/perl
  2. # ABSTRACT: List tests in a TestRail run matching the provided filters
  3. # PODNAME: TestRail::Bin::Tests
  4. =head1 SYNOPSIS
  5. testrail-tests [OPTIONS] | xargs prove -PTestrail=...
  6. require `which testrail-tests`;
  7. TestRail::Bin::Test::run('args' => \@args);
  8. =head1 DESCRIPTION
  9. testrail-tests - list tests in a run matching the provided filters.
  10. Can be used as the modulino TestRail::Bin::Tests.
  11. Has a single 'run' function which accepts a hash with the 'args' parameter being the array of arguments.
  12. =head1 PARAMETERS:
  13. =head2 MANDATORY PARAMETERS
  14. =over 4
  15. --apiurl : full URL to get to TestRail index document
  16. --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
  17. --user : Your TestRail User Name.
  18. -j --project : desired project name.
  19. -r --run : desired run name.
  20. =back
  21. All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
  22. =head2 SEMI-OPTIONAL PARAMETERS
  23. =over 4
  24. -p --plan : desired plan name. Required if the run passed is a child of a plan.
  25. -m --match : attempt to find filenames matching the test names in the provided directory.
  26. --no-match : attempt to find filenames that do not match test names in the provided directory.
  27. --orphans : attempt to find tests in TestRail which aren't in the provided directory.
  28. The three above options are mutually exclusive.
  29. -n --no-recurse : if match (or no-match) passed, do not recurse subdirectories.
  30. -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L<Encode::Supported> for supported encodings.
  31. =back
  32. =head2 OPTIONAL PARAMETERS
  33. =over 4
  34. -c --config : configuration name to filter plans in run. Can be passed multiple times.
  35. -s --status : only list tests marked as [status] in testrail. Can be passed multiple times.
  36. -a --assignedto : only list tests assigned to user. Can be passed multiple times.
  37. --extension : only list files ending in the provided string (e.g. .pl, .pm, .t, .test)
  38. =back
  39. =head1 CONFIGURATION FILE
  40. In your \$HOME, (or the current directory, if your system has no concept of a home directory) put a file called .testrailrc with key=value syntax separated by newlines.
  41. Valid Keys are the same as documented by L<App::Prove::Plugin::TestRail>.
  42. All options specified thereby are overridden by passing the command-line switches above.
  43. =head1 MISCELLANEOUS OPTIONS:
  44. =over 4
  45. --help : show this output
  46. =back
  47. =cut
  48. package TestRail::Bin::Tests;
  49. use strict;
  50. use warnings;
  51. use utf8;
  52. use TestRail::API;
  53. use TestRail::Utils;
  54. use TestRail::Utils::Find;
  55. use Getopt::Long qw{GetOptionsFromArray};
  56. use File::HomeDir qw{my_home};
  57. if (!caller()) {
  58. my ($out,$code) = run('args' => \@ARGV);
  59. print $out;
  60. exit $code;
  61. }
  62. sub run {
  63. my %params = @_;
  64. my $opts ={};
  65. #Parse config file if we are missing api url/key or user
  66. my $homedir = my_home() || '.';
  67. if (-e $homedir . '/.testrailrc') {
  68. $opts = TestRail::Utils::parseConfig($homedir);
  69. }
  70. GetOptionsFromArray($params{'args'},
  71. 'apiurl=s' => \$opts->{'apiurl'},
  72. 'password=s' => \$opts->{'password'},
  73. 'user=s' => \$opts->{'user'},
  74. 'j|project=s' => \$opts->{'project'},
  75. 'p|plan=s' => \$opts->{'plan'},
  76. 'r|run=s' => \$opts->{'run'},
  77. 'c|config=s@' => \$opts->{'configs'},
  78. 's|status=s@' => \$opts->{'statuses'},
  79. 'a|assignedto=s@' => \$opts->{'users'},
  80. 'm|match=s' => \$opts->{'match'},
  81. 'no-match=s' => \$opts->{'no-match'},
  82. 'orphans=s' => \$opts->{'orphans'},
  83. 'n|no-recurse' => \$opts->{'no-recurse'},
  84. 'e|encoding=s' => \$opts->{'encoding'},
  85. 'extension=s' => \$opts->{'extension'},
  86. 'h|help' => \$opts->{'help'},
  87. );
  88. if ($opts->{help}) { return ('',TestRail::Utils::help()); }
  89. $opts->{'browser'} = $params{'browser'};
  90. TestRail::Utils::interrogateUser($opts,qw{apiurl user password project run});
  91. my $tr = TestRail::Utils::getHandle($opts);
  92. my ($cases) = TestRail::Utils::Find::getTests($opts,$tr);
  93. die "No cases in TestRail!\n" unless $cases;
  94. $opts->{'names-only'} = 1;
  95. my @tests = TestRail::Utils::Find::findTests($opts,@$cases);
  96. return (join("\n",@tests)."\n", 0) if scalar(@tests);
  97. return ('',255);
  98. }
  99. 1;
  100. __END__
  101. L<TestRail::API>
  102. L<File::HomeDir> for the finding of .testrailrc
  103. =head1 SPECIAL THANKS
  104. Thanks to cPanel Inc, for graciously funding the creation of this distribution.