|
| 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 (like API credentials) |
| 6 | +load_dotenv() |
| 7 | + |
| 8 | +# Configuration Constants |
| 9 | +PROJECT_ID = 17 # ID of the TestRail project to work with |
| 10 | +SUITE_NAMES = [ |
| 11 | + "Address Bar and Search", |
| 12 | + "Audio/Video", |
| 13 | + "Bookmarks and History", |
| 14 | + "Downloads", |
| 15 | + "Drag and Drop", |
| 16 | + "Find Toolbar", |
| 17 | + "Form Autofill", |
| 18 | + "Geolocation", |
| 19 | + "Language Packs", |
| 20 | + "Menus", |
| 21 | + "Networking", |
| 22 | + "Notifications", |
| 23 | + "PDF Viewer", |
| 24 | + "Password Manager", |
| 25 | + "Pocket", |
| 26 | + "Preferences", |
| 27 | + "Profile", |
| 28 | + "Printing UI", |
| 29 | + "Reader View", |
| 30 | + "Scrolling", |
| 31 | + "Security and Privacy", |
| 32 | + "Sync & Firefox Account", |
| 33 | + "Tabs", |
| 34 | + "Theme and Toolbar", |
| 35 | +] # Only process suites with this name |
| 36 | + |
| 37 | +CUSTOM_SUB_TEST_SUITES = [2] # Value to set for 'custom_sub_test_suites' field (e.g. 'Smoke') |
| 38 | +DRY_RUN = True # If True, simulate updates without sending changes |
| 39 | + |
| 40 | + |
| 41 | +def get_all_cases_from_suite(tr, project_id, suite_id): |
| 42 | + """ |
| 43 | + Fetch all test cases from a given TestRail suite using pagination. |
| 44 | +
|
| 45 | + Args: |
| 46 | + tr: TestRail API client instance. |
| 47 | + project_id (int): The TestRail project ID. |
| 48 | + suite_id (int): The specific suite ID within the project. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + list: All test cases as a list of dictionaries. |
| 52 | + """ |
| 53 | + all_cases = [] # List to hold all fetched cases |
| 54 | + offset = 0 # Pagination offset |
| 55 | + limit = 240 # API fetch limit per request |
| 56 | + |
| 57 | + while True: |
| 58 | + # Build API URL with pagination |
| 59 | + endpoint = f"get_cases/{project_id}&suite_id={suite_id}&limit={limit}&offset={offset}" |
| 60 | + response = tr.client.send_get(endpoint) |
| 61 | + cases = response.get("cases", []) |
| 62 | + |
| 63 | + if not cases: |
| 64 | + # Break loop when no more cases are returned |
| 65 | + break |
| 66 | + |
| 67 | + all_cases.extend(cases) |
| 68 | + |
| 69 | + # If we receive less than the limit, it means we've reached the last page |
| 70 | + if len(cases) < limit: |
| 71 | + break |
| 72 | + |
| 73 | + offset += limit # Move to next page |
| 74 | + |
| 75 | + logging.info(f"Total cases fetched from suite {suite_id}: {len(all_cases)}") |
| 76 | + return all_cases |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + logging.basicConfig(level=logging.INFO) # Enable info-level logging |
| 81 | + tr = testrail_init() # Initialize the TestRail client |
| 82 | + |
| 83 | + # Get all suites for the project and filter the ones we need |
| 84 | + suites = tr.get_suites(PROJECT_ID) |
| 85 | + suite_ids = [suite["id"] for suite in suites if suite["name"] in SUITE_NAMES] |
| 86 | + logging.info(f"Found suites: {suite_ids}") |
| 87 | + |
| 88 | + # Gather all test cases from selected suites |
| 89 | + all_cases = [] |
| 90 | + for suite_id in suite_ids: |
| 91 | + cases = get_all_cases_from_suite(tr, PROJECT_ID, suite_id) |
| 92 | + all_cases.extend(cases) |
| 93 | + |
| 94 | + logging.info(f"Total test cases found for selected suites: {len(all_cases)}") |
| 95 | + |
| 96 | + updated_count = 0 # Track how many cases we updated |
| 97 | + |
| 98 | + # Iterate over all test cases |
| 99 | + for case in all_cases: |
| 100 | + case_id = case["id"] |
| 101 | + automation_status = case.get("custom_automation_status") # Read the automation status field |
| 102 | + current_custom_value = case.get("custom_sub_test_suites", "Not Set") # Read current custom field value |
| 103 | + |
| 104 | + logging.info(f"Case ID {case_id} custom_automation_status: {automation_status}") |
| 105 | + |
| 106 | + # Only proceed if the custom automation status equals 4 (4 = Completed) |
| 107 | + if automation_status != 4: |
| 108 | + logging.info(f"Skipping case {case_id} – custom_automation_status = {automation_status}") |
| 109 | + continue |
| 110 | + |
| 111 | + logging.info( |
| 112 | + f"Case ID {case_id} matches (custom_automation_status == 4). " |
| 113 | + f"Current custom_sub_test_suites: {current_custom_value}" |
| 114 | + ) |
| 115 | + |
| 116 | + # Update the field if not in dry run mode |
| 117 | + if not DRY_RUN: |
| 118 | + result = tr.update_case_field(case_id, "custom_sub_test_suites", [2]) |
| 119 | + logging.info(f"Updated case {case_id} to '[2]', Result: {result}") |
| 120 | + else: |
| 121 | + logging.info(f"[DRY RUN] Would update case {case_id} from '{current_custom_value}' to '[2]'.") |
| 122 | + |
| 123 | + updated_count += 1 # Count how many updates we made (or would have made) |
| 124 | + |
| 125 | + logging.info(f"Total cases {'updated' if not DRY_RUN else 'to be updated'}: {updated_count}") |
0 commit comments