|
| 1 | +import logging |
| 2 | +from dotenv import load_dotenv |
| 3 | +from modules.testrail_integration import testrail_init |
| 4 | + |
| 5 | +# Load environment variables from .env file |
| 6 | +load_dotenv() |
| 7 | + |
| 8 | +# Configuration Constants |
| 9 | +PROJECT_ID = 17 # The ID of the TestRail project to work with |
| 10 | +SUITE_NAMES = ["PDF Viewer"] # List of suite names to process |
| 11 | + |
| 12 | +CUSTOM_SUB_TEST_SUITES = [2] # Value to set for the 'custom_sub_test_suites' field |
| 13 | +TEST_SUB_SUITES = { |
| 14 | + 1: "functional", |
| 15 | + 2: "smoke", |
| 16 | + 3: "accessibility", |
| 17 | + 4: "l10n", |
| 18 | + 5: "security", |
| 19 | + 6: "fxa", |
| 20 | + 7: "other" |
| 21 | +} |
| 22 | +updated_count = 0 |
| 23 | +DRY_RUN = True # If True, only log actions without making changes |
| 24 | + |
| 25 | + |
| 26 | +def get_all_cases_from_suite(tr, project_id, suite_id): |
| 27 | + """ |
| 28 | + Fetch all test cases from a given TestRail suite using pagination. |
| 29 | +
|
| 30 | + Args: |
| 31 | + tr: TestRail API client instance. |
| 32 | + project_id (int): The ID of the TestRail project. |
| 33 | + suite_id (int): The ID of the suite to fetch cases from. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + list: A list of test case dictionaries. |
| 37 | + """ |
| 38 | + all_cases = [] # List to store all fetched test cases |
| 39 | + offset = 0 # Starting point for pagination |
| 40 | + limit = 240 # Maximum number of cases to fetch in each API call |
| 41 | + |
| 42 | + while True: |
| 43 | + # Build the API endpoint with pagination parameters |
| 44 | + endpoint = f"get_cases/{project_id}&suite_id={suite_id}&limit={limit}&offset={offset}" |
| 45 | + response = tr.client.send_get(endpoint) # Send API request to TestRail |
| 46 | + cases = response.get("cases", []) # Extract the list of test cases |
| 47 | + |
| 48 | + if not cases: |
| 49 | + # If no more cases are found, break the loop |
| 50 | + break |
| 51 | + |
| 52 | + # Add the fetched cases to the complete list |
| 53 | + all_cases.extend(cases) |
| 54 | + |
| 55 | + if len(cases) < limit: |
| 56 | + # If the number of fetched cases is less than the limit, we are at the last page |
| 57 | + break |
| 58 | + |
| 59 | + # Increment offset to fetch the next batch of cases |
| 60 | + offset += limit |
| 61 | + |
| 62 | + # Log the total number of fetched cases |
| 63 | + logging.info(f"Total cases fetched from suite {suite_id}: {len(all_cases)}") |
| 64 | + return all_cases |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + # Initialize logging to display info messages |
| 69 | + logging.basicConfig(level=logging.INFO) |
| 70 | + |
| 71 | + # Initialize the TestRail API client |
| 72 | + tr = testrail_init() |
| 73 | + |
| 74 | + # Fetch suite IDs matching the given suite names |
| 75 | + suites = tr.get_suites(PROJECT_ID) |
| 76 | + suite_ids = [suite["id"] for suite in suites if suite["name"] in SUITE_NAMES] |
| 77 | + logging.info(f"Found suites: {suite_ids}") |
| 78 | + |
| 79 | + # Fetch all test cases from the selected suites |
| 80 | + all_cases = [] # List to store all fetched test cases |
| 81 | + for suite_id in suite_ids: |
| 82 | + # Fetch and add cases from each suite |
| 83 | + cases = get_all_cases_from_suite(tr, PROJECT_ID, suite_id) |
| 84 | + all_cases.extend(cases) |
| 85 | + |
| 86 | + # Log the total number of test cases found |
| 87 | + logging.info(f"Total test cases found for selected suites: {len(all_cases)}") |
| 88 | + |
| 89 | + updated_count = 0 # Counter for the number of cases updated |
| 90 | + |
| 91 | + # Iterate through all cases |
| 92 | + for case in all_cases: |
| 93 | + case_id = case["id"] |
| 94 | + sub_test_suites_value = case.get("sub_test_suites", []) |
| 95 | + |
| 96 | + logging.info(f"Case ID {case_id} sub_test_suites: {sub_test_suites_value}") |
| 97 | + |
| 98 | + case_id = case["id"] |
| 99 | + sub_test_suites_value = case.get("sub_test_suites", []) # Fallback to empty list |
| 100 | + custom_value = case.get("custom_sub_test_suites", "Not Set") |
| 101 | + |
| 102 | + # Check if 2 is present in sub_test_suites |
| 103 | + if 2 not in sub_test_suites_value: |
| 104 | + logging.info(f"Skipping case {case_id} – sub_test_suites = {sub_test_suites_value}") |
| 105 | + continue |
| 106 | + |
| 107 | + logging.info( |
| 108 | + f"Case ID {case_id} matches (contains 2 in sub_test_suites). Current custom_sub_test_suites: {custom_value}") |
| 109 | + |
| 110 | + if not DRY_RUN: |
| 111 | + # Perform the update |
| 112 | + result = tr.update_case_field(case_id, "custom_sub_test_suites", CUSTOM_SUB_TEST_SUITES) |
| 113 | + logging.info(f"Updated case {case_id} to '{CUSTOM_SUB_TEST_SUITES}', Result: {result}") |
| 114 | + else: |
| 115 | + logging.info(f"[DRY RUN] Would update case {case_id} from '{custom_value}' to '{CUSTOM_SUB_TEST_SUITES}'.") |
| 116 | + |
| 117 | + updated_count += 1 |
| 118 | + |
| 119 | + logging.info(f"Total cases {'updated' if not DRY_RUN else 'to be updated'}: {updated_count}") |
0 commit comments