Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e56deb

Browse files
committed
TST: add test for notebook executing
1 parent 14eadeb commit 7e56deb

File tree

2 files changed

+914
-0
lines changed

2 files changed

+914
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
import os
3+
import subprocess
4+
import tempfile
5+
import nbformat
6+
7+
8+
# From https://blog.thedataincubator.com/2016/06/testing-jupyter-notebooks/
9+
10+
def _notebook_run(nb_file):
11+
"""Execute a notebook via nbconvert and collect output.
12+
:returns (parsed nb object, execution errors)
13+
"""
14+
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
15+
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
16+
"--ExecutePreprocessor.timeout=500",
17+
"--output", fout.name, nb_file]
18+
subprocess.check_call(args)
19+
20+
fout.seek(0)
21+
nb = nbformat.read(fout, nbformat.current_nbformat)
22+
23+
errors = [output for cell in nb.cells if "outputs" in cell
24+
for output in cell["outputs"]
25+
if output.output_type == "error"]
26+
return nb, errors
27+
28+
29+
def test_ipynb():
30+
nb, errors = _notebook_run('lib/matplotlib/tests/test_nbagg_01.ipynb')
31+
assert errors == []

0 commit comments

Comments
 (0)