|
4 | 4 |
|
5 | 5 | import glob |
6 | 6 | from os.path import dirname, basename, join |
| 7 | +import traceback |
7 | 8 |
|
8 | 9 | import sys |
9 | 10 |
|
|
16 | 17 |
|
17 | 18 | set_date_format(DateFormatTypes.UK_LONG) |
18 | 19 |
|
19 | | -# I put this here to get the library loaded and header printed before loop |
20 | 20 |
|
21 | | -print("Looking in folder:", dirname(__file__)) |
22 | | -modules = sorted(glob.glob(join(dirname(__file__), "Test*.py"))) |
23 | | -num_modules = len(modules) |
| 21 | +def main(start_index=0, end_index=None): |
| 22 | + """Loop over test cases""" |
24 | 23 |
|
25 | | -""" This is the index of the file - change this to start later in the list """ |
26 | | -N = 0 |
27 | | -M = num_modules |
| 24 | + # I put this here to get the library loaded and header printed before loop |
| 25 | + test_folder = dirname(__file__) |
| 26 | + print("Looking in folder:", test_folder) |
| 27 | + modules = sorted(glob.glob(join(test_folder, "Test*.py"))) |
| 28 | + num_modules = len(modules) |
28 | 29 |
|
29 | | -############################################################################### |
| 30 | + if end_index is None or end_index > num_modules: |
| 31 | + end_index = num_modules |
30 | 32 |
|
31 | | -for module_file_name in modules[N : M + 1]: |
| 33 | + for idx in range(start_index, end_index): |
32 | 34 |
|
33 | | - try: |
| 35 | + module_path = modules[idx] |
| 36 | + module_name = basename(module_path)[:-3] |
34 | 37 |
|
35 | | - module_text_name = basename(module_file_name[:-3]) |
36 | 38 | print( |
37 | | - "TEST: %3d out of %3d: MODULE: %-35s " |
38 | | - % (N + 1, num_modules, module_text_name), |
| 39 | + f"TEST: {idx + 1:3d} out of {num_modules:3d}: MODULE: {module_name:<35} ", |
39 | 40 | end="", |
40 | 41 | ) |
41 | | - module_name = __import__(module_text_name) |
42 | | - num_errors = module_name.test_cases._global_num_errors |
43 | | - num_warnings = module_name.test_cases._global_num_warnings |
44 | 42 |
|
45 | | - print( |
46 | | - "WARNINGS: %3d ERRORS: %3d " % (num_warnings, num_errors), end="" |
47 | | - ) |
| 43 | + try: |
| 44 | + module = __import__(module_name) |
| 45 | + |
| 46 | + num_errors = getattr(module.test_cases, "_global_num_errors", 0) |
| 47 | + num_warnings = getattr(module.test_cases, "_global_num_warnings", 0) |
| 48 | + |
| 49 | + print(f"WARNINGS: {num_warnings:3d} ERRORS: {num_errors:3d} ", end="") |
| 50 | + |
| 51 | + if num_errors > 0: |
| 52 | + print("*" * num_errors, end="") |
| 53 | + |
| 54 | + print() |
| 55 | + |
| 56 | + except (FinError, ValueError, NameError, TypeError) as e: |
| 57 | + # Handle known financepy or Python errors gracefully |
| 58 | + print(f"{type(e).__name__}: {e} ************") |
| 59 | + except Exception: |
| 60 | + # Catch all other unexpected errors, print traceback for debugging |
| 61 | + print("Unexpected error:") |
| 62 | + traceback.print_exc() |
| 63 | + # Continue to next test module regardless of errors |
| 64 | + |
48 | 65 |
|
49 | | - if num_errors > 0: |
50 | | - for i in range(0, num_errors): |
51 | | - print("*", end="") |
52 | | - |
53 | | - print("") |
54 | | - N = N + 1 |
55 | | - |
56 | | - # Want testing to continue even if a module has an exception |
57 | | - except FinError as err: |
58 | | - print("FinError:", err._message, "************") |
59 | | - N = N + 1 |
60 | | - except ValueError as err: |
61 | | - print("Value Error:", err.args[0], "************") |
62 | | - N = N + 1 |
63 | | - except NameError as err: |
64 | | - print("Name Error:", err.args[0], "************") |
65 | | - N = N + 1 |
66 | | - except TypeError as err: |
67 | | - print("Type Error:", err.args[0], "************") |
68 | | - N = N + 1 |
69 | | - except Exception: |
70 | | - print("Unexpected error:", sys.exc_info()[0]) |
71 | | - N = N + 1 |
| 66 | +if __name__ == "__main__": |
| 67 | + # Optionally customize start and end test indices here |
| 68 | + main() |
72 | 69 |
|
73 | 70 | ############################################################################### |
0 commit comments