Skip to content

Commit 1a62669

Browse files
committed
Update test_documentation_examples.py
1 parent dd1876f commit 1a62669

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

_unittests/ut__main/test_documentation_examples.py

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import importlib
55
import subprocess
66
import time
7+
from onnx_array_api import __file__ as onnx_array_api_file
78
from onnx_array_api.ext_test_case import ExtTestCase
89

10+
VERBOSE = 0
11+
ROOT = os.path.realpath(os.path.abspath(os.path.join(onnx_array_api_file, "..", "..")))
12+
913

1014
def import_source(module_file_path, module_name):
1115
if not os.path.exists(module_file_path):
@@ -20,43 +24,58 @@ def import_source(module_file_path, module_name):
2024

2125

2226
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):
2464
this = os.path.abspath(os.path.dirname(__file__))
2565
fold = os.path.normpath(os.path.join(this, "..", "..", "_doc", "examples"))
2666
found = os.listdir(fold)
27-
tested = 0
2867
for name in found:
2968
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+
5977

78+
TestDocumentationExamples.add_test_methods()
6079

6180
if __name__ == "__main__":
62-
unittest.main()
81+
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)