Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief :epkg:`pytest` is sometimes slow. This file provides helpers to
4easily run test function.
5"""
6import os
7import importlib
8import re
9import warnings
10import time
11from traceback import format_exc
12from inspect import signature
15class TestExecutionError(RuntimeError):
16 """
17 Raised when the execution of a test fails.
18 """
20 def __init__(self, module, name, exc):
21 """
22 @param module module
23 @param name function name
24 @param exc exception
25 """
26 if name is None and isinstance(exc, list):
27 msg = "Test module '{}' failed\n---\n{}".format(
28 module.__name__, "\n---\n".join(str(_) for _ in exc))
29 RuntimeError.__init__(self, msg)
30 elif name is not None:
31 msg = "Function '{}' from module '{}' failed due to '{}'\n{}".format(
32 name, module.__name__, exc, format_exc())
33 RuntimeError.__init__(self, msg)
34 else:
35 raise RuntimeError( # pragma: no cover
36 "Unknown test error.")
39def run_test_function(module, pattern="^test_.*", stop_first=False, verbose=False, fLOG=print):
40 """
41 Runs test functions from *module*.
43 :param module: module (string or module)
44 :param pattern: function pattern
45 :param stop_first: stops at the first error or run all of them
46 :param verbose: prints out the name of the functions
47 :param fLOG: logging function
49 The following piece of code could also be used to
50 run all tests not using any parameter.
52 ::
54 fcts = [v for k, v in locals().items() if k.startswith('test_')]
55 for fct in fcts:
56 print("run", fct.__name__)
57 try:
58 fct()
59 except Exception as e:
60 if 'missing' in str(e):
61 print(e)
62 continue
63 raise e
64 """
65 if isinstance(module, str):
66 module_path = module
67 module = os.path.splitext(module)[0]
68 _, module_name = os.path.split(os.path.splitext(module)[0])
69 with warnings.catch_warnings(record=False):
70 spec = importlib.util.spec_from_file_location(
71 module_name, module_path)
72 if spec is None:
73 raise ImportError( # pragma: no cover
74 "Cannot import module '{}' from '{}'.".format(
75 module_name, module_path))
76 module = importlib.util.module_from_spec(spec)
77 spec.loader.exec_module(module)
78 if module is None:
79 raise ValueError( # pragma: no cover
80 "module must be specified.")
82 reg = re.compile(pattern)
83 fcts = []
84 for name, fct in module.__dict__.items():
85 if not reg.search(name):
86 continue
87 if not callable(fct):
88 continue # pragma: no cover
89 sig = signature(fct)
90 if sig.parameters:
91 continue
92 fcts.append((name, fct))
94 excs = []
95 tested = []
96 i = 0
97 for name, fct in fcts:
99 t0 = time.perf_counter()
100 with warnings.catch_warnings(record=False):
101 try:
102 fct()
103 exc = None
104 except Exception as e:
105 exc = TestExecutionError(module, name, e)
106 if stop_first:
107 raise exc
108 excs.append(exc)
109 dt = time.perf_counter() - t0
110 if verbose:
111 fLOG( # pragma: no cover
112 "[run_test_function] {}/{}: {} '{}' in {:0.000}s".format(
113 i + 1, len(fcts), 'OK' if exc is None else '--', name, dt))
114 tested.append(name)
115 i += 1
117 if len(excs) > 0:
118 raise TestExecutionError(module, None, excs)
119 if len(tested) == 0:
120 raise ValueError(
121 "No function found in '{}' with pattern '{}'.".format(module, pattern))