-
Notifications
You must be signed in to change notification settings - Fork 98
/
test.py
58 lines (47 loc) · 1.7 KB
/
test.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
#!/usr/bin/env
"""Basic test script that runs all notebooks."""
import os
import subprocess
import tempfile
import sys
import nbformat
import doctest
import glob
if sys.version_info >= (3,0):
kernel = 'python3'
else:
kernel = 'python2'
# Construct notebook list
notebooks = glob.glob("*.ipynb")
# Note we leave out the python intro as there are purposeful exceptions
notebooks.remove('01_python.ipynb')
# The following either require additional packages (FV and FE), do not entirely
# work yet (spectral), or do not contain any substantial Python code
notebooks.remove('13_finite_volume.ipynb')
notebooks.remove('14_spectral.ipynb')
notebooks.remove('15_finite_element.ipynb')
notebooks.remove('16_performance.ipynb')
def _notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=60",
"--ExecutePreprocessor.kernel_name="+kernel,
"--output", fout.name, path]
subprocess.check_call(args)
fout.seek(0)
nb = nbformat.reads(fout.read().decode('utf-8'), nbformat.current_nbformat)
errors = [output for cell in nb.cells if "outputs" in cell
for output in cell["outputs"]
if output.output_type == "error"]
return nb, errors
def run_tests():
for filename in notebooks:
if (filename.split('.')[-1] == 'ipynb'):
nb, errors = _notebook_run(filename)
if errors != []:
raise(Exception)
if __name__ == '__main__':
run_tests()