-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_examples.py
60 lines (49 loc) · 1.44 KB
/
test_examples.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
"""Execute notebooks as tests, reporting an error if cell throws an exception.
Adapted from MDTraj https://github.com/mdtraj/mdtraj/pull/1130 which adapted
from https://gist.github.com/minrk/2620876.
"""
import glob
import os
import sys
from jupyter_client import KernelManager
import nbformat
import pytest
ROOT = os.getcwd()
NOTEBOOKS = [f for f in glob.glob('*/*.ipynb')]
@pytest.mark.parametrize("filepath", NOTEBOOKS)
def test_examples(filepath):
os.chdir(ROOT)
check_one_notebook(filepath)
def check_one_notebook(filepath):
folder, filename = os.path.split(filepath)
os.chdir(folder)
with open(filename) as f:
nb = nbformat.reads(f.read(), nbformat.NO_CONVERT)
run_notebook(nb)
os.chdir('../')
def run_notebook(nb):
km = KernelManager()
km.start_kernel(stderr=open(os.devnull, 'w'))
kc = km.client()
kc.start_channels()
shell = kc.shell_channel
# simple ping:
kc.execute("pass")
shell.get_msg()
failures = 0
for cell in nb.cells:
if cell.cell_type != 'code':
continue
kc.execute(cell.source)
# wait for finish, maximum 120s
reply = shell.get_msg(timeout=120)['content']
if reply['status'] == 'error':
failures += 1
print("\nFAILURE:")
print('\n'.join(reply['traceback']))
print()
kc.stop_channels()
km.shutdown_kernel()
del km
if failures > 0:
raise Exception()