TestRail.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # ABSTRACT: Upload your TAP results to TestRail in realtime
  2. # PODNAME: App::Prove::Plugin::TestRail
  3. package App::Prove::Plugin::TestRail;
  4. use strict;
  5. use warnings;
  6. use utf8;
  7. =head1 SYNOPSIS
  8. `prove -PTestRail='apiurl=http://some.testlink.install/,user=someUser,password=somePassword,project=TestProject,run=TestRun,plan=TestPlan,configs=Config1:Config2:Config3,version=0.014' sometest.t`
  9. =cut
  10. =head1 DESCRIPTION
  11. Prove plugin to upload test results to TestRail installations.
  12. Accepts input in the standard Prove plugin fashion (-Ppluginname='key=value,key=value,key=value...'), but will also parse a config file.
  13. When fed in prove plugin style, key=value input is expected.
  14. If ~/.testrailrc exists, it will be parsed for any of these values in a newline separated key=value list. Example:
  15. apiurl=http://some.testrail.install
  16. user=someGuy
  17. password=superS3cret
  18. project=TestProject
  19. run=TestRun
  20. plan=GosPlan
  21. configs=config1:config2:config3: ... :configN
  22. version=xx.xx.xx.xx
  23. case_per_ok=0
  24. step_results=sr_sys_name
  25. Note that passing configurations as filters for runs inside of plans are separated by colons.
  26. Values passed in via query string will override values in ~/.testrailrc.
  27. =head1 OVERRIDDEN METHODS
  28. =head2 load(parser)
  29. Shoves the arguments passed to the prove plugin into $ENV so that Test::Rail::Parser can get at them.
  30. Not the most elegant solution, but I see no other clear path to get those variables downrange to it's constructor.
  31. =cut
  32. sub load {
  33. my ($class, $p) = @_;
  34. my ($apiurl,$password,$user,$project,$run,$case_per_ok,$step_results) = _parseConfig();
  35. my $app = $p->{app_prove};
  36. my $args = $p->{'args'};
  37. my $params = {};
  38. my @kvp = ();
  39. my ($key,$value);
  40. foreach my $arg (@$args) {
  41. @kvp = split(/=/,$arg);
  42. if (scalar(@kvp) < 2) {
  43. print "Unrecognized Argument '$arg' to App::Prove::Plugin::Testrail, ignoring\n";
  44. next;
  45. }
  46. $key = shift @kvp;
  47. $value = join('',@kvp);
  48. $params->{$key} = $value;
  49. }
  50. $app->harness('Test::Rail::Harness');
  51. $app->merge(1);
  52. #XXX I can't figure out for the life of me any other way to pass this data. #YOLO
  53. $ENV{'TESTRAIL_APIURL'} = $params->{apiurl};
  54. $ENV{'TESTRAIL_USER'} = $params->{user};
  55. $ENV{'TESTRAIL_PASS'} = $params->{password};
  56. $ENV{'TESTRAIL_PROJ'} = $params->{project};
  57. $ENV{'TESTRAIL_RUN'} = $params->{run};
  58. $ENV{'TESTRAIL_PLAN'} = $params->{plan};
  59. $ENV{'TESTRAIL_CONFIGS'} = $params->{configs};
  60. $ENV{'TESTRAIL_VERSION'} = $params->{version};
  61. $ENV{'TESTRAIL_CASEOK'} = $params->{case_per_ok};
  62. $ENV{'TESTRAIL_STEPS'} = $params->{step_results};
  63. return $class;
  64. }
  65. sub _parseConfig {
  66. my $results = {};
  67. my $arr =[];
  68. open(my $fh, '<', $ENV{"HOME"} . '/.testrailrc') or return (undef,undef,undef);#couldn't open!
  69. while (<$fh>) {
  70. chomp;
  71. @$arr = split(/=/,$_);
  72. if (scalar(@$arr) != 2) {
  73. warn("Could not parse $_ in tlreport config\n");
  74. next;
  75. }
  76. $results->{lc($arr->[0])} = $arr->[1];
  77. }
  78. close($fh);
  79. return ($results->{'apiurl'},$results->{'password'},$results->{'user'},$results->{'project'},$results->{'run'},$results->{'case_per_ok'},$results->{'step_results'});
  80. }
  81. 1;
  82. __END__
  83. =head1 SEE ALSO
  84. L<TestRail::API>
  85. L<Test::Rail::Parser>
  86. L<App::Prove>
  87. =head1 SPECIAL THANKS
  88. Thanks to cPanel Inc, for graciously funding the creation of this module.