|
1 |
| -import os |
2 | 1 | import logging
|
| 2 | +from dotenv import load_dotenv |
3 | 3 | from modules.testrail_integration import testrail_init
|
4 | 4 |
|
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() |
9 | 7 |
|
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 |
14 | 13 |
|
15 | 14 |
|
16 | 15 | 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 |
21 | 30 |
|
22 | 31 | 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 | + |
29 | 37 | if not cases:
|
| 38 | + # If no more cases are found, break the loop |
30 | 39 | break
|
| 40 | + |
| 41 | + # Add the fetched cases to the complete list |
31 | 42 | all_cases.extend(cases)
|
32 |
| - # If the number of cases returned is less than the limit, we've reached the last page. |
| 43 | + |
33 | 44 | if len(cases) < limit:
|
| 45 | + # If the number of fetched cases is less than the limit, we are at the last page |
34 | 46 | break
|
| 47 | + |
| 48 | + # Increment offset to fetch the next batch of cases |
35 | 49 | offset += limit
|
36 | 50 |
|
| 51 | + # Log the total number of fetched cases |
37 | 52 | logging.info(f"Total cases fetched from suite {suite_id}: {len(all_cases)}")
|
38 | 53 | return all_cases
|
39 | 54 |
|
40 | 55 |
|
41 | 56 | if __name__ == "__main__":
|
| 57 | + # Initialize logging to display info messages |
42 | 58 | logging.basicConfig(level=logging.INFO)
|
| 59 | + |
| 60 | + # Initialize the TestRail API client |
43 | 61 | tr = testrail_init()
|
44 | 62 |
|
45 |
| - # Get suite IDs for the selected suite names |
| 63 | + # Fetch suite IDs matching the given suite names |
46 | 64 | suites = tr.get_suites(PROJECT_ID)
|
47 |
| - # suite_ids = [suite["id"] for suite in suites] |
48 | 65 | suite_ids = [suite["id"] for suite in suites if suite["name"] in SUITE_NAMES]
|
49 | 66 | logging.info(f"Found suites: {suite_ids}")
|
50 | 67 |
|
51 |
| - all_cases = [] |
| 68 | + # Fetch all test cases from the selected suites |
| 69 | + all_cases = [] # List to store all fetched test cases |
52 | 70 | for suite_id in suite_ids:
|
| 71 | + # Fetch and add cases from each suite |
53 | 72 | cases = get_all_cases_from_suite(tr, PROJECT_ID, suite_id)
|
54 | 73 | all_cases.extend(cases)
|
55 | 74 |
|
| 75 | + # Log the total number of test cases found |
56 | 76 | logging.info(f"Total test cases found for selected suites: {len(all_cases)}")
|
57 | 77 |
|
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 |
63 | 79 |
|
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