Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Trigger Jenkins build with parameter RunID from testrail


#1

Hi guys! first of all many thanks for the past and future support! you are all a great community

Well heres the thing, i am trying to automate the test runs triggers from Test Rail using the “Start Test” button from the test runs created.

Already set up the UI and the Trigger.php but it seems but when i press the run test button an error is shown and the jenkins job is not triggered.

  1. I have parameterized “RunId” as a parammeter build in my jenkins config

  2. Tested the trigger URL using postman and works fine, the URL is something like:

http://jenkins./job//buildWithParameters?token=RANDOMETOKEN&RunId=

  1. I have set the trigger.php with : the following code:

`

define(‘TESTRAIL_API_ADDRESS’, ‘’);

define(‘TESTRAIL_API_USER’, ‘< User >’);

define(‘TESTRAIL_API_PASSWORD’, ‘< password>’);

//function execute_test($run_id, $case_id, $test_id)

function execute_test($run_id)

{

$resourceUrl = ‘http://jenkins./job//buildWithParameters?token=RANDOMETOKEN&RunId=’ . $run_id;

//$resourceExists = false;

$ch = curl_init($resourceUrl);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

//201 = OK
if($statusCode == '201'){
return 7;
} else { return 5;}

}
@set_time_limit(0);
run_id = null; if (isset(_GET[‘run_id’]))
{
if (preg_match(’/^[0-9]+/', _GET[‘run_id’]))
{
run_id = (int) _GET[‘run_id’];
}
}
if (!$run_id)
{
throw new TestRailException(
‘Run ID not available, exiting immediately’
);
}

$api = new TestRail_api(
TESTRAIL_API_ADDRESS,
TESTRAIL_API_USER,
TESTRAIL_API_PASSWORD
);

$status_id = execute_test($run_id);

$tests = $api->send_command(‘GET’, ‘get_tests/’ . $run_id);
foreach ($tests as $test)
{
//$status_id = execute_test($run_id, $test->case_id, $test->id);
if ($status_id)
{
$api->send_command(
‘POST’,
‘add_result/’ .
$test->id,
array(
‘status_id’ => $status_id
)

  );

}
}
class TestRail_api
{
private _address; private _userpwd;
public function __construct($address, $user, $password)
{
$this->_address = rtrim($address, ‘/’) . ‘/’;
$this->_user = $user;
$this->_password = $password;
$this->_userpwd = “$user:$password”;
}
private function _send_request($method, $url, $data,
&$status_code)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($method == ‘POST’)
{
if ($data)
{
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
json_encode($data)
);
}
else
{
$headers[‘Content-Length’] = ‘0’;
curl_setopt($ch, CURLOPT_POSTFIELDS, ‘’);
}
}
else
{
curl_setopt($ch, CURLOPT_POST, false);
}
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
‘Expect:’,
‘Content-Type: application/json’
)
);
curl_setopt($ch, CURLOPT_USERPWD, “$this->_userpwd”);
$data = curl_exec($ch);
if ($data === false)
{
throw new TestRailException(curl_error($ch));
}
$info = curl_getinfo($ch);
curl_close($ch);
$status_code = $info[‘http_code’];
return $data;
}
public function send_command($method, $uri, $data = null)
{
$url = $this->_address . “index.php?api/v2/$uri”;
$response = $this->_send_request($method, $url, $data,
$status_code);
$obj = null;
if ($response && is_string($response))
{
$obj = json_decode($response);
}
if ($status_code != 200 && $status_code != 201)
{
throw new TestRailException(
'HTTP status code: ’ . $status_code
);
}
return $obj;
}
}
class TestRailException extends Exception
{
}`

But for some reason Jenkins is not triggering the job, any advise? thanks


#3

Hi Agustin,

Could you let us know what the exact error message is? Depending on what is generating the error message we may be able to assist further. But please understand that we don’t have a professional services team review client code in detail.