-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
executable file
·63 lines (45 loc) · 1.49 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
59
60
61
62
63
"""Test script that runs the Demo notebook."""
import subprocess
import tempfile
import nbformat
def _notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
Parameters
----------
path:
Returns
-------
nb, errors: tuple
A tuple of the parsed nb object and the execution errors.
"""
kernel = "python3"
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=1200",
"--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(*notebooks):
"""Run the test on every provided notebook
Parameters
----------
notebooks: str
The full filepaths to the notebooks to be tested.
"""
for filename in notebooks:
if filename.split('.')[-1] == "ipynb":
_, errors = _notebook_run(filename)
if errors != []:
raise Exception
def main():
"""Begin the test script here"""
notebooks = ["examples/Demo.ipynb"]
run_tests(*notebooks)
if __name__ == "__main__":
main()