Skip to content

Commit 21d6b64

Browse files
committed
test demos cleanup
1 parent 02aa6a1 commit 21d6b64

File tree

1 file changed

+104
-93
lines changed

1 file changed

+104
-93
lines changed

tests/test_demos.py

Lines changed: 104 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import sys
99
from glob import glob
10+
import unittest
1011
from nose.tools import assert_less, assert_in
1112
from returnn.util import better_exchook
1213
from returnn.util.basic import which_pip
@@ -15,6 +16,15 @@
1516
py = sys.executable
1617
print("Python:", py)
1718

19+
os.chdir((os.path.dirname(__file__) or ".") + "/..")
20+
assert os.path.exists("rnn.py")
21+
22+
23+
try:
24+
import theano
25+
except ImportError:
26+
theano = None
27+
1828

1929
def build_env():
2030
theano_flags = {key: value for (key, value)
@@ -79,110 +89,111 @@ def cleanup_tmp_models(config_filename):
7989
os.remove(f)
8090

8191

82-
class TestDemos(object):
83-
84-
@classmethod
85-
def setup_class(cls):
86-
os.chdir((os.path.dirname(__file__) or ".") + "/..")
87-
assert os.path.exists("rnn.py")
88-
89-
def test_demo_task12ax(self):
90-
fer = run_config_get_fer("demos/demo-theano-task12ax.config")
91-
assert_less(fer, 0.01)
92-
93-
def test_demo_iter_dataset_task12ax(self):
94-
cleanup_tmp_models("demos/demo-theano-task12ax.config")
95-
out = run(py, "demos/demo-iter-dataset.py", "demos/demo-theano-task12ax.config")
96-
assert_in("Epoch 5.", out.splitlines())
97-
98-
def test_demo_returnn_as_framework(self):
99-
print("Prepare.")
100-
import subprocess
101-
import shutil
102-
from glob import glob
103-
from returnn.util.basic import get_login_username
104-
# echo via subprocess, because this stdout as well as the other will always be visible.
105-
subprocess.check_call(["echo", "travis_fold:start:test_demo_returnn_as_framework"])
106-
assert os.path.exists("setup.py")
107-
if glob("dist/*.tar.gz"):
108-
# we want it unique below
109-
for fn in glob("dist/*.tar.gz"):
110-
os.remove(fn)
111-
if os.path.exists("MANIFEST"):
112-
os.remove("MANIFEST") # auto-generated. will be recreated
113-
if os.path.exists("docs/crnn"):
114-
os.remove("docs/crnn") # this is auto-generated, and confuses setup.py sdist
115-
if os.path.exists("docs/returnn"):
116-
os.remove("docs/returnn") # this is auto-generated, and confuses setup.py sdist
117-
tmp_model_dir = "/tmp/%s/returnn-demo-as-framework" % get_login_username()
118-
if os.path.exists(tmp_model_dir):
119-
shutil.rmtree(tmp_model_dir, ignore_errors=True)
120-
print("setup.py sdist, to create package.")
121-
subprocess.check_call([py, "setup.py", "sdist"])
122-
dist_fns = glob("dist/*.tar.gz")
123-
assert len(dist_fns) == 1
124-
dist_fn = os.path.abspath(dist_fns[0])
125-
pip_path = which_pip()
126-
print("Pip install Returnn.")
127-
in_virtual_env = hasattr(sys, 'real_prefix') # https://stackoverflow.com/questions/1871549/
128-
cmd = [py, pip_path, "install"]
129-
if not in_virtual_env:
130-
cmd += ["--user"]
131-
cmd += ["-v", dist_fn]
132-
print("$ %s" % " ".join(cmd))
133-
subprocess.check_call(cmd, cwd="/")
134-
print("Running demo now.")
135-
subprocess.check_call([py, "demo-returnn-as-framework.py"], cwd="demos")
136-
print("Success.")
137-
subprocess.check_call(["echo", "travis_fold:end:test_demo_returnn_as_framework"])
138-
139-
def test_demo_sprint_interface(self):
140-
import subprocess
141-
# echo via subprocess, because this stdout as well as the other will always be visible.
142-
subprocess.check_call(["echo", "travis_fold:start:test_demo_sprint_interface"])
143-
subprocess.check_call([py, os.path.abspath("demos/demo-sprint-interface.py")], cwd="/")
144-
subprocess.check_call(["echo", "travis_fold:end:test_demo_sprint_interface"])
145-
146-
def test_returnn_as_framework_TaskSystem(self):
147-
import subprocess
148-
# echo via subprocess, because this stdout as well as the other will always be visible.
149-
subprocess.check_call(["echo", "travis_fold:start:test_returnn_as_framework_TaskSystem"])
150-
subprocess.check_call([py, os.path.abspath("tests/returnn-as-framework.py"), "test_TaskSystem_Pickler()"], cwd="/")
151-
subprocess.check_call(["echo", "travis_fold:end:test_returnn_as_framework_TaskSystem"])
152-
153-
def test_returnn_as_framework_old_style_crnn_TFUtil(self):
154-
import subprocess
155-
# echo via subprocess, because this stdout as well as the other will always be visible.
156-
subprocess.check_call(["echo", "travis_fold:start:test_returnn_as_framework_old_style_crnn_TFUtil"])
157-
subprocess.check_call([
158-
py, os.path.abspath("tests/returnn-as-framework.py"),
159-
"--old-style", "--returnn-package-name", "crnn",
160-
"test_old_style_import_crnn_TFUtil()"], cwd="/")
161-
subprocess.check_call(["echo", "travis_fold:end:test_returnn_as_framework_old_style_crnn_TFUtil"])
162-
163-
def test_returnn_as_framework_old_style_TFUtil(self):
164-
import subprocess
165-
# echo via subprocess, because this stdout as well as the other will always be visible.
166-
subprocess.check_call(["echo", "travis_fold:start:test_returnn_as_framework_old_style_TFUtil"])
167-
subprocess.check_call([
168-
py, os.path.abspath("tests/returnn-as-framework.py"), "--old-style", "test_old_style_import_TFUtil()"], cwd="/")
169-
subprocess.check_call(["echo", "travis_fold:end:test_returnn_as_framework_old_style_TFUtil"])
92+
@unittest.skipIf(not theano, "Theano not installed")
93+
def test_demo_task12ax():
94+
fer = run_config_get_fer("demos/demo-theano-task12ax.config")
95+
assert_less(fer, 0.01)
96+
97+
98+
def test_demo_iter_dataset_task12ax():
99+
cleanup_tmp_models("demos/demo-theano-task12ax.config")
100+
out = run(py, "demos/demo-iter-dataset.py", "demos/demo-theano-task12ax.config")
101+
assert_in("Epoch 5.", out.splitlines())
102+
103+
104+
def test_demo_returnn_as_framework():
105+
print("Prepare.")
106+
import subprocess
107+
import shutil
108+
from glob import glob
109+
from returnn.util.basic import get_login_username
110+
# echo via subprocess, because this stdout as well as the other will always be visible.
111+
subprocess.check_call(["echo", "travis_fold:start:test_demo_returnn_as_framework"])
112+
assert os.path.exists("setup.py")
113+
if glob("dist/*.tar.gz"):
114+
# we want it unique below
115+
for fn in glob("dist/*.tar.gz"):
116+
os.remove(fn)
117+
if os.path.exists("MANIFEST"):
118+
os.remove("MANIFEST") # auto-generated. will be recreated
119+
if os.path.exists("docs/crnn"):
120+
os.remove("docs/crnn") # this is auto-generated, and confuses setup.py sdist
121+
if os.path.exists("docs/returnn"):
122+
os.remove("docs/returnn") # this is auto-generated, and confuses setup.py sdist
123+
tmp_model_dir = "/tmp/%s/returnn-demo-as-framework" % get_login_username()
124+
if os.path.exists(tmp_model_dir):
125+
shutil.rmtree(tmp_model_dir, ignore_errors=True)
126+
print("setup.py sdist, to create package.")
127+
subprocess.check_call([py, "setup.py", "sdist"])
128+
dist_fns = glob("dist/*.tar.gz")
129+
assert len(dist_fns) == 1
130+
dist_fn = os.path.abspath(dist_fns[0])
131+
pip_path = which_pip()
132+
print("Pip install Returnn.")
133+
in_virtual_env = hasattr(sys, 'real_prefix') # https://stackoverflow.com/questions/1871549/
134+
cmd = [py, pip_path, "install"]
135+
if not in_virtual_env:
136+
cmd += ["--user"]
137+
cmd += ["-v", dist_fn]
138+
print("$ %s" % " ".join(cmd))
139+
subprocess.check_call(cmd, cwd="/")
140+
print("Running demo now.")
141+
subprocess.check_call([py, "demo-returnn-as-framework.py"], cwd="demos")
142+
print("Success.")
143+
subprocess.check_call(["echo", "travis_fold:end:test_demo_returnn_as_framework"])
144+
145+
146+
def test_demo_sprint_interface():
147+
import subprocess
148+
# echo via subprocess, because this stdout as well as the other will always be visible.
149+
subprocess.check_call(["echo", "travis_fold:start:test_demo_sprint_interface"])
150+
subprocess.check_call([py, os.path.abspath("demos/demo-sprint-interface.py")], cwd="/")
151+
subprocess.check_call(["echo", "travis_fold:end:test_demo_sprint_interface"])
152+
153+
154+
def test_returnn_as_framework_TaskSystem():
155+
import subprocess
156+
# echo via subprocess, because this stdout as well as the other will always be visible.
157+
subprocess.check_call(["echo", "travis_fold:start:test_returnn_as_framework_TaskSystem"])
158+
subprocess.check_call([py, os.path.abspath("tests/returnn-as-framework.py"), "test_TaskSystem_Pickler()"], cwd="/")
159+
subprocess.check_call(["echo", "travis_fold:end:test_returnn_as_framework_TaskSystem"])
160+
161+
162+
def test_returnn_as_framework_old_style_crnn_TFUtil():
163+
import subprocess
164+
# echo via subprocess, because this stdout as well as the other will always be visible.
165+
subprocess.check_call(["echo", "travis_fold:start:test_returnn_as_framework_old_style_crnn_TFUtil"])
166+
subprocess.check_call([
167+
py, os.path.abspath("tests/returnn-as-framework.py"),
168+
"--old-style", "--returnn-package-name", "crnn",
169+
"test_old_style_import_crnn_TFUtil()"], cwd="/")
170+
subprocess.check_call(["echo", "travis_fold:end:test_returnn_as_framework_old_style_crnn_TFUtil"])
171+
172+
173+
def test_returnn_as_framework_old_style_TFUtil():
174+
import subprocess
175+
# echo via subprocess, because this stdout as well as the other will always be visible.
176+
subprocess.check_call(["echo", "travis_fold:start:test_returnn_as_framework_old_style_TFUtil"])
177+
subprocess.check_call([
178+
py, os.path.abspath("tests/returnn-as-framework.py"), "--old-style", "test_old_style_import_TFUtil()"], cwd="/")
179+
subprocess.check_call(["echo", "travis_fold:end:test_returnn_as_framework_old_style_TFUtil"])
170180

171181

172182
if __name__ == '__main__':
173183
better_exchook.install()
174-
TestDemos.setup_class()
175-
tests = TestDemos()
176184
import sys
177185
fns = sys.argv[1:]
178186
if not fns:
179-
fns = [arg for arg in dir(tests) if arg.startswith("test_")]
187+
fns = [arg for arg in globals() if arg.startswith("test_")]
180188
for arg in fns:
181-
f = getattr(tests, arg)
189+
f = globals()[arg]
182190
print("-" * 20)
183191
print("Run:", f)
184192
print("-" * 20)
185-
f()
193+
try:
194+
f()
195+
except unittest.SkipTest as exc:
196+
print("(SkipTest: %s)" % exc)
186197
print("-" * 20)
187198
print("Ok.")
188199
print("-" * 20)

0 commit comments

Comments
 (0)