|
| 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 = ["Fakespot Shopping Sidebar"] # List of suite names to process |
| 11 | + |
| 12 | +CUSTOM_SUB_TEST_SUITES = [1] # Value to set for the 'custom_sub_test_suites' field |
| 13 | +DRY_RUN = True # If True, only log actions without making changes |
| 14 | + |
| 15 | + |
| 16 | +def get_all_cases_from_suite(tr, project_id, suite_id): |
| 17 | + """ |
| 18 | + Fetch all test cases from a given TestRail suite using pagination. |
| 19 | +
|
| 20 | + Args: |
| 21 | + tr: TestRail API client instance. |
| 22 | + project_id (int): The ID of the TestRail project. |
| 23 | + suite_id (int): The ID of the suite to fetch cases from. |
| 24 | +
|
| 25 | + Returns: |
| 26 | + list: A list of test case dictionaries. |
| 27 | + """ |
| 28 | + all_cases = [] # List to store all fetched test cases |
| 29 | + offset = 0 # Starting point for pagination |
| 30 | + limit = 240 # Maximum number of cases to fetch in each API call |
| 31 | + |
| 32 | + while True: |
| 33 | + # Build the API endpoint with pagination parameters |
| 34 | + endpoint = f"get_cases/{project_id}&suite_id={suite_id}&limit={limit}&offset={offset}" |
| 35 | + response = tr.client.send_get(endpoint) # Send API request to TestRail |
| 36 | + cases = response.get("cases", []) # Extract the list of test cases |
| 37 | + |
| 38 | + if not cases: |
| 39 | + # If no more cases are found, break the loop |
| 40 | + break |
| 41 | + |
| 42 | + # Add the fetched cases to the complete list |
| 43 | + all_cases.extend(cases) |
| 44 | + |
| 45 | + if len(cases) < limit: |
| 46 | + # If the number of fetched cases is less than the limit, we are at the last page |
| 47 | + break |
| 48 | + |
| 49 | + # Increment offset to fetch the next batch of cases |
| 50 | + offset += limit |
| 51 | + |
| 52 | + # Log the total number of fetched cases |
| 53 | + logging.info(f"Total cases fetched from suite {suite_id}: {len(all_cases)}") |
| 54 | + return all_cases |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + # Initialize logging to display info messages |
| 59 | + logging.basicConfig(level=logging.INFO) |
| 60 | + |
| 61 | + # Initialize the TestRail API client |
| 62 | + tr = testrail_init() |
| 63 | + |
| 64 | + # Fetch suite IDs matching the given suite names |
| 65 | + suites = tr.get_suites(PROJECT_ID) |
| 66 | + suite_ids = [suite["id"] for suite in suites if suite["name"] in SUITE_NAMES] |
| 67 | + logging.info(f"Found suites: {suite_ids}") |
| 68 | + |
| 69 | + # Fetch all test cases from the selected suites |
| 70 | + all_cases = [] # List to store all fetched test cases |
| 71 | + for suite_id in suite_ids: |
| 72 | + # Fetch and add cases from each suite |
| 73 | + cases = get_all_cases_from_suite(tr, PROJECT_ID, suite_id) |
| 74 | + all_cases.extend(cases) |
| 75 | + |
| 76 | + # Log the total number of test cases found |
| 77 | + logging.info(f"Total test cases found for selected suites: {len(all_cases)}") |
| 78 | + |
| 79 | + updated_count = 0 # Counter for the number of cases updated |
| 80 | + |
| 81 | + # Iterate over each test case to display and update the 'custom_sub_test_suites' field |
| 82 | + for case in all_cases: |
| 83 | + # Get the current value of the 'custom_sub_test_suites' field |
| 84 | + current_value = case.get("custom_sub_test_suites", "Not Set") |
| 85 | + logging.info(f"Case ID {case['id']} current value: {current_value}") |
| 86 | + |
| 87 | + if not DRY_RUN: |
| 88 | + # If not in dry run mode, perform the update |
| 89 | + result = tr.update_case_field(case["id"], "custom_sub_test_suites", CUSTOM_SUB_TEST_SUITES) |
| 90 | + logging.info(f"Updated case {case['id']} to '{CUSTOM_SUB_TEST_SUITES}', Result: {result}") |
| 91 | + else: |
| 92 | + # In dry run mode, just log the intended change without making it |
| 93 | + logging.info(f"[DRY RUN] Would update case {case['id']} from '{current_value}' to '{CUSTOM_SUB_TEST_SUITES}'.") |
| 94 | + |
| 95 | + updated_count += 1 # Increment the updated count |
| 96 | + |
| 97 | + # Log the total number of cases updated or to be updated |
| 98 | + logging.info(f"Total cases {'updated' if not DRY_RUN else 'to be updated'}: {updated_count}") |
0 commit comments