Skip to content

Commit c87926f

Browse files
Hani YacoubHani Yacoub
authored andcommitted
Fix script
1 parent a64e20b commit c87926f

File tree

1 file changed

+62
-44
lines changed

1 file changed

+62
-44
lines changed
Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,97 @@
1-
import os
21
import logging
2+
from dotenv import load_dotenv
33
from modules.testrail_integration import testrail_init
44

5-
# Configuration
6-
PROJECT_ID = 17
7-
SUITE_NAMES = ["Downloads"]
8-
CUSTOM_SUB_TEST_SUITES = 1 # "Functional Only"
5+
# Load environment variables from .env file
6+
load_dotenv()
97

10-
# Set TestRail credentials
11-
os.environ["TESTRAIL_BASE_URL"] = "https://mozilla.testrail.io"
12-
os.environ["TESTRAIL_USERNAME"] = ""
13-
os.environ["TESTRAIL_API_KEY"] = ""
8+
# Configuration Constants
9+
PROJECT_ID = 17 # The ID of the TestRail project to work with
10+
SUITE_NAMES = ["Downloads", "Address Bar and Search <136 (to use with late beta) - locked"] # List of suite names to process
11+
CUSTOM_SUB_TEST_SUITES = [1] # Value to set for the 'custom_sub_test_suites' field
12+
DRY_RUN = True # If True, only log actions without making changes
1413

1514

1615
def get_all_cases_from_suite(tr, project_id, suite_id):
17-
"""Fetch all test cases from a suite by handling pagination."""
18-
all_cases = []
19-
offset = 0
20-
limit = 240 # Default limit for TestRail API is 250
16+
"""
17+
Fetch all test cases from a given TestRail suite using pagination.
18+
19+
Args:
20+
tr: TestRail API client instance.
21+
project_id (int): The ID of the TestRail project.
22+
suite_id (int): The ID of the suite to fetch cases from.
23+
24+
Returns:
25+
list: A list of test case dictionaries.
26+
"""
27+
all_cases = [] # List to store all fetched test cases
28+
offset = 0 # Starting point for pagination
29+
limit = 240 # Maximum number of cases to fetch in each API call
2130

2231
while True:
23-
# Build endpoint with pagination parameters
24-
endpoint = (
25-
f"get_cases/{project_id}&suite_id={suite_id}&limit={limit}&offset={offset}"
26-
)
27-
response = tr.client.send_get(endpoint)
28-
cases = response.get("cases", [])
32+
# Build the API endpoint with pagination parameters
33+
endpoint = f"get_cases/{project_id}&suite_id={suite_id}&limit={limit}&offset={offset}"
34+
response = tr.client.send_get(endpoint) # Send API request to TestRail
35+
cases = response.get("cases", []) # Extract the list of test cases
36+
2937
if not cases:
38+
# If no more cases are found, break the loop
3039
break
40+
41+
# Add the fetched cases to the complete list
3142
all_cases.extend(cases)
32-
# If the number of cases returned is less than the limit, we've reached the last page.
43+
3344
if len(cases) < limit:
45+
# If the number of fetched cases is less than the limit, we are at the last page
3446
break
47+
48+
# Increment offset to fetch the next batch of cases
3549
offset += limit
3650

51+
# Log the total number of fetched cases
3752
logging.info(f"Total cases fetched from suite {suite_id}: {len(all_cases)}")
3853
return all_cases
3954

4055

4156
if __name__ == "__main__":
57+
# Initialize logging to display info messages
4258
logging.basicConfig(level=logging.INFO)
59+
60+
# Initialize the TestRail API client
4361
tr = testrail_init()
4462

45-
# Get suite IDs for the selected suite names
63+
# Fetch suite IDs matching the given suite names
4664
suites = tr.get_suites(PROJECT_ID)
47-
# suite_ids = [suite["id"] for suite in suites]
4865
suite_ids = [suite["id"] for suite in suites if suite["name"] in SUITE_NAMES]
4966
logging.info(f"Found suites: {suite_ids}")
5067

51-
all_cases = []
68+
# Fetch all test cases from the selected suites
69+
all_cases = [] # List to store all fetched test cases
5270
for suite_id in suite_ids:
71+
# Fetch and add cases from each suite
5372
cases = get_all_cases_from_suite(tr, PROJECT_ID, suite_id)
5473
all_cases.extend(cases)
5574

75+
# Log the total number of test cases found
5676
logging.info(f"Total test cases found for selected suites: {len(all_cases)}")
5777

58-
for case in all_cases:
59-
result = tr.update_case_field(case["id"], "custom_sub_tests_suites", CUSTOM_SUB_TEST_SUITES)
60-
logging.debug(f"Updated case {case['id']} result: {result}")
61-
62-
logging.info("All applicable test cases updated to 'Functional Only' successfully!")
78+
updated_count = 0 # Counter for the number of cases updated
6379

64-
# Re-verify that all cases have been updated correctly
65-
logging.info("Verifying that all test cases were updated correctly...")
66-
failed_updates = []
67-
68-
for suite_id in suite_ids:
69-
updated_cases = get_all_cases_from_suite(tr, PROJECT_ID, suite_id)
70-
for case in updated_cases:
71-
if case.get("custom_sub_tests_suites") != CUSTOM_SUB_TEST_SUITES:
72-
failed_updates.append(case["id"])
73-
74-
if failed_updates:
75-
logging.warning(f"The following case IDs were not updated correctly: {failed_updates}")
76-
logging.warning(f"Number of failed updates: {len(failed_updates)}")
77-
78-
else:
79-
logging.info("All test cases have the correct 'custom_sub_tests_suites' value.")
80+
# Iterate over each test case to display and update the 'custom_sub_test_suites' field
81+
for case in all_cases:
82+
# Get the current value of the 'custom_sub_test_suites' field
83+
current_value = case.get("custom_sub_test_suites", "Not Set")
84+
logging.info(f"Case ID {case['id']} current value: {current_value}")
85+
86+
if not DRY_RUN:
87+
# If not in dry run mode, perform the update
88+
result = tr.update_case_field(case["id"], "custom_sub_test_suites", CUSTOM_SUB_TEST_SUITES)
89+
logging.info(f"Updated case {case['id']} to '{CUSTOM_SUB_TEST_SUITES}', Result: {result}")
90+
else:
91+
# In dry run mode, just log the intended change without making it
92+
logging.info(f"[DRY RUN] Would update case {case['id']} from '{current_value}' to '{CUSTOM_SUB_TEST_SUITES}'.")
93+
94+
updated_count += 1 # Increment the updated count
95+
96+
# Log the total number of cases updated or to be updated
97+
logging.info(f"Total cases {'updated' if not DRY_RUN else 'to be updated'}: {updated_count}")

0 commit comments

Comments
 (0)