-
Notifications
You must be signed in to change notification settings - Fork 0
/
PTFTestRail.py
114 lines (104 loc) · 4.45 KB
/
PTFTestRail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import testrail
import config_reader_ip as CF
#get the test rail client object
def get_testrail_client():
# Get the TestRail Url
testrail_url = CF.get('Default_TestRail', 'TESTRAIL_URL')
client = testrail.APIClient(testrail_url)
# Get and set the TestRail User and Password
client.user = CF.get('Default_TestRail', 'TESTRAIL_USER')
client.password = CF.get('Default_TestRail', 'TESTRAIL_PASSWORD')
return client
#Adds a new test result,status, comment or assigns a test (for a test run and case combination).
def update_test_result(run_id, case_id,result_flag,comments):
# Create the client objects
client = get_testrail_client()
#"Update TestRail for a given run_id and case_id"
update_flag = False
# 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
if run_id is not None:
try:
result = client.send_post(
'add_result_for_case/{}/{}'.format(run_id, case_id),
{'status_id': int(status_id), 'comment': str(comments)}
)
print(result)
except OSError as err:
print("OS error: {0}".format(err))
else:
print('Updated test result for case: %s in test run: %s with msg:%s' % (case_id, run_id, comments))
return update_flag
def add_results(run_id, result_flag,comments):
# Create the client objects
client = get_testrail_client()
#"Update TestRail for a given run_id"
update_flag = False
# Update the result in TestRail using send_post function.
# Parameters for add_result_for_case is the runid.
# 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
if run_id is not None:
try:
result = client.send_post(
'add_results/{}'.format(run_id),
{'status_id': int(status_id), 'comment': str(comments)}
)
print(result)
except OSError as err:
print("OS error: {0}".format(err))
else:
print('Updated test results for test run: %s with msg:%s' % (run_id, comments))
return update_flag
#Attaching the file in created test results
def update_testresult_attachement(result_id,attachements_ptha):
# Create the client objects
client = get_testrail_client()
update_flag = False
# Update the result in TestRail using send_post function.
# Parameters for add_result_for_case is the runid.
# status_id is 1 for Passed, 2 For Blocked, 4 for Retest and 5 for Failed
if result_id is not None:
try:
client.send_post('add_attachment_to_result/{}'.format(result_id), attachements_ptha)
except (FileNotFoundError, IOError):
print("Wrong file or file path")
else:
print('Attachements added for: %s in existing test run' %(result_id))
return update_flag
#Fetch the test case details from test rail and retuns in a key value paire
def get_test_data(case_id):
# Create the client objects
client = get_testrail_client()
tc_case_info = client.send_get('get_case/{}'.format(case_id))
return tc_case_info
#"Get the project ID using project name"
def get_project_id(project_name):
# Create the client objects
client = get_testrail_client()
project_id=None
projects = client.send_get('get_projects')
project_name_list = [name['name'] for name in projects]
project_id_list = [id['id'] for id in projects]
for i in range(len(project_name_list)):
if project_name_list[i] == project_name:
project_id = project_id_list[i]
break
return project_id
def get_run_id(test_run_name,project_id):
"Get the run ID using test name and project name"
run_id=None
# Create the client objects
client = get_testrail_client()
try:
test_runs = client.send_get('get_runs/%s'%(project_id))
except OSError as err:
print("OS error: {0}".format(err))
else:
for test_run in test_runs:
if test_run['name'] == test_run_name:
run_id = test_run['id']
break
return run_id