|
| 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