mirror of
https://github.com/Telecominfraproject/wlan-testing.git
synced 2025-11-03 04:18:15 +00:00
fixtures pulled as a test case
Signed-off-by: shivamcandela <shivam.thakur@candelatech.com>
This commit is contained in:
@@ -15,19 +15,20 @@ import requests
|
||||
from pprint import pprint
|
||||
import os
|
||||
|
||||
class TestRail_Client:
|
||||
def __init__(self, command_line_args):
|
||||
self.user = command_line_args.testrail_user_id
|
||||
self.password = command_line_args.testrail_user_password
|
||||
self.command_line_args = command_line_args
|
||||
base_url = command_line_args.testrail_base_url
|
||||
|
||||
# tr_user=os.getenv('TR_USER')
|
||||
# tr_pw=os.getenv('TR_PWD')
|
||||
# project = os.getenv('PROJECT_ID')
|
||||
|
||||
|
||||
class APIClient:
|
||||
def __init__(self, base_url, tr_user, tr_pw, project):
|
||||
self.user = tr_user
|
||||
self.password = tr_pw
|
||||
self.project = project
|
||||
if not base_url.endswith('/'):
|
||||
base_url += '/'
|
||||
self.__url = base_url + 'index.php?/api/v2/'
|
||||
self.use_testrails = True
|
||||
if command_line_args.testrail_user_id == "NONE":
|
||||
self.use_testrails = False
|
||||
|
||||
|
||||
def send_get(self, uri, filepath=None):
|
||||
"""Issue a GET request (read) against the API.
|
||||
@@ -57,9 +58,6 @@ class TestRail_Client:
|
||||
return self.__send_request('POST', uri, data)
|
||||
|
||||
def __send_request(self, method, uri, data):
|
||||
if not self.use_testrails:
|
||||
return {"TESTRAILS":"DISABLED"}
|
||||
|
||||
url = self.__url + uri
|
||||
|
||||
auth = str(
|
||||
@@ -69,10 +67,10 @@ class TestRail_Client:
|
||||
'ascii'
|
||||
).strip()
|
||||
headers = {'Authorization': 'Basic ' + auth}
|
||||
#print("Method =" , method)
|
||||
# print("Method =" , method)
|
||||
|
||||
if method == 'POST':
|
||||
if uri[:14] == 'add_attachment': # add_attachment API method
|
||||
if uri[:14] == 'add_attachment': # add_attachment API method
|
||||
files = {'attachment': (open(data, 'rb'))}
|
||||
response = requests.post(url, headers=headers, files=files)
|
||||
files['attachment'].close()
|
||||
@@ -83,25 +81,25 @@ class TestRail_Client:
|
||||
else:
|
||||
headers['Content-Type'] = 'application/json'
|
||||
response = requests.get(url, headers=headers)
|
||||
#print("headers = ", headers)
|
||||
#print("resonse=", response)
|
||||
#print("response code =", response.status_code)
|
||||
# print("headers = ", headers)
|
||||
# print("resonse=", response)
|
||||
# print("response code =", response.status_code)
|
||||
|
||||
if response.status_code > 201:
|
||||
|
||||
try:
|
||||
error = response.json()
|
||||
except: # response.content not formatted as JSON
|
||||
except: # response.content not formatted as JSON
|
||||
error = str(response.content)
|
||||
#raise APIError('TestRail API returned HTTP %s (%s)' % (response.status_code, error))
|
||||
# raise APIError('TestRail API returned HTTP %s (%s)' % (response.status_code, error))
|
||||
print('TestRail API returned HTTP %s (%s)' % (response.status_code, error))
|
||||
return
|
||||
else:
|
||||
print(uri[:15])
|
||||
if uri[:15] == 'get_attachments': # Expecting file, not JSON
|
||||
if uri[:15] == 'get_attachments': # Expecting file, not JSON
|
||||
try:
|
||||
print('opening file')
|
||||
print (str(response.content))
|
||||
print(str(response.content))
|
||||
open(data, 'wb').write(response.content)
|
||||
print('opened file')
|
||||
return (data)
|
||||
@@ -111,39 +109,31 @@ class TestRail_Client:
|
||||
|
||||
try:
|
||||
return response.json()
|
||||
except: # Nothing to return
|
||||
except: # Nothing to return
|
||||
return {}
|
||||
|
||||
def get_project_id(self, project_name):
|
||||
"Get the project ID using project name"
|
||||
|
||||
if not self.use_testrails:
|
||||
return -1
|
||||
|
||||
project_id = None
|
||||
projects = self.send_get('get_projects')
|
||||
##pprint(projects)
|
||||
for project in projects:
|
||||
if project['name']== project_name:
|
||||
if project['name'] == project_name:
|
||||
project_id = project['id']
|
||||
# project_found_flag=True
|
||||
break
|
||||
print("project Id =",project_id)
|
||||
print("project Id =", project_id)
|
||||
return project_id
|
||||
|
||||
def get_run_id(self, test_run_name):
|
||||
"Get the run ID using test name and project name"
|
||||
|
||||
if not self.use_testrails:
|
||||
return -1
|
||||
|
||||
run_id = None
|
||||
project_id = self.get_project_id(project_name=project)
|
||||
project_id = self.get_project_id(project_name=self.project)
|
||||
|
||||
try:
|
||||
test_runs = self.send_get('get_runs/%s' % (project_id))
|
||||
#print("------------TEST RUNS----------")
|
||||
#pprint(test_runs)
|
||||
# print("------------TEST RUNS----------")
|
||||
# pprint(test_runs)
|
||||
|
||||
except Exception:
|
||||
print
|
||||
@@ -153,23 +143,18 @@ class TestRail_Client:
|
||||
for test_run in test_runs:
|
||||
if test_run['name'] == test_run_name:
|
||||
run_id = test_run['id']
|
||||
#print("run Id in Test Runs=",run_id)
|
||||
# print("run Id in Test Runs=",run_id)
|
||||
break
|
||||
return run_id
|
||||
|
||||
|
||||
def update_testrail(self, case_id, run_id, status_id, msg):
|
||||
"Update TestRail for a given run_id and case_id"
|
||||
|
||||
if not self.use_testrails:
|
||||
return False
|
||||
|
||||
update_flag = False
|
||||
# Get the TestRail client account details
|
||||
# Update the result in TestRail using send_post function.
|
||||
# Parameters for add_result_for_case is the combination of runid and case id.
|
||||
# status_id is 1 for Passed, 2 For Blocked, 4 for Retest and 5 for Failed
|
||||
#status_id = 1 if result_flag is True else 5
|
||||
# status_id = 1 if result_flag is True else 5
|
||||
|
||||
print("result status Pass/Fail = ", status_id)
|
||||
print("case id=", case_id)
|
||||
@@ -179,7 +164,7 @@ class TestRail_Client:
|
||||
result = self.send_post(
|
||||
'add_result_for_case/%s/%s' % (run_id, case_id),
|
||||
{'status_id': status_id, 'comment': msg})
|
||||
print("result in post",result)
|
||||
print("result in post", result)
|
||||
except Exception:
|
||||
print
|
||||
'Exception in update_testrail() updating TestRail.'
|
||||
@@ -193,8 +178,19 @@ class TestRail_Client:
|
||||
def create_testrun(self, name, case_ids, project_id, milestone_id, description):
|
||||
result = self.send_post(
|
||||
'add_run/%s' % (project_id),
|
||||
{'name': name, 'case_ids': case_ids, 'milestone_id': milestone_id, 'description': description, 'include_all': False})
|
||||
{'name': name, 'case_ids': case_ids, 'milestone_id': milestone_id, 'description': description,
|
||||
'include_all': False})
|
||||
print("result in post", result)
|
||||
|
||||
def update_testrun(self, runid, description):
|
||||
result = self.send_post(
|
||||
'update_run/%s' % (runid),
|
||||
{'description': description})
|
||||
print("result in post", result)
|
||||
|
||||
|
||||
# client: APIClient = APIClient(os.getenv('TR_URL'))
|
||||
|
||||
|
||||
class APIError(Exception):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user