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: 3007
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: 3007
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, $tcid = []);
      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

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

Postby sander_pool » Thu Nov 30, 2017 8:33 pm

It is interesting that no API method exists to get a list of all test cases that are in a project. I realize that generally you access test cases in the context of a test plan but there are certainly use cases to just get all the cases in a project. At the moment I am evaluating the scalability of the latest version of TL. To do this I'm writing a Python script that creates an artificial data set of test cases, plans, builds, platforms and results. I can then assess if the reporting still works. This was a problem for me a few years ago with an earlier version of TL and before committing to TL now I need to explore this aspect.

Anyway, for this script it would be convenient to have a method that gets all the test cases in a project so I can automatically add them to multiple plans with multiple platforms. I'm glad I found this thread so I don't have to look for this API call :)
Principal QA Automation Engineer at Somewhere
sander_pool
TestLink user
 
Posts: 12
Joined: Thu May 10, 2012 8:17 pm
Location: CT - USA

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

Postby sander_pool » Thu Nov 30, 2017 8:58 pm

This is some example code on how to get all the test cases in all the projects using the Python API. Not a big deal to loop through the top level suites and concatenate all the test cases into a single list.

Code: Select all
import testlink

tls = testlink.TestLinkHelper().connect(testlink.TestlinkAPIClient)
projs = tls.getProjects()
for proj in projs:
    print('Project:' + str(proj))
    suites = tls.getFirstLevelTestSuitesForTestProject(proj['id'])
    for suite in suites:
        print("Suite: " + str(suite))
        for tc in tls.getTestCasesForTestSuite(testsuiteid=suite['id'], deep=True, details=False):
            print("Test case: " + str(tc))
Principal QA Automation Engineer at Somewhere
sander_pool
TestLink user
 
Posts: 12
Joined: Thu May 10, 2012 8:17 pm
Location: CT - USA


Return to XML-RPC API



Who is online

Users browsing this forum: No registered users and 2 guests