Hide keyboard shortcuts

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 

13 

14 

15class TestExecutionError(RuntimeError): 

16 """ 

17 Raised when the execution of a test fails. 

18 """ 

19 

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("Unknown test error.") 

36 

37 

38def run_test_function(module, pattern="^test_.*", stop_first=False, verbose=False, fLOG=print): 

39 """ 

40 Runs test functions from *module*. 

41 

42 :param module: module (string or module) 

43 :param pattern: function pattern 

44 :param stop_first: stops at the first error or run all of them 

45 :param verbose: prints out the name of the functions 

46 :param fLOG: logging function 

47 

48 The following piece of code could also be used to 

49 run all tests not using any parameter. 

50 

51 :: 

52 

53 fcts = [v for k, v in locals().items() if k.startswith('test_')] 

54 for fct in fcts: 

55 print("run", fct.__name__) 

56 try: 

57 fct() 

58 except Exception as e: 

59 if 'missing' in str(e): 

60 print(e) 

61 continue 

62 raise e 

63 """ 

64 if isinstance(module, str): 

65 module_path = module 

66 module = os.path.splitext(module)[0] 

67 _, module_name = os.path.split(os.path.splitext(module)[0]) 

68 with warnings.catch_warnings(record=False): 

69 spec = importlib.util.spec_from_file_location( 

70 module_name, module_path) 

71 if spec is None: 

72 raise ImportError("Cannot import module '{}' from '{}'.".format( 

73 module_name, module_path)) 

74 module = importlib.util.module_from_spec(spec) 

75 spec.loader.exec_module(module) 

76 if module is None: 

77 raise ValueError("module must be specified.") 

78 

79 reg = re.compile(pattern) 

80 fcts = [] 

81 for name, fct in module.__dict__.items(): 

82 if not reg.search(name): 

83 continue 

84 if not callable(fct): 

85 continue 

86 sig = signature(fct) 

87 if sig.parameters: 

88 continue 

89 fcts.append((name, fct)) 

90 

91 excs = [] 

92 tested = [] 

93 i = 0 

94 for name, fct in fcts: 

95 

96 t0 = time.perf_counter() 

97 with warnings.catch_warnings(record=False): 

98 try: 

99 fct() 

100 exc = None 

101 except Exception as e: 

102 exc = TestExecutionError(module, name, e) 

103 if stop_first: 

104 raise exc 

105 excs.append(exc) 

106 dt = time.perf_counter() - t0 

107 if verbose: 

108 fLOG("[run_test_function] {}/{}: {} '{}' in {:0.000}s".format( 

109 i + 1, len(fcts), 'OK' if exc is None else '--', name, dt)) 

110 tested.append(name) 

111 i += 1 

112 

113 if len(excs) > 0: 

114 raise TestExecutionError(module, None, excs) 

115 if len(tested) == 0: 

116 raise ValueError( 

117 "No function found in '{}' with pattern '{}'.".format(module, pattern))