def parse_durations_file(file_path):
    """Parse a durations file to extract test timing information."""
    slowest_tests = []
    try:
        durations_file = file_path.replace("_stats.txt", "_durations.txt")
        if os.path.exists(durations_file):
            with open(durations_file, "r") as f:
                content = f.read()

                # Skip the header line
                for line in content.split("\n")[1:]:
                    if line.strip():
                        # Format is typically: 10.37s call     tests/path/to/test.py::TestClass::test_method
                        parts = line.strip().split()
                        if len(parts) >= 3:
                            time_str = parts[0]
                            test_path = " ".join(parts[2:])

                            # Skip entries with "< 0.05 secs were omitted" or similar
                            if "secs were omitted" in test_path:
                                continue

                            try:
                                time_seconds = float(time_str.rstrip("s"))
                                slowest_tests.append({"test": test_path, "duration": time_seconds})
                            except ValueError:
                                # If time_str is not a valid float, it might be a different format
                                # For example, some pytest formats show "< 0.05s" or similar
                                if test_path.startswith("<") and "secs were omitted" in test_path:
                                    # Extract the time value from test_path if it's in the format "< 0.05 secs were omitted"
                                    try:
                                        # This handles entries where the time is in the test_path itself
                                        dur_match = re.search(r"(\d+(?:\.\d+)?)", test_path)
                                        if dur_match:
                                            time_seconds = float(dur_match.group(1))
                                            slowest_tests.append({"test": test_path, "duration": time_seconds})
                                    except ValueError:
                                        pass
    except Exception as e:
        print(f"Error parsing durations file {file_path.replace('_stats.txt', '_durations.txt')}: {e}")

    return slowest_tests
Parse a durations file to extract test timing information.
utils/consolidated_test_report.py