API.pm 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. # ABSTRACT: Provides an interface to TestRail's REST api via HTTP
  2. # PODNAME: TestRail::API
  3. package TestRail::API;
  4. =head1 SYNOPSIS
  5. use TestRail::API;
  6. my ($username,$password,$host) = ('foo','bar','testlink.baz.foo');
  7. my $tr = TestRail::API->new($username, $password, $host);
  8. =head1 DESCRIPTION
  9. C<TestRail::API> provides methods to access an existing TestRail account using API v2. You can then do things like look up tests, set statuses and create runs from lists of cases.
  10. It is by no means exhaustively implementing every TestRail API function.
  11. =head1 IMPORTANT
  12. All the methods aside from the constructor should not die, but return a false value upon failure.
  13. When the server is not responsive, expect a -500 response, and retry accordingly.
  14. I recommend using the excellent L<Attempt> module for this purpose.
  15. =cut
  16. use 5.010;
  17. use strict;
  18. use warnings;
  19. use Carp qw{cluck confess};
  20. use Scalar::Util qw{reftype looks_like_number};
  21. use Clone 'clone';
  22. use Try::Tiny;
  23. use JSON::XS;
  24. use HTTP::Request;
  25. use LWP::UserAgent;
  26. use Types::Serialiser; #Not necesarily shared by JSON::XS on all platforms
  27. use Data::Validate::URI qw{is_uri};
  28. =head1 CONSTRUCTOR
  29. =head2 B<new (api_url, user, password)>
  30. Creates new C<TestRail::API> object.
  31. =over 4
  32. =item STRING C<API URL> - base url for your TestRail api server.
  33. =item STRING C<USER> - Your TestRail User.
  34. =item STRING C<PASSWORD> - Your TestRail password.
  35. =item BOOLEAN C<DEBUG> - Print the JSON responses from TL with your requests.
  36. =back
  37. Returns C<TestRail::API> object if login is successful.
  38. my $tr = TestRail::API->new('http://tr.test/testrail', 'moo','M000000!');
  39. Dies on all communication errors with the TestRail server.
  40. Does not do above checks if debug is passed.
  41. =cut
  42. sub new {
  43. my ($class,$apiurl,$user,$pass,$debug) = @_;
  44. confess("Constructor must be called statically, not by an instance") if ref($class);
  45. confess("Invalid URI passed to constructor") if !is_uri($apiurl);
  46. $user //= $ENV{'TESTRAIL_USER'};
  47. $pass //= $ENV{'TESTRAIL_PASSWORD'};
  48. $debug //= 0;
  49. my $self = {
  50. user => $user,
  51. pass => $pass,
  52. apiurl => $apiurl,
  53. debug => $debug,
  54. testtree => [],
  55. flattree => [],
  56. user_cache => [],
  57. type_cache => [],
  58. default_request => undef,
  59. browser => new LWP::UserAgent()
  60. };
  61. #Create default request to pass on to LWP::UserAgent
  62. $self->{'default_request'} = new HTTP::Request();
  63. $self->{'default_request'}->authorization_basic($user,$pass);
  64. bless( $self, $class );
  65. return $self if $self->debug; #For easy class testing without mocks
  66. #Manually do the get_users call to check HTTP status
  67. my $res = $self->_doRequest('index.php?/api/v2/get_users');
  68. confess "Error: network unreachable" if !defined($res);
  69. if ( (reftype($res) || 'undef') ne 'ARRAY') {
  70. confess "Unexpected return from _doRequest: $res" if !looks_like_number($res);
  71. confess "Could not communicate with TestRail Server! Check that your URI is correct, and your TestRail installation is functioning correctly." if $res == -500;
  72. confess "Could not list testRail users! Check that your TestRail installation has it's API enabled, and your credentials are correct" if $res == -403;
  73. confess "Bad user credentials!" if $res == -401;
  74. confess "HTTP error $res encountered while communicating with TestRail server. Resolve issue and try again." if !$res;
  75. confess "Unknown error occurred: $res";
  76. }
  77. confess "No users detected on TestRail Install! Check that your API is functioning correctly." if !scalar(@$res);
  78. $self->{'user_cache'} = $res;
  79. #Check that the User is actually in the list
  80. my $usr = $self->getUserByEmail($user);
  81. confess "Could not find your TestRail user on the system!" if !(reftype($usr) eq 'HASH');
  82. return $self;
  83. }
  84. =head1 GETTERS
  85. =head2 B<apiurl>
  86. =head2 B<debug>
  87. Accessors for these parameters you pass into the constructor, in case you forget.
  88. =cut
  89. sub apiurl {
  90. my $self = shift;
  91. confess("Object methods must be called by an instance") unless ref($self);
  92. return $self->{'apiurl'}
  93. }
  94. sub debug {
  95. my $self = shift;
  96. confess("Object methods must be called by an instance") unless ref($self);
  97. return $self->{'debug'};
  98. }
  99. #Convenient JSON-HTTP fetcher
  100. sub _doRequest {
  101. my ($self,$path,$method,$data) = @_;
  102. confess("Object methods must be called by an instance") unless ref($self);
  103. my $req = clone $self->{'default_request'};
  104. $method //= 'GET';
  105. $req->method($method);
  106. $req->url($self->apiurl.'/'.$path);
  107. warn "$method ".$self->apiurl."/$path" if $self->debug;
  108. #Data sent is JSON
  109. my $content = $data ? encode_json($data) : '';
  110. $req->content($content);
  111. $req->header( "Content-Type" => "application/json" );
  112. my $response = $self->{'browser'}->request($req);
  113. return $response if !defined($response); #worst case
  114. if ($response->code == 403) {
  115. cluck "ERROR: Access Denied.";
  116. return -403;
  117. }
  118. if ($response->code != 200) {
  119. cluck "ERROR: Arguments Bad: ".$response->content;
  120. return -int($response->code);
  121. }
  122. try {
  123. return decode_json($response->content);
  124. } catch {
  125. if ($response->code == 200 && !$response->content) {
  126. return 1; #This function probably just returns no data
  127. } else {
  128. cluck "ERROR: Malformed JSON returned by API.";
  129. cluck $@;
  130. if (!$self->debug) { #Otherwise we've already printed this, but we need to know if we encounter this
  131. cluck "RAW CONTENT:";
  132. cluck $response->content
  133. }
  134. return 0;
  135. }
  136. }
  137. }
  138. =head1 USER METHODS
  139. =head2 B<getUsers ()>
  140. Get all the user definitions for the provided Test Rail install.
  141. Returns ARRAYREF of user definition HASHREFs.
  142. =cut
  143. sub getUsers {
  144. my $self = shift;
  145. confess("Object methods must be called by an instance") unless ref($self);
  146. my $res = $self->_doRequest('index.php?/api/v2/get_users');
  147. return -500 if !$res || (reftype($res) || 'undef') ne 'ARRAY';
  148. $self->{'user_cache'} = $res;
  149. return $res;
  150. }
  151. =head2 B<getUserByID(id)>
  152. =cut
  153. =head2 B<getUserByName(name)>
  154. =cut
  155. =head2 B<getUserByEmail(email)>
  156. Get user definition hash by ID, Name or Email.
  157. Returns user def HASHREF.
  158. =cut
  159. #I'm just using the cache for the following methods because it's more straightforward and faster past 1 call.
  160. sub getUserByID {
  161. my ($self,$user) = @_;
  162. confess("Object methods must be called by an instance") unless ref($self);
  163. confess("User ID must be integer") unless $self->_checkInteger($user);
  164. $self->getUsers() if !defined($self->{'user_cache'});
  165. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  166. foreach my $usr (@{$self->{'user_cache'}}) {
  167. return $usr if $usr->{'id'} == $user;
  168. }
  169. return 0;
  170. }
  171. sub getUserByName {
  172. my ($self,$user) = @_;
  173. confess("Object methods must be called by an instance") unless ref($self);
  174. confess("User must be string") unless $self->_checkString($user);
  175. $self->getUsers() if !defined($self->{'user_cache'});
  176. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  177. foreach my $usr (@{$self->{'user_cache'}}) {
  178. return $usr if $usr->{'name'} eq $user;
  179. }
  180. return 0;
  181. }
  182. sub getUserByEmail {
  183. my ($self,$email) = @_;
  184. confess("Object methods must be called by an instance") unless ref($self);
  185. confess("Email must be string") unless $self->_checkString($email);
  186. $self->getUsers() if !defined($self->{'user_cache'});
  187. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  188. foreach my $usr (@{$self->{'user_cache'}}) {
  189. return $usr if $usr->{'email'} eq $email;
  190. }
  191. return 0;
  192. }
  193. =head1 PROJECT METHODS
  194. =head2 B<createProject (name, [description,send_announcement])>
  195. Creates new Project (Database of testsuites/tests).
  196. Optionally specify an announcement to go out to the users.
  197. Requires TestRail admin login.
  198. =over 4
  199. =item STRING C<NAME> - Desired name of project.
  200. =item STRING C<DESCRIPTION> (optional) - Description of project. Default value is 'res ipsa loquiter'.
  201. =item BOOLEAN C<SEND ANNOUNCEMENT> (optional) - Whether to confront users with an announcement about your awesome project on next login. Default false.
  202. =back
  203. Returns project definition HASHREF on success, false otherwise.
  204. $tl->createProject('Widgetronic 4000', 'Tests for the whiz-bang new product', true);
  205. =cut
  206. sub createProject {
  207. my ($self,$name,$desc,$announce) = @_;
  208. confess("Object methods must be called by an instance") unless ref($self);
  209. confess("Project name must be string") unless $self->_checkString($name);
  210. $desc //= 'res ipsa loquiter';
  211. $announce //= 0;
  212. confess("Project description must be string") unless $self->_checkString($desc);
  213. confess("Announce must be integer") unless $self->_checkInteger($announce);
  214. my $input = {
  215. name => $name,
  216. announcement => $desc,
  217. show_announcement => $announce ? Types::Serialiser::true : Types::Serialiser::false
  218. };
  219. my $result = $self->_doRequest('index.php?/api/v2/add_project','POST',$input);
  220. return $result;
  221. }
  222. =head2 B<deleteProject (id)>
  223. Deletes specified project by ID.
  224. Requires TestRail admin login.
  225. =over 4
  226. =item STRING C<NAME> - Desired name of project.
  227. =back
  228. Returns BOOLEAN.
  229. $success = $tl->deleteProject(1);
  230. =cut
  231. sub deleteProject {
  232. my ($self,$proj) = @_;
  233. confess("Object methods must be called by an instance") unless ref($self);
  234. confess("Project ID must be integer") unless $self->_checkInteger($proj);
  235. my $result = $self->_doRequest('index.php?/api/v2/delete_project/'.$proj,'POST');
  236. return $result;
  237. }
  238. =head2 B<getProjects ()>
  239. Get all available projects
  240. Returns array of project definition HASHREFs, false otherwise.
  241. $projects = $tl->getProjects;
  242. =cut
  243. sub getProjects {
  244. my $self = shift;
  245. confess("Object methods must be called by an instance") unless ref($self);
  246. my $result = $self->_doRequest('index.php?/api/v2/get_projects');
  247. #Save state for future use, if needed
  248. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  249. $self->{'testtree'} = $result;
  250. #Note that it's a project for future reference by recursive tree search
  251. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  252. foreach my $pj (@{$result}) {
  253. $pj->{'type'} = 'project';
  254. }
  255. return $result;
  256. }
  257. =head2 B<getProjectByName ($project)>
  258. Gets some project definition hash by it's name
  259. =over 4
  260. =item STRING C<PROJECT> - desired project
  261. =back
  262. Returns desired project def HASHREF, false otherwise.
  263. $project = $tl->getProjectByName('FunProject');
  264. =cut
  265. sub getProjectByName {
  266. my ($self,$project) = @_;
  267. confess("Object methods must be called by an instance") unless ref($self);
  268. confess("Project must be string.") unless $self->_checkString($project);
  269. #See if we already have the project list...
  270. my $projects = $self->{'testtree'};
  271. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  272. $projects = $self->getProjects() unless scalar(@$projects);
  273. #Search project list for project
  274. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  275. for my $candidate (@$projects) {
  276. return $candidate if ($candidate->{'name'} eq $project);
  277. }
  278. return 0;
  279. }
  280. =head2 B<getProjectByID ($project)>
  281. Gets some project definition hash by it's ID
  282. =over 4
  283. =item INTEGER C<PROJECT> - desired project
  284. =back
  285. Returns desired project def HASHREF, false otherwise.
  286. $projects = $tl->getProjectByID(222);
  287. =cut
  288. sub getProjectByID {
  289. my ($self,$project) = @_;
  290. confess("Object methods must be called by an instance") unless ref($self);
  291. confess("No project provided.") unless $project;
  292. confess("Project ID must be integer") unless $self->_checkInteger($project);
  293. #See if we already have the project list...
  294. my $projects = $self->{'testtree'};
  295. $projects = $self->getProjects() unless scalar(@$projects);
  296. #Search project list for project
  297. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  298. for my $candidate (@$projects) {
  299. return $candidate if ($candidate->{'id'} eq $project);
  300. }
  301. return 0;
  302. }
  303. =head1 TESTSUITE METHODS
  304. =head2 B<createTestSuite (project_id, name, [description])>
  305. Creates new TestSuite (folder of tests) in the database of test specifications under given project id having given name and details.
  306. =over 4
  307. =item INTEGER C<PROJECT ID> - ID of project this test suite should be under.
  308. =item STRING C<NAME> - Desired name of test suite.
  309. =item STRING C<DESCRIPTION> (optional) - Description of test suite. Default value is 'res ipsa loquiter'.
  310. =back
  311. Returns TS definition HASHREF on success, false otherwise.
  312. $tl->createTestSuite(1, 'broken tests', 'Tests that should be reviewed');
  313. =cut
  314. sub createTestSuite {
  315. my ($self,$project_id,$name,$details) = @_;
  316. confess("Object methods must be called by an instance") unless ref($self);
  317. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  318. confess("Name must be a string") unless $self->_checkString($name);
  319. $details ||= 'res ipsa loquiter';
  320. confess("Project details must be a string") unless $self->_checkString($details);
  321. my $input = {
  322. name => $name,
  323. description => $details
  324. };
  325. my $result = $self->_doRequest('index.php?/api/v2/add_suite/'.$project_id,'POST',$input);
  326. return $result;
  327. }
  328. =head2 B<deleteTestSuite (suite_id)>
  329. Deletes specified testsuite.
  330. =over 4
  331. =item INTEGER C<SUITE ID> - ID of testsuite to delete.
  332. =back
  333. Returns BOOLEAN.
  334. $tl->deleteTestSuite(1);
  335. =cut
  336. sub deleteTestSuite {
  337. my ($self,$suite_id) = @_;
  338. confess("Object methods must be called by an instance") unless ref($self);
  339. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  340. my $result = $self->_doRequest('index.php?/api/v2/delete_suite/'.$suite_id,'POST');
  341. return $result;
  342. }
  343. =head2 B<getTestSuites (project_id)>
  344. Gets the testsuites for a project
  345. =over 4
  346. =item STRING C<PROJECT ID> - desired project's ID
  347. =back
  348. Returns ARRAYREF of testsuite definition HASHREFs, 0 on error.
  349. $suites = $tl->getTestSuites(123);
  350. =cut
  351. sub getTestSuites {
  352. my ($self,$proj) = @_;
  353. confess("Object methods must be called by an instance") unless ref($self);
  354. confess("Project ID must be integer") unless $self->_checkInteger($proj);
  355. return $self->_doRequest('index.php?/api/v2/get_suites/'.$proj);
  356. }
  357. =head2 B<getTestSuiteByName (project_id,testsuite_name)>
  358. Gets the testsuite that matches the given name inside of given project.
  359. =over 4
  360. =item STRING C<PROJECT ID> - ID of project holding this testsuite
  361. =item STRING C<TESTSUITE NAME> - desired parent testsuite name
  362. =back
  363. Returns desired testsuite definition HASHREF, false otherwise.
  364. $suites = $tl->getTestSuitesByName(321, 'hugSuite');
  365. =cut
  366. sub getTestSuiteByName {
  367. my ($self,$project_id,$testsuite_name) = @_;
  368. confess("Object methods must be called by an instance") unless ref($self);
  369. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  370. confess("Testsuite Name must be String") unless $self->_checkString($testsuite_name);
  371. #TODO cache
  372. my $suites = $self->getTestSuites($project_id);
  373. return -500 if !$suites || (reftype($suites) || 'undef') ne 'ARRAY'; #No suites for project, or no project
  374. foreach my $suite (@$suites) {
  375. return $suite if $suite->{'name'} eq $testsuite_name;
  376. }
  377. return 0; #Couldn't find it
  378. }
  379. =head2 B<getTestSuiteByID (testsuite_id)>
  380. Gets the testsuite with the given ID.
  381. =over 4
  382. =item STRING C<TESTSUITE_ID> - TestSuite ID.
  383. =back
  384. Returns desired testsuite definition HASHREF, false otherwise.
  385. $tests = $tl->getTestSuiteByID(123);
  386. =cut
  387. sub getTestSuiteByID {
  388. my ($self,$testsuite_id) = @_;
  389. confess("Object methods must be called by an instance") unless ref($self);
  390. confess("Testsuite ID must be integer") unless $self->_checkInteger($testsuite_id);
  391. my $result = $self->_doRequest('index.php?/api/v2/get_suite/'.$testsuite_id);
  392. return $result;
  393. }
  394. =head1 SECTION METHODS
  395. =head2 B<createSection(project_id,suite_id,name,[parent_id])>
  396. Creates a section.
  397. =over 4
  398. =item INTEGER C<PROJECT ID> - Parent Project ID.
  399. =item INTEGER C<SUITE ID> - Parent TestSuite ID.
  400. =item STRING C<NAME> - desired section name.
  401. =item INTEGER C<PARENT ID> (optional) - parent section id
  402. =back
  403. Returns new section definition HASHREF, false otherwise.
  404. $section = $tr->createSection(1,1,'nugs',1);
  405. =cut
  406. sub createSection {
  407. my ($self,$project_id,$suite_id,$name,$parent_id) = @_;
  408. confess("Object methods must be called by an instance") unless ref($self);
  409. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  410. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  411. confess("Section name must be string") unless $self->_checkString($name);
  412. confess("Parent section ID must be integer") unless !defined($parent_id) || $self->_checkInteger($parent_id);
  413. my $input = {
  414. name => $name,
  415. suite_id => $suite_id
  416. };
  417. $input->{'parent_id'} = $parent_id if $parent_id;
  418. my $result = $self->_doRequest('index.php?/api/v2/add_section/'.$project_id,'POST',$input);
  419. return $result;
  420. }
  421. =head2 B<deleteSection (section_id)>
  422. Deletes specified section.
  423. =over 4
  424. =item INTEGER C<SECTION ID> - ID of section to delete.
  425. =back
  426. Returns BOOLEAN.
  427. $tr->deleteSection(1);
  428. =cut
  429. sub deleteSection {
  430. my ($self,$section_id) = @_;
  431. confess("Object methods must be called by an instance") unless ref($self);
  432. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  433. my $result = $self->_doRequest('index.php?/api/v2/delete_section/'.$section_id,'POST');
  434. return $result;
  435. }
  436. =head2 B<getSections (project_id,suite_id)>
  437. Gets sections for a given project and suite.
  438. =over 4
  439. =item INTEGER C<PROJECT ID> - ID of parent project.
  440. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  441. =back
  442. Returns ARRAYREF of section definition HASHREFs.
  443. $tr->getSections(1,2);
  444. =cut
  445. sub getSections {
  446. my ($self,$project_id,$suite_id) = @_;
  447. confess("Object methods must be called by an instance") unless ref($self);
  448. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  449. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  450. return $self->_doRequest("index.php?/api/v2/get_sections/$project_id&suite_id=$suite_id");
  451. }
  452. =head2 B<getSectionByID (section_id)>
  453. Gets desired section.
  454. =over 4
  455. =item INTEGER C<PROJECT ID> - ID of parent project.
  456. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  457. =back
  458. Returns section definition HASHREF.
  459. $tr->getSectionByID(344);
  460. =cut
  461. sub getSectionByID {
  462. my ($self,$section_id) = @_;
  463. confess("Object methods must be called by an instance") unless ref($self);
  464. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  465. return $self->_doRequest("index.php?/api/v2/get_section/$section_id");
  466. }
  467. =head2 B<getSectionByName (project_id,suite_id,name)>
  468. Gets desired section.
  469. =over 4
  470. =item INTEGER C<PROJECT ID> - ID of parent project.
  471. =item INTEGER C<SUITE ID> - ID of suite to get section for.
  472. =item STRING C<NAME> - name of section to get
  473. =back
  474. Returns section definition HASHREF.
  475. $tr->getSectionByName(1,2,'nugs');
  476. =cut
  477. sub getSectionByName {
  478. my ($self,$project_id,$suite_id,$section_name) = @_;
  479. confess("Object methods must be called by an instance") unless ref($self);
  480. confess("Project ID must be an integer") unless $self->_checkInteger($project_id);
  481. confess("Suite ID must be an integer") unless $self->_checkInteger($suite_id);
  482. confess("Section Name must be a string") unless $self->_checkString($section_name);
  483. my $sections = $self->getSections($project_id,$suite_id);
  484. return -500 if !$sections || (reftype($sections) || 'undef') ne 'ARRAY';
  485. foreach my $sec (@$sections) {
  486. return $sec if $sec->{'name'} eq $section_name;
  487. }
  488. return 0;
  489. }
  490. =head1 CASE METHODS
  491. =head2 B<getCaseTypes ()>
  492. Gets possible case types.
  493. Returns ARRAYREF of case type definition HASHREFs.
  494. $tr->getCaseTypes();
  495. =cut
  496. sub getCaseTypes {
  497. my $self = shift;
  498. confess("Object methods must be called by an instance") unless ref($self);
  499. my $types = $self->_doRequest("index.php?/api/v2/get_case_types");
  500. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  501. $self->{'type_cache'} = $types if !$self->{'type_cache'}; #We can't change this with API, so assume it is static
  502. return $self->{'type_cache'};
  503. }
  504. =head2 B<getCaseTypeByName (name)>
  505. Gets case type by name.
  506. =over 4
  507. =item STRING C<NAME> - Name of desired case type
  508. =back
  509. Returns case type definition HASHREF.
  510. $tr->getCaseTypeByName();
  511. =cut
  512. sub getCaseTypeByName {
  513. #Useful for marking automated tests, etc
  514. my ($self,$name) = @_;
  515. confess("Object methods must be called by an instance") unless ref($self);
  516. confess("Case type must be string") unless $self->_checkString($name);
  517. my $types = $self->getCaseTypes();
  518. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  519. foreach my $type (@$types) {
  520. return $type if $type->{'name'} eq $name;
  521. }
  522. return 0;
  523. }
  524. =head2 B<createCase(section_id,title,type_id,options,extra_options)>
  525. Creates a test case.
  526. =over 4
  527. =item INTEGER C<SECTION ID> - Parent Project ID.
  528. =item STRING C<TITLE> - Case title.
  529. =item INTEGER C<TYPE_ID> (optional) - desired test type's ID. Defaults to whatever your TR install considers the default type.
  530. =item HASHREF C<OPTIONS> (optional) - Custom fields in the case are the keys, set to the values provided. See TestRail API documentation for more info.
  531. =item HASHREF C<EXTRA OPTIONS> (optional) - contains priority_id, estimate, milestone_id and refs as possible keys. See TestRail API documentation for more info.
  532. =back
  533. Returns new case definition HASHREF, false otherwise.
  534. $custom_opts = {
  535. preconds => "Test harness installed",
  536. steps => "Do the needful",
  537. expected => "cubicle environment transforms into Dali painting"
  538. };
  539. $other_opts = {
  540. priority_id => 4,
  541. milestone_id => 666,
  542. estimate => '2m 45s',
  543. refs => ['TRACE-22','ON-166'] #ARRAYREF of bug IDs.
  544. }
  545. $case = $tr->createCase(1,'Do some stuff',3,$custom_opts,$other_opts);
  546. =cut
  547. sub createCase {
  548. my ($self,$section_id,$title,$type_id,$opts,$extras) = @_;
  549. confess("Object methods must be called by an instance") unless ref($self);
  550. confess("Section ID ($section_id) must be integer") unless $self->_checkInteger($section_id);
  551. confess("title must be string") unless $self->_checkString($title);
  552. confess("Type ID must be integer") unless !defined($type_id) || $self->_checkInteger($type_id);
  553. confess("Options must be HASHREF") unless !defined($opts) || (reftype($opts) || 'undef') ne 'HASH';
  554. confess("Extras must be HASHREF") unless !defined($extras) || (reftype($extras) || 'undef') ne 'HASH';
  555. my $stuff = {
  556. title => $title,
  557. type_id => $type_id
  558. };
  559. #Handle sort of optional but baked in options
  560. if (defined($extras) && reftype($extras) eq 'HASH') {
  561. $stuff->{'priority_id'} = $extras->{'priority_id'} if defined($extras->{'priority_id'});
  562. $stuff->{'estimate'} = $extras->{'estimate'} if defined($extras->{'estimate'});
  563. $stuff->{'milestone_id'} = $extras->{'milestone_id'} if defined($extras->{'milestone_id'});
  564. $stuff->{'refs'} = join(',',@{$extras->{'refs'}}) if defined($extras->{'refs'});
  565. }
  566. #Handle custom fields
  567. if (defined($opts) && reftype($opts) eq 'HASH') {
  568. foreach my $key (keys(%$opts)) {
  569. $stuff->{"custom_$key"} = $opts->{$key};
  570. }
  571. }
  572. my $result = $self->_doRequest("index.php?/api/v2/add_case/$section_id",'POST',$stuff);
  573. return $result;
  574. }
  575. =head2 B<deleteCase (case_id)>
  576. Deletes specified section.
  577. =over 4
  578. =item INTEGER C<CASE ID> - ID of case to delete.
  579. =back
  580. Returns BOOLEAN.
  581. $tr->deleteCase(1324);
  582. =cut
  583. sub deleteCase {
  584. my ($self,$case_id) = @_;
  585. confess("Object methods must be called by an instance") unless ref($self);
  586. confess("Case ID must be integer") unless $self->_checkInteger($case_id);
  587. my $result = $self->_doRequest("index.php?/api/v2/delete_case/$case_id",'POST');
  588. return $result;
  589. }
  590. =head2 B<getCases (project_id,suite_id,section_id)>
  591. Gets cases for provided section.
  592. =over 4
  593. =item INTEGER C<PROJECT ID> - ID of parent project.
  594. =item INTEGER C<SUITE ID> - ID of parent suite.
  595. =item INTEGER C<SECTION ID> - ID of parent section
  596. =back
  597. Returns ARRAYREF of test case definition HASHREFs.
  598. $tr->getCases(1,2,3);
  599. =cut
  600. sub getCases {
  601. my ($self,$project_id,$suite_id,$section_id) = @_;
  602. confess("Object methods must be called by an instance") unless ref($self);
  603. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  604. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  605. my $url = "index.php?/api/v2/get_cases/$project_id&suite_id=$suite_id";
  606. $url .= "&section_id=$section_id" if $section_id;
  607. return $self->_doRequest($url);
  608. }
  609. =head2 B<getCaseByName (project_id,suite_id,section_id,name)>
  610. Gets case by name.
  611. =over 4
  612. =item INTEGER C<PROJECT ID> - ID of parent project.
  613. =item INTEGER C<SUITE ID> - ID of parent suite.
  614. =item INTEGER C<SECTION ID> - ID of parent section.
  615. =item STRING <NAME> - Name of desired test case.
  616. =back
  617. Returns test case definition HASHREF.
  618. $tr->getCaseByName(1,2,3,'nugs');
  619. =cut
  620. sub getCaseByName {
  621. my ($self,$project_id,$suite_id,$section_id,$name) = @_;
  622. confess("Object methods must be called by an instance") unless ref($self);
  623. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  624. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  625. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  626. confess("Test Case name must be string") unless $self->_checkString($name);
  627. my $cases = $self->getCases($project_id,$suite_id,$section_id);
  628. return -500 if !$cases || (reftype($cases) || 'undef') ne 'ARRAY';
  629. foreach my $case (@$cases) {
  630. return $case if $case->{'title'} eq $name;
  631. }
  632. return 0;
  633. }
  634. =head2 B<getCaseByID (case_id)>
  635. Gets case by ID.
  636. =over 4
  637. =item INTEGER C<CASE ID> - ID of case.
  638. =back
  639. Returns test case definition HASHREF.
  640. $tr->getCaseByID(1345);
  641. =cut
  642. sub getCaseByID {
  643. my ($self,$case_id) = @_;
  644. confess("Object methods must be called by an instance") unless ref($self);
  645. confess("Case ID must be integer") unless $self->_checkInteger($case_id);
  646. return $self->_doRequest("index.php?/api/v2/get_case/$case_id");
  647. }
  648. =head1 RUN METHODS
  649. =head2 B<createRun (project_id,suite_id,name,description,milestone_id,assigned_to_id,case_ids)>
  650. Create a run.
  651. =over 4
  652. =item INTEGER C<PROJECT ID> - ID of parent project.
  653. =item INTEGER C<SUITE ID> - ID of suite to base run on
  654. =item STRING C<NAME> - Name of run
  655. =item STRING C<DESCRIPTION> (optional) - Description of run
  656. =item INTEGER C<MILESTONE ID> (optional) - ID of milestone
  657. =item INTEGER C<ASSIGNED TO ID> (optional) - User to assign the run to
  658. =item ARRAYREF C<CASE IDS> (optional) - Array of case IDs in case you don't want to use the whole testsuite when making the build.
  659. =back
  660. Returns run definition HASHREF.
  661. $tr->createRun(1,1345,'RUN AWAY','SO FAR AWAY',22,3,[3,4,5,6]);
  662. =cut
  663. #If you pass an array of case ids, it implies include_all is false
  664. sub createRun {
  665. my ($self,$project_id,$suite_id,$name,$desc,$milestone_id,$assignedto_id,$case_ids) = @_;
  666. confess("Object methods must be called by an instance") unless ref($self);
  667. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  668. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  669. confess("Name must be string") unless $self->_checkString($name);
  670. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  671. confess("Milestone ID must be integer") unless !defined($milestone_id) || $self->_checkInteger($milestone_id);
  672. confess("Assigned To ID must be integer") unless !defined($assignedto_id) || $self->_checkInteger($assignedto_id);
  673. confess("Case IDs must be ARRAYREF") unless !defined($case_ids) || (reftype($case_ids) || 'undef') eq 'ARRAY';
  674. my $stuff = {
  675. suite_id => $suite_id,
  676. name => $name,
  677. description => $desc,
  678. milestone_id => $milestone_id,
  679. assignedto_id => $assignedto_id,
  680. include_all => $case_ids ? Types::Serialiser::false : Types::Serialiser::true,
  681. case_ids => $case_ids
  682. };
  683. my $result = $self->_doRequest("index.php?/api/v2/add_run/$project_id",'POST',$stuff);
  684. return $result;
  685. }
  686. =head2 B<deleteRun (run_id)>
  687. Deletes specified run.
  688. =over 4
  689. =item INTEGER C<RUN ID> - ID of run to delete.
  690. =back
  691. Returns BOOLEAN.
  692. $tr->deleteRun(1324);
  693. =cut
  694. sub deleteRun {
  695. my ($self,$run_id) = @_;
  696. confess("Object methods must be called by an instance") unless ref($self);
  697. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  698. my $result = $self->_doRequest("index.php?/api/v2/delete_run/$run_id",'POST');
  699. return $result;
  700. }
  701. =head2 B<getRuns (project_id)>
  702. Get all runs for specified project.
  703. =over 4
  704. =item INTEGER C<PROJECT_ID> - ID of parent project
  705. =back
  706. Returns ARRAYREF of run definition HASHREFs.
  707. $allRuns = $tr->getRuns(6969);
  708. =cut
  709. sub getRuns {
  710. my ($self,$project_id) = @_;
  711. confess("Object methods must be called by an instance") unless ref($self);
  712. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  713. return $self->_doRequest("index.php?/api/v2/get_runs/$project_id");
  714. }
  715. =head2 B<getRunByName (project_id,name)>
  716. Gets run by name.
  717. =over 4
  718. =item INTEGER C<PROJECT ID> - ID of parent project.
  719. =item STRING <NAME> - Name of desired run.
  720. =back
  721. Returns run definition HASHREF.
  722. $tr->getRunByName(1,'gravy');
  723. =cut
  724. sub getRunByName {
  725. my ($self,$project_id,$name) = @_;
  726. confess("Object methods must be called by an instance") unless ref($self);
  727. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  728. confess("Run name must be string") unless $self->_checkString($name);
  729. my $runs = $self->getRuns($project_id);
  730. return -500 if !$runs || (reftype($runs) || 'undef') ne 'ARRAY';
  731. foreach my $run (@$runs) {
  732. return $run if $run->{'name'} eq $name;
  733. }
  734. return 0;
  735. }
  736. =head2 B<getRunByID (run_id)>
  737. Gets run by ID.
  738. =over 4
  739. =item INTEGER C<RUN ID> - ID of desired run.
  740. =back
  741. Returns run definition HASHREF.
  742. $tr->getRunByID(7779311);
  743. =cut
  744. sub getRunByID {
  745. my ($self,$run_id) = @_;
  746. confess("Object methods must be called by an instance") unless ref($self);
  747. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  748. return $self->_doRequest("index.php?/api/v2/get_run/$run_id");
  749. }
  750. =head1 PLAN METHODS
  751. =head2 B<createPlan (project_id,name,description,milestone_id,entries)>
  752. Create a test plan.
  753. =over 4
  754. =item INTEGER C<PROJECT ID> - ID of parent project.
  755. =item STRING C<NAME> - Name of plan
  756. =item STRING C<DESCRIPTION> (optional) - Description of plan
  757. =item INTEGER C<MILESTONE_ID> (optional) - ID of milestone
  758. =item ARRAYREF C<ENTRIES> (optional) - New Runs to initially populate the plan with -- See TestRail API documentation for more advanced inputs here.
  759. =back
  760. Returns test plan definition HASHREF, or false on failure.
  761. $entries = {
  762. suite_id => 345,
  763. include_all => Types::Serialiser::true,
  764. assignedto_id => 1
  765. }
  766. $tr->createPlan(1,'Gosplan','Robo-Signed Soviet 5-year plan',22,$entries);
  767. =cut
  768. sub createPlan {
  769. my ($self,$project_id,$name,$desc,$milestone_id,$entries) = @_;
  770. confess("Object methods must be called by an instance") unless ref($self);
  771. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  772. confess("Plan name must be string") unless $self->_checkString($name);
  773. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  774. confess("Milestone ID must be integer") unless !defined($milestone_id) || $self->_checkInteger($milestone_id);
  775. confess("Entries must be ARRAYREF") unless !defined($entries) || (reftype($entries) || 'undef') eq 'ARRAY';
  776. my $stuff = {
  777. name => $name,
  778. description => $desc,
  779. milestone_id => $milestone_id,
  780. entries => $entries
  781. };
  782. my $result = $self->_doRequest("index.php?/api/v2/add_plan/$project_id",'POST',$stuff);
  783. return $result;
  784. }
  785. =head2 B<deletePlan (plan_id)>
  786. Deletes specified plan.
  787. =over 4
  788. =item INTEGER C<PLAN ID> - ID of plan to delete.
  789. =back
  790. Returns BOOLEAN.
  791. $tr->deletePlan(8675309);
  792. =cut
  793. sub deletePlan {
  794. my ($self,$plan_id) = @_;
  795. confess("Object methods must be called by an instance") unless ref($self);
  796. confess("Plan ID must be integer") unless $self->_checkInteger($plan_id);
  797. my $result = $self->_doRequest("index.php?/api/v2/delete_plan/$plan_id",'POST');
  798. return $result;
  799. }
  800. =head2 B<getPlans (project_id)>
  801. Deletes specified plan.
  802. =over 4
  803. =item INTEGER C<PROJECT ID> - ID of parent project.
  804. =back
  805. Returns ARRAYREF of plan definition HASHREFs.
  806. $tr->getPlans(8);
  807. =cut
  808. sub getPlans {
  809. my ($self,$project_id) = @_;
  810. confess("Object methods must be called by an instance") unless ref($self);
  811. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  812. return $self->_doRequest("index.php?/api/v2/get_plans/$project_id");
  813. }
  814. =head2 B<getPlanByName (project_id,name)>
  815. Gets specified plan by name.
  816. =over 4
  817. =item INTEGER C<PROJECT ID> - ID of parent project.
  818. =item STRING C<NAME> - Name of test plan.
  819. =back
  820. Returns plan definition HASHREF.
  821. $tr->getPlanByName(8,'GosPlan');
  822. =cut
  823. sub getPlanByName {
  824. my ($self,$project_id,$name) = @_;
  825. confess("Object methods must be called by an instance") unless ref($self);
  826. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  827. confess("Plan name must be string") unless $self->_checkString($name);
  828. my $plans = $self->getPlans($project_id);
  829. return -500 if !$plans || (reftype($plans) || 'undef') ne 'ARRAY';
  830. foreach my $plan (@$plans) {
  831. return $plan if $plan->{'name'} eq $name;
  832. }
  833. return 0;
  834. }
  835. =head2 B<getPlanByID (plan_id)>
  836. Gets specified plan by ID.
  837. =over 4
  838. =item INTEGER C<PLAN ID> - ID of plan.
  839. =back
  840. Returns plan definition HASHREF.
  841. $tr->getPlanByID(2);
  842. =cut
  843. sub getPlanByID {
  844. my ($self,$plan_id) = @_;
  845. confess("Object methods must be called by an instance") unless ref($self);
  846. confess("Plan ID must be integer") unless $self->_checkInteger($plan_id);
  847. return $self->_doRequest("index.php?/api/v2/get_plan/$plan_id");
  848. }
  849. =head1 MILESTONE METHODS
  850. =head2 B<createMilestone (project_id,name,description,due_on)>
  851. Create a milestone.
  852. =over 4
  853. =item INTEGER C<PROJECT ID> - ID of parent project.
  854. =item STRING C<NAME> - Name of milestone
  855. =item STRING C<DESCRIPTION> (optional) - Description of milestone
  856. =item INTEGER C<DUE_ON> - Date at which milestone should be completed. Unix Timestamp.
  857. =back
  858. Returns milestone definition HASHREF, or false on failure.
  859. $tr->createMilestone(1,'Patriotic victory of world perlism','Accomplish by Robo-Signed Soviet 5-year plan',time()+157788000);
  860. =cut
  861. sub createMilestone {
  862. my ($self,$project_id,$name,$desc,$due_on) = @_;
  863. confess("Object methods must be called by an instance") unless ref($self);
  864. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  865. confess("Name must be string") unless $self->_checkString($name);
  866. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  867. confess("Due on must be unix time stamp (integer)") unless !defined($due_on) || $self->_checkInteger($due_on);
  868. my $stuff = {
  869. name => $name,
  870. description => $desc,
  871. due_on => $due_on # unix timestamp
  872. };
  873. my $result = $self->_doRequest("index.php?/api/v2/add_milestone/$project_id",'POST',$stuff);
  874. return $result;
  875. }
  876. =head2 B<deleteMilestone (milestone_id)>
  877. Deletes specified milestone.
  878. =over 4
  879. =item INTEGER C<MILESTONE ID> - ID of milestone to delete.
  880. =back
  881. Returns BOOLEAN.
  882. $tr->deleteMilestone(86);
  883. =cut
  884. sub deleteMilestone {
  885. my ($self,$milestone_id) = @_;
  886. confess("Object methods must be called by an instance") unless ref($self);
  887. confess("Milestone ID must be integer") unless $self->_checkInteger($milestone_id);
  888. my $result = $self->_doRequest("index.php?/api/v2/delete_milestone/$milestone_id",'POST');
  889. return $result;
  890. }
  891. =head2 B<getMilestones (project_id)>
  892. Get milestones for some project.
  893. =over 4
  894. =item INTEGER C<PROJECT ID> - ID of parent project.
  895. =back
  896. Returns ARRAYREF of milestone definition HASHREFs.
  897. $tr->getMilestones(8);
  898. =cut
  899. sub getMilestones {
  900. my ($self,$project_id) = @_;
  901. confess("Object methods must be called by an instance") unless ref($self);
  902. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  903. return $self->_doRequest("index.php?/api/v2/get_milestones/$project_id");
  904. }
  905. =head2 B<getMilestoneByName (project_id,name)>
  906. Gets specified milestone by name.
  907. =over 4
  908. =item INTEGER C<PROJECT ID> - ID of parent project.
  909. =item STRING C<NAME> - Name of milestone.
  910. =back
  911. Returns milestone definition HASHREF.
  912. $tr->getMilestoneByName(8,'whee');
  913. =cut
  914. sub getMilestoneByName {
  915. my ($self,$project_id,$name) = @_;
  916. confess("Object methods must be called by an instance") unless ref($self);
  917. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  918. confess("Milestone name must be string") unless $self->_checkString($name);
  919. my $milestones = $self->getMilestones($project_id);
  920. return -500 if !$milestones || (reftype($milestones) || 'undef') ne 'ARRAY';
  921. foreach my $milestone (@$milestones) {
  922. return $milestone if $milestone->{'name'} eq $name;
  923. }
  924. return 0;
  925. }
  926. =head2 B<getMilestoneByID (plan_id)>
  927. Gets specified milestone by ID.
  928. =over 4
  929. =item INTEGER C<MILESTONE ID> - ID of milestone.
  930. =back
  931. Returns milestone definition HASHREF.
  932. $tr->getMilestoneByID(2);
  933. =cut
  934. sub getMilestoneByID {
  935. my ($self,$milestone_id) = @_;
  936. confess("Object methods must be called by an instance") unless ref($self);
  937. confess("Milestone ID must be integer") unless $self->_checkInteger($milestone_id);
  938. return $self->_doRequest("index.php?/api/v2/get_milestone/$milestone_id");
  939. }
  940. =head1 TEST METHODS
  941. =head2 B<getTests (run_id)>
  942. Get tests for some run.
  943. =over 4
  944. =item INTEGER C<RUN ID> - ID of parent run.
  945. =back
  946. Returns ARRAYREF of test definition HASHREFs.
  947. $tr->getTests(8);
  948. =cut
  949. sub getTests {
  950. my ($self,$run_id) = @_;
  951. confess("Object methods must be called by an instance") unless ref($self);
  952. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  953. return $self->_doRequest("index.php?/api/v2/get_tests/$run_id");
  954. }
  955. =head2 B<getTestByName (run_id,name)>
  956. Gets specified test by name.
  957. =over 4
  958. =item INTEGER C<RUN ID> - ID of parent run.
  959. =item STRING C<NAME> - Name of milestone.
  960. =back
  961. Returns test definition HASHREF.
  962. $tr->getTestByName(36,'wheeTest');
  963. =cut
  964. sub getTestByName {
  965. my ($self,$run_id,$name) = @_;
  966. confess("Object methods must be called by an instance") unless ref($self);
  967. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  968. confess("Test name must be string") unless $self->_checkString($name);
  969. my $tests = $self->getTests($run_id);
  970. return -500 if !$tests || (reftype($tests) || 'undef') ne 'ARRAY';
  971. foreach my $test (@$tests) {
  972. return $test if $test->{'title'} eq $name;
  973. }
  974. return 0;
  975. }
  976. =head2 B<getTestByID (test_id)>
  977. Gets specified test by ID.
  978. =over 4
  979. =item INTEGER C<TEST ID> - ID of test.
  980. =back
  981. Returns test definition HASHREF.
  982. $tr->getTestByID(222222);
  983. =cut
  984. sub getTestByID {
  985. my ($self,$test_id) = @_;
  986. confess("Object methods must be called by an instance") unless ref($self);
  987. confess("Test ID must be integer") unless $self->_checkInteger($test_id);
  988. return $self->_doRequest("index.php?/api/v2/get_test/$test_id");
  989. }
  990. =head2 B<getTestResultFields()>
  991. Gets custom fields that can be set for tests.
  992. Returns ARRAYREF of result definition HASHREFs.
  993. =cut
  994. sub getTestResultFields {
  995. my $self = shift;
  996. confess("Object methods must be called by an instance") unless ref($self);
  997. return $self->_doRequest('index.php?/api/v2/get_result_fields');
  998. }
  999. =head2 B<getPossibleTestStatuses()>
  1000. Gets all possible statuses a test can be set to.
  1001. Returns ARRAYREF of status definition HASHREFs.
  1002. =cut
  1003. sub getPossibleTestStatuses {
  1004. my $self = shift;
  1005. confess("Object methods must be called by an instance") unless ref($self);
  1006. return $self->_doRequest('index.php?/api/v2/get_statuses');
  1007. }
  1008. =head2 B<createTestResults(test_id,status_id,comment,options,custom_options)>
  1009. Creates a result entry for a test.
  1010. =over 4
  1011. =item INTEGER C<TEST_ID> - ID of desired test
  1012. =item INTEGER C<STATUS_ID> - ID of desired test result status
  1013. =item STRING C<COMMENT> (optional) - Any comments about this result
  1014. =item HASHREF C<OPTIONS> (optional) - Various "Baked-In" options that can be set for test results. See TR docs for more information.
  1015. =item HASHREF C<CUSTOM OPTIONS> (optional) - Options to set for custom fields. See L<TestRail::API::buildStepResults> for a simple way to post up custom steps.
  1016. =back
  1017. Returns result definition HASHREF.
  1018. $options = {
  1019. elapsed => '30m 22s',
  1020. defects => ['TSR-3','BOOM-44'],
  1021. version => '6969'
  1022. };
  1023. $custom_options = {
  1024. step_results => [
  1025. {
  1026. content => 'Step 1',
  1027. expected => "Bought Groceries",
  1028. actual => "No Dinero!",
  1029. status_id => 2
  1030. },
  1031. {
  1032. content => 'Step 2',
  1033. expected => 'Ate Dinner',
  1034. actual => 'Went Hungry',
  1035. status_id => 2
  1036. }
  1037. ]
  1038. };
  1039. $res = $tr->createTestResults(1,2,'Test failed because it was all like WAAAAAAA when I poked it',$options,$custom_options);
  1040. =cut
  1041. sub createTestResults {
  1042. my ($self,$test_id,$status_id,$comment,$opts,$custom_fields) = @_;
  1043. confess("Object methods must be called by an instance") unless ref($self);
  1044. confess("Test ID must be integer") unless $self->_checkInteger($test_id);
  1045. confess("Status ID must be integer") unless $self->_checkInteger($status_id);
  1046. confess("Comment must be string") unless !defined($comment) || $self->_checkString($comment);
  1047. confess("Options must be HASHREF") unless !defined($opts) || (reftype($opts) || 'undef') eq 'HASH';
  1048. confess("Custom Options must be HASHREF") unless !defined($custom_fields) || (reftype($custom_fields) || 'undef') eq 'HASH';
  1049. my $stuff = {
  1050. status_id => $status_id,
  1051. comment => $comment
  1052. };
  1053. #Handle options
  1054. if (defined($opts) && reftype($opts) eq 'HASH') {
  1055. $stuff->{'version'} = defined($opts->{'version'}) ? $opts->{'version'} : undef;
  1056. $stuff->{'elapsed'} = defined($opts->{'elapsed'}) ? $opts->{'elapsed'} : undef;
  1057. $stuff->{'defects'} = defined($opts->{'defects'}) ? join(',',@{$opts->{'defects'}}) : undef;
  1058. $stuff->{'assignedto_id'} = defined($opts->{'assignedto_id'}) ? $opts->{'assignedto_id'} : undef;
  1059. }
  1060. #Handle custom fields
  1061. if (defined($custom_fields) && reftype($custom_fields) eq 'HASH') {
  1062. foreach my $field (keys(%$custom_fields)) {
  1063. $stuff->{"custom_$field"} = $custom_fields->{$field};
  1064. }
  1065. }
  1066. return $self->_doRequest("index.php?/api/v2/add_result/$test_id",'POST',$stuff);
  1067. }
  1068. =head2 B<getTestResults(test_id,limit)>
  1069. Get the recorded results for desired test, limiting output to 'limit' entries.
  1070. =over 4
  1071. =item INTEGER C<TEST_ID> - ID of desired test
  1072. =item POSITIVE INTEGER C<LIMIT> (OPTIONAL) - provide no more than this number of results.
  1073. =item INTEGER C<OFFSET> (OPTIONAL) - Offset to begin viewing resultset at.
  1074. =back
  1075. Returns ARRAYREF of result definition HASHREFs.
  1076. =cut
  1077. sub getTestResults {
  1078. my ($self,$test_id,$limit,$offset) = @_;
  1079. confess("Object methods must be called by an instance") unless ref($self);
  1080. confess("Test ID must be positive integer") unless $self->_checkInteger($test_id);
  1081. confess("Result limitation must be positive integer") unless !defined($limit) || ($self->_checkInteger($limit) && $limit > 0);
  1082. confess("Result offset must be integer") unless !defined($offset) || $self->_checkInteger($offset);
  1083. my $url = "index.php?/api/v2/get_results/$test_id";
  1084. $url .= "&limit=$limit" if defined($limit);
  1085. $url .= "&offset=$offset" if defined($offset);
  1086. return $self->_doRequest($url);
  1087. }
  1088. =head1 STATIC METHODS
  1089. =head2 B<buildStepResults(content,expected,actual,status_id)>
  1090. Convenience method to build the stepResult hashes seen in the custom options for getTestResults.
  1091. =over 4
  1092. =item STRING C<CONTENT> (optional) - The step itself.
  1093. =item STRING C<EXPECTED> (optional) - Expected result of test step.
  1094. =item STRING C<ACTUAL> (optional) - Actual result of test step
  1095. =item INTEGER C<STATUS ID> (optional) - Status ID of result
  1096. =back
  1097. =cut
  1098. #Convenience method for building stepResults
  1099. sub buildStepResults {
  1100. my ($content,$expected,$actual,$status_id) = @_;
  1101. return {
  1102. content => $content,
  1103. expected => $expected,
  1104. actual => $actual,
  1105. status_id => $status_id
  1106. };
  1107. }
  1108. #Type checks
  1109. sub _checkInteger {
  1110. shift;
  1111. my $integer = shift;
  1112. return ( defined $integer && looks_like_number($integer) && int($integer) == $integer );
  1113. }
  1114. sub _checkString {
  1115. shift;
  1116. my $str = shift;
  1117. return ( defined($str) && !ref($str) );
  1118. }
  1119. 1;
  1120. __END__
  1121. =head1 SEE ALSO
  1122. L<HTTP::Request>
  1123. L<LWP::UserAgent>
  1124. L<JSON::XS>
  1125. L<http://docs.gurock.com/testrail-api2/start>
  1126. =head1 SPECIAL THANKS
  1127. Thanks to cPanel Inc, for graciously funding the creation of this module.