4
4
import importlib
5
5
import subprocess
6
6
import time
7
+ from onnx_array_api import __file__ as onnx_array_api_file
7
8
from onnx_array_api .ext_test_case import ExtTestCase
8
9
10
+ VERBOSE = 0
11
+ ROOT = os .path .realpath (os .path .abspath (os .path .join (onnx_array_api_file , ".." , ".." )))
12
+
9
13
10
14
def import_source (module_file_path , module_name ):
11
15
if not os .path .exists (module_file_path ):
@@ -20,43 +24,58 @@ def import_source(module_file_path, module_name):
20
24
21
25
22
26
class TestDocumentationExamples (ExtTestCase ):
23
- def test_documentation_examples (self ):
27
+ def run_test (self , fold : str , name : str , verbose = 0 ) -> int :
28
+ ppath = os .environ ["PYTHONPATH" ]
29
+ if len (ppath ) == 0 :
30
+ os .environ ["PYTHONPATH" ] = ROOT
31
+ elif ROOT not in ppath :
32
+ sep = ";" if sys .platform == "win32" else ":"
33
+ os .environ ["PYTHONPATH" ] = ppath + sep + ROOT
34
+ perf = time .perf_counter ()
35
+ try :
36
+ mod = import_source (fold , os .path .splitext (name )[0 ])
37
+ assert mod is not None
38
+ except FileNotFoundError :
39
+ # try another way
40
+ cmds = [sys .executable , "-u" , os .path .join (fold , name )]
41
+ p = subprocess .Popen (cmds , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
42
+ res = p .communicate ()
43
+ out , err = res
44
+ st = err .decode ("ascii" , errors = "ignore" )
45
+ if len (st ) > 0 and "Traceback" in st :
46
+ if '"dot" not found in path.' in st :
47
+ # dot not installed, this part
48
+ # is tested in onnx framework
49
+ if verbose :
50
+ print (f"failed: { name !r} due to missing dot." )
51
+ return 0
52
+ raise AssertionError (
53
+ "Example '{}' (cmd: {} - exec_prefix='{}') "
54
+ "failed due to\n {}"
55
+ "" .format (name , cmds , sys .exec_prefix , st )
56
+ )
57
+ dt = time .perf_counter () - perf
58
+ if verbose :
59
+ print (f"{ dt :.3f} : run { name !r} " )
60
+ return 1
61
+
62
+ @classmethod
63
+ def add_test_methods (cls ):
24
64
this = os .path .abspath (os .path .dirname (__file__ ))
25
65
fold = os .path .normpath (os .path .join (this , ".." , ".." , "_doc" , "examples" ))
26
66
found = os .listdir (fold )
27
- tested = 0
28
67
for name in found :
29
68
if name .startswith ("plot_" ) and name .endswith (".py" ):
30
- perf = time .perf_counter ()
31
- try :
32
- mod = import_source (fold , os .path .splitext (name )[0 ])
33
- assert mod is not None
34
- except FileNotFoundError :
35
- # try another way
36
- cmds = [sys .executable , "-u" , os .path .join (fold , name )]
37
- p = subprocess .Popen (
38
- cmds , stdout = subprocess .PIPE , stderr = subprocess .PIPE
39
- )
40
- res = p .communicate ()
41
- out , err = res
42
- st = err .decode ("ascii" , errors = "ignore" )
43
- if len (st ) > 0 and "Traceback" in st :
44
- if '"dot" not found in path.' in st :
45
- # dot not installed, this part
46
- # is tested in onnx framework
47
- print (f"failed: { name !r} due to missing dot." )
48
- continue
49
- raise AssertionError (
50
- "Example '{}' (cmd: {} - exec_prefix='{}') "
51
- "failed due to\n {}"
52
- "" .format (name , cmds , sys .exec_prefix , st )
53
- )
54
- dt = time .perf_counter () - perf
55
- print (f"{ dt :.3f} : run { name !r} " )
56
- tested += 1
57
- if tested == 0 :
58
- raise AssertionError ("No example was tested." )
69
+ short_name = os .path .split (os .path .splitext (name )[0 ])[- 1 ]
70
+
71
+ def _test_ (self , name = name ):
72
+ res = self .run_test (fold , name , verbose = VERBOSE )
73
+ self .assertTrue (res )
74
+
75
+ setattr (cls , f"test_{ short_name } " , _test_ )
76
+
59
77
78
+ TestDocumentationExamples .add_test_methods ()
60
79
61
80
if __name__ == "__main__" :
62
- unittest .main ()
81
+ unittest .main (verbosity = 2 )
0 commit comments