Skip to content

Commit 2019825

Browse files
committed
use pytest to automatically run notebooks
1 parent b36b765 commit 2019825

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

planet-notebook-docker/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,5 @@ RUN apt-get remove --yes g++ gcc ssh git make curl && \
112112
rm -rf /var/lib/{apt,dpkg,cache,log}/
113113

114114
USER $NB_USER
115+
116+
RUN pip install pytest

tests/test_notebooks.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import subprocess
3+
4+
# import nbformat
5+
# from nbconvert.preprocessors import ExecutePreprocessor
6+
import pytest
7+
8+
ROOT_DIR = '.'
9+
10+
11+
def find_notebooks(root_dir):
12+
print(os.listdir(root_dir))
13+
notebook_ext = '.ipynb'
14+
15+
notebooks = []
16+
for (dirpath, dirnames, filenames) in os.walk(root_dir):
17+
# look for 'norun' file, if present, do not look for notebooks in this dir
18+
if 'norun' in filenames:
19+
# do not look into subdirectories, modify in-place for walk()
20+
dirnames[:] = []
21+
22+
# clear filenames
23+
filenames = []
24+
25+
# modify in place to filter subdirectories in walk()
26+
# https://stackoverflow.com/questions/19859840/excluding-directories-in-os-walk
27+
dirnames[:] = [n for n in dirnames if n != '.ipynb_checkpoints']
28+
29+
for name in filenames:
30+
ext = os.path.splitext(name)[1]
31+
if ext == notebook_ext:
32+
# convert to string so test displays the path
33+
notebook_name = str(os.path.join(dirpath, name))
34+
print(notebook_name)
35+
print(type(notebook_name))
36+
notebooks.append(notebook_name)
37+
38+
return notebooks
39+
40+
41+
@pytest.mark.parametrize("notebook", find_notebooks(ROOT_DIR))
42+
def test_run_notebooks(notebook):
43+
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
44+
"--ExecutePreprocessor.timeout=600",
45+
notebook]
46+
subprocess.check_call(args)

0 commit comments

Comments
 (0)