-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathrun_tests.py
executable file
·80 lines (61 loc) · 1.93 KB
/
run_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
from __future__ import print_function
import glob
import os
import os.path as osp
import subprocess
import yaml
def parse(data):
for _, datum in data.items():
if isinstance(datum, dict):
for key_content in parse(datum):
yield key_content
else:
for item in datum:
for key in item:
item_k = item[key]
if isinstance(item_k, dict):
content = item_k["content"]
is_code = item_k.get("is_code", True)
if item_k.get("skip_test", False):
continue
else:
content = item_k
is_code = True
if is_code and content is not None:
yield key, content
def main():
here = osp.dirname(osp.abspath(__file__))
with open(osp.join(here, "conversions.yaml")) as f:
data = yaml.safe_load(f)
for fname in glob.glob(osp.join(here, "tests/*.py")):
os.remove(fname)
for i, (key, content) in enumerate(parse(data)):
if key == "numpy":
code = """\
import numpy as np
def test_{key}_{id:04d}():
x = np.array([[1, 2, 3], [4, 5, 6]])
{content}
"""
elif key == "pytorch":
code = """\
import torch
def test_{key}_{id:04d}():
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
{content}
"""
else:
raise ValueError
content = "\n".join(" " * 4 + line for line in content.splitlines())
code = code.format(key=key, id=i, content=content)
test_file = osp.join(
here, "tests/test_{key}_{id:04d}.py".format(key=key, id=i)
)
with open(test_file, "w") as f:
f.write(code)
cmd = "pytest -vs tests"
print("+ %s" % cmd)
subprocess.check_call(cmd, shell=True)
if __name__ == "__main__":
main()