This repository has been archived by the owner on Feb 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_notebooks.py
66 lines (52 loc) · 1.66 KB
/
test_notebooks.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
import os
import subprocess
import tempfile
import nbformat
_TEST_DIR = os.path.abspath(os.path.dirname(__file__))
_EXCLUDE = []
def all_notebooks(path="."):
notebooks = []
for root, dirs, files in os.walk(path):
if ".ipynb_checkpoints" in root:
continue
for file in files:
if file.endswith(".ipynb") and (file not in _EXCLUDE):
notebooks.append(os.path.join(root, file))
return notebooks
def pytest_generate_tests(metafunc):
if "notebook" in metafunc.fixturenames:
metafunc.parametrize("notebook", all_notebooks(_TEST_DIR))
def _notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
_, notebook = os.path.split(path)
base, ext = os.path.splitext(notebook)
with tempfile.NamedTemporaryFile("w", suffix=".ipynb") as fp:
args = [
"jupyter",
"nbconvert",
"--to",
"notebook",
"--execute",
"--ExecutePreprocessor.kernel_name=python",
"--ExecutePreprocessor.timeout=None",
"--output",
fp.name,
"--output-dir=.",
path,
]
subprocess.check_call(args)
nb = nbformat.read(fp.name, nbformat.current_nbformat, encoding="UTF-8")
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 test_notebook(tmpdir, notebook):
with tmpdir.as_cwd():
nb, errors = _notebook_run(notebook)
assert not errors