forked from pybamm-team/PyBaMM
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-tests.py
executable file
·396 lines (354 loc) · 11.3 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python
#
# Runs all unit tests included in PyBaMM.
#
# The code in this file is adapted from Pints
# (see https://github.com/pints-team/pints)
#
import re
import os
import pybamm
import sys
import argparse
import unittest
import subprocess
def run_code_tests(executable=False, folder: str = "unit", interpreter="python"):
"""
Runs tests, exits if they don't finish.
Parameters
----------
executable : bool (default False)
If True, tests are run in subprocesses using the executable 'python'.
Must be True for travis tests (otherwise tests always 'pass')
folder : str
Which folder to run the tests from (unit, integration or both ('all'))
"""
if folder == "all":
tests = "tests/"
else:
tests = "tests/" + folder
if folder == "unit":
pybamm.settings.debug_mode = True
if interpreter == "python":
# Make sure to refer to the interpreter for the
# currently activated virtual environment
interpreter = sys.executable
if executable is False:
suite = unittest.defaultTestLoader.discover(tests, pattern="test*.py")
result = unittest.TextTestRunner(verbosity=2).run(suite)
ret = int(not result.wasSuccessful())
else:
print("Running {} tests with executable '{}'".format(folder, interpreter))
cmd = [interpreter, "-m", "unittest", "discover", "-v", tests]
p = subprocess.Popen(cmd)
try:
ret = p.wait()
except KeyboardInterrupt:
try:
p.terminate()
except OSError:
pass
p.wait()
print("")
sys.exit(1)
if ret != 0:
sys.exit(ret)
def run_doc_tests():
"""
Checks if the documentation can be built, runs any doctests (currently not
used).
"""
print("Checking if docs can be built.")
p = subprocess.Popen(
["sphinx-build", "-b", "doctest", "docs", "docs/build/html", "-W"]
)
try:
ret = p.wait()
except KeyboardInterrupt:
try:
p.terminate()
except OSError:
pass
p.wait()
print("")
sys.exit(1)
if ret != 0:
print("FAILED")
sys.exit(ret)
def run_notebook_and_scripts(executable="python"):
"""
Runs Jupyter notebook tests. Exits if they fail.
"""
# Scan and run
print("Testing notebooks and scripts with executable `" + str(executable) + "`")
if not scan_for_nb_and_scripts("examples", True, executable):
print("\nErrors encountered in notebooks")
sys.exit(1)
print("\nOK")
def scan_for_nb_and_scripts(root, recursive=True, executable="python"):
"""
Scans for, and tests, all notebooks and scripts in a directory.
"""
ok = True
debug = False
# Scan path
for filename in os.listdir(root):
path = os.path.join(root, filename)
# Recurse into subdirectories
if recursive and os.path.isdir(path):
# Ignore hidden directories
if filename[:1] == ".":
continue
ok &= scan_for_nb_and_scripts(path, recursive, executable)
# Test notebooks
if os.path.splitext(path)[1] == ".ipynb":
if debug:
print(path)
else:
ok &= test_notebook(path, executable)
# Test scripts
elif os.path.splitext(path)[1] == ".py":
if debug:
print(path)
else:
ok &= test_script(path, executable)
# Return True if every notebook is ok
return ok
def test_notebook(path, executable="python"):
"""
Tests a single notebook, exists if it doesn't finish.
"""
import nbconvert
import pybamm
b = pybamm.Timer()
print("Test " + path + " ... ", end="")
sys.stdout.flush()
# Make sure the notebook has a "%pip install pybamm -q" command, for using Google
# Colab
with open(path, "r") as f:
if "%pip install pybamm -q" not in f.read():
# print error and exit
print("\n" + "-" * 70)
print("ERROR")
print("-" * 70)
print("Installation command '%pip install pybamm -q' not found in notebook")
print("-" * 70)
return False
# Make sure the notebook has "pybamm.print_citations()" to print the relevant papers
with open(path, "r") as f:
if "pybamm.print_citations()" not in f.read():
# print error and exit
print("\n" + "-" * 70)
print("ERROR")
print("-" * 70)
print(
"Print citations command 'pybamm.print_citations()' not found in "
"notebook"
)
print("-" * 70)
return False
# Load notebook, convert to Python
e = nbconvert.exporters.PythonExporter()
code, __ = e.from_filename(path)
# Remove coding statement, if present
code = "\n".join([x for x in code.splitlines() if x[:9] != "# coding"])
# Tell matplotlib not to produce any figures
env = dict(os.environ)
env["MPLBACKEND"] = "Template"
# If notebook makes use of magic commands then
# the script must be run using ipython
# https://github.com/jupyter/nbconvert/issues/503#issuecomment-269527834
executable = (
"ipython"
if (("run_cell_magic(" in code) or ("run_line_magic(" in code))
else executable
)
# Run in subprocess
cmd = [executable] + ["-c", code]
try:
p = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
)
stdout, stderr = p.communicate()
# TODO: Use p.communicate(timeout=3600) if Python3 only
if p.returncode != 0:
# Show failing code, output and errors before returning
print("ERROR")
print("-- script " + "-" * (79 - 10))
for i, line in enumerate(code.splitlines()):
j = str(1 + i)
print(j + " " * (5 - len(j)) + line)
print("-- stdout " + "-" * (79 - 10))
print(str(stdout, "utf-8"))
print("-- stderr " + "-" * (79 - 10))
print(str(stderr, "utf-8"))
print("-" * 79)
return False
except KeyboardInterrupt:
p.terminate()
print("ABORTED")
sys.exit(1)
# Sucessfully run
print("ok ({})".format(b.time()))
return True
def test_script(path, executable="python"):
"""
Tests a single notebook, exists if it doesn't finish.
"""
import pybamm
b = pybamm.Timer()
print("Test " + path + " ... ", end="")
sys.stdout.flush()
# Tell matplotlib not to produce any figures
env = dict(os.environ)
env["MPLBACKEND"] = "Template"
# Run in subprocess
cmd = [executable] + [path]
try:
p = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
)
stdout, stderr = p.communicate()
# TODO: Use p.communicate(timeout=3600) if Python3 only
if p.returncode != 0:
# Show failing code, output and errors before returning
print("ERROR")
print("-- stdout " + "-" * (79 - 10))
print(str(stdout, "utf-8"))
print("-- stderr " + "-" * (79 - 10))
print(str(stderr, "utf-8"))
print("-" * 79)
return False
except KeyboardInterrupt:
p.terminate()
print("ABORTED")
sys.exit(1)
# Sucessfully run
print("ok ({})".format(b.time()))
return True
def export_notebook(ipath, opath):
"""
Exports the notebook at `ipath` to a Python file at `opath`.
"""
import nbconvert
from traitlets.config import Config
# Create nbconvert configuration to ignore text cells
c = Config()
c.TemplateExporter.exclude_markdown = True
# Load notebook, convert to Python
e = nbconvert.exporters.PythonExporter(config=c)
code, __ = e.from_filename(ipath)
# Remove "In [1]:" comments
r = re.compile(r"(\s*)# In\[([^]]*)\]:(\s)*")
code = r.sub("\n\n", code)
# Store as executable script file
with open(opath, "w") as f:
f.write("#!/usr/bin/env python")
f.write(code)
os.chmod(opath, 0o775)
if __name__ == "__main__":
# Set up argument parsing
parser = argparse.ArgumentParser(
description="Run unit tests for PyBaMM.",
epilog="To run individual unit tests, use e.g. '$ tests/unit/test_timer.py'",
)
# Unit tests
parser.add_argument(
"--integration",
action="store_true",
help="Run integration tests using the python interpreter.",
)
parser.add_argument(
"--unit",
action="store_true",
help="Run unit tests using the `python` interpreter.",
)
parser.add_argument(
"--all",
action="store_true",
help="Run all tests (unit and integration) using the `python` interpreter.",
)
parser.add_argument(
"--nosub",
action="store_true",
help="Run unit tests without starting a subprocess.",
)
# Notebook tests
parser.add_argument(
"--examples",
action="store_true",
help="Test all Jupyter notebooks and scripts in `examples`.",
)
parser.add_argument(
"-debook",
nargs=2,
metavar=("in", "out"),
help="Export a Jupyter notebook to a Python file for manual testing.",
)
# Flake8 (deprecated)
parser.add_argument(
"--flake8",
action="store_true",
help="Run flake8 to check for style issues (deprecated, use pre-commit)",
)
# Doctests
parser.add_argument(
"--doctest",
action="store_true",
help="Run any doctests, check if docs can be built",
)
# Combined test sets
parser.add_argument(
"--quick",
action="store_true",
help="Run quick checks (code tests, docs)",
)
# Non-standard Python interpreter name for subprocesses
parser.add_argument(
"--interpreter",
nargs="?",
default="python",
metavar="python",
help="Give the name of the Python interpreter if it is not 'python'",
)
# Parse!
args = parser.parse_args()
# Run tests
has_run = False
# Unit vs integration
interpreter = args.interpreter
# Unit tests
if args.integration:
has_run = True
run_code_tests(True, "integration", interpreter)
if args.unit:
has_run = True
run_code_tests(True, "unit", interpreter)
if args.all:
has_run = True
run_code_tests(True, "all", interpreter)
if args.nosub:
has_run = True
run_code_tests(folder="unit", interpreter=interpreter)
# Flake8
if args.flake8:
raise NotImplementedError("flake8 is no longer used. Use pre-commit instead.")
# Doctests
if args.doctest:
has_run = True
run_doc_tests()
# Notebook tests
elif args.examples:
has_run = True
run_notebook_and_scripts(interpreter)
if args.debook:
has_run = True
export_notebook(*args.debook)
# Combined test sets
if args.quick:
has_run = True
run_code_tests("all", interpreter=interpreter)
run_doc_tests()
# Help
if not has_run:
parser.print_help()