How to get all test cases from project through testlink api

Discussion and information for XML-RPC interface.

Moderators: Amaradana, TurboPT, TL Developers

How to get all test cases from project through testlink api

Postby zeeshanyshaikh » Fri Mar 24, 2017 7:26 am

I can only see methods to retrieve the test cases if they are bind with any plan. (getTestCasesForTestPlan). I want to retrieve all the test cases written by the user before its used in a plan. Basically, I want to track the count of test cases, written by, created at, all such data before hand (before adding to test plan) through testlink API.

Something similar to Test Cases created per User on the UI

Any suggestions would be appreciated.
zeeshanyshaikh
TestLink user
 
Posts: 3
Joined: Fri Feb 24, 2017 7:40 am
Location: Mumbai, India

Re: How to get all test cases from project through testlink

Postby fman » Mon Mar 27, 2017 5:45 am

if yuu have check all available method on API, only way is that you develop the missing method and provide your work to community
fman
Member of TestLink Community
 
Posts: 2987
Joined: Tue Nov 15, 2005 7:19 am

Re: How to get all test cases from project through testlink

Postby zeeshanyshaikh » Tue Mar 28, 2017 6:32 am

Thanks, @fman

I was able to create method for fetching all the test cases per project (again from this we need to filter it out as per need)
Below are the changes made:

Modified getall() function from \testlink1.9.16\lib\functions\testcase.class.php

Code: Select all
function get_all($limit, $offset)
  {
   $sql = " SELECT {$this->tables['nodes_hierarchy']}.parent_id, {$this->tables['nodes_hierarchy']}.id
             FROM  {$this->tables['nodes_hierarchy']}
  WHERE {$this->tables['nodes_hierarchy']}.node_type_id={$this->my_node_type} LIMIT {$limit} OFFSET {$offset}";
    $recordset = $this->db->get_recordset($sql);
 
    return $recordset;
  }


Added two methods to fetch the project name and test case information in \testlink1.9.16\lib\api\xmlrpc\v1\xmlrpc.class.php. Used recursive call to get parent id to get test suite and ultimately reach its root node which is nothing but its project name. Fetching the test cases in chunks of 100 to avoid memory consumption. Again need to call getAllTestCasesInProject() function in loop to get all test cases from all projects.

Code: Select all
public function getAllTestCasesInProject($args, $limit=100, $offset=0){
    
   $allTCInfo = array();
   $alltestcases = $this->tcaseMgr->get_all($limit, $offset);
   foreach($alltestcases as $testcase){
      $eachtc = array();
      //*workarond to get project name* - fetch testcase id and recursivelly check the parent testsuite till reaches the root node which is the project name
      $eachtc["project"] = $this->_checkParentOfEachTestSuite($testcase["parent_id"], $args);
      $args["testcaseid"] = $testcase["id"];   
      $eachtc["testcase"] = $this->getTestCase($args);
      $allTCInfo[] = $eachtc;
   }
   return $allTCInfo;
  }
 
  protected function _checkParentOfEachTestSuite($testSuiteId, $args, $ListTSID = array()){
   $args["testsuiteid"] = $testSuiteId;
   $testsuite = $this->getTestSuiteByID($args);
   if(array_key_exists("0",$testsuite)){
      $getallproj = $this->getProjects($args);
      foreach ($getallproj as $getproj) {
         if($getproj["id"] == $ListTSID[count($ListTSID) -1] || $getproj["id"] == $testSuiteId){
            return $getproj["name"];
         }
      }
   }else{
      $ListTSID[] = $testsuite["parent_id"];
      return $this->_checkParentOfEachTestSuite($testsuite["parent_id"], $args, $ListTSID);
   }
  }


Added the getAllTestCasesInProject() function in initMethodYellowPages() method.
Hope this is helpful for others as well. Please let me know if this can be further optimized.

Thanks
Zeeshan
zeeshanyshaikh
TestLink user
 
Posts: 3
Joined: Fri Feb 24, 2017 7:40 am
Location: Mumbai, India

Re: How to get all test cases from project through testlink

Postby fman » Tue Mar 28, 2017 7:52 pm

Hi

1) SQL code has to be valid for MySQL, Postgresql & MSSSQL. have you checked it?
2) Is a very bad pratice, to change an existent method, without maintaining original behaviour
3) you have modified get_all() or getall(), it's not the same.
4) would you mind to explain changes?
5) we already have a method to in testproject class => get_all_testcases_id() why not to use this?

IMHO you need to rethink your solution
fman
Member of TestLink Community
 
Posts: 2987
Joined: Tue Nov 15, 2005 7:19 am

Re: How to get all test cases from project through testlink

Postby zeeshanyshaikh » Wed Mar 29, 2017 9:21 am

Thanks, @fman for taking your time and correcting me.

I agree my approach was wrong and method of implementing as well. I have reverted back all the changes (reverted get_all() function as well)

Now am using the function provided by you from testproject class => get_all_testcases_id() . I have created a new method to get all test case id within a project, one can loop through and get all details of each test case by using getTestCase() method. Below is the code for the fetching all test case id.

Code: Select all
/**
   * Gets a list of test case id's within a project
   *
   * @param struct $args
   * @param string $args["devKey"]
   * @param int $args["testprojectid"]
   * @return mixed $resultInfo
   *         
   * @access public
   */   
  public function getProjectTestCases($args){    
   $messagePrefix="(" .__FUNCTION__ . ") - ";
       
    $this->_setArgs($args);
    $checkFunctions = array('authenticate','checkTestProjectID');       
    $status_ok=$this->_runChecks($checkFunctions,$messagePrefix);       
 
    if($status_ok)
    {
      $testProjectID = $this->args[self::$testProjectIDParamName];
      $info=$this->tprojectMgr->get_all_testcases_id($testProjectID);
      if( !is_null($info) && count($info) > 0 )
      {
          $info = array_values($info);
      }
      return $info; 
    }
    else
    {
      return $this->errors;
    }
  }


Let me know if this has any issues. :)
zeeshanyshaikh
TestLink user
 
Posts: 3
Joined: Fri Feb 24, 2017 7:40 am
Location: Mumbai, India


Return to XML-RPC API



Who is online

Users browsing this forum: No registered users and 2 guests