|
| 1 | +import os |
1 | 2 | from pathlib import Path |
2 | 3 | import subprocess |
3 | | -import tempfile |
| 4 | +from tempfile import TemporaryDirectory |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 |
|
|
9 | 10 | # From https://blog.thedataincubator.com/2016/06/testing-jupyter-notebooks/ |
10 | 11 |
|
11 | 12 |
|
12 | | -def _notebook_run(nb_file): |
13 | | - """Execute a notebook via nbconvert and collect output. |
14 | | - :returns (parsed nb object, execution errors) |
15 | | - """ |
16 | | - with tempfile.NamedTemporaryFile(suffix=".ipynb", mode='w+t') as fout: |
17 | | - subprocess.check_call([ |
18 | | - "jupyter", "nbconvert", "--to", "notebook", |
19 | | - "--execute", "--ExecutePreprocessor.timeout=500", |
20 | | - "--output", fout.name, nb_file, |
21 | | - ]) |
22 | | - fout.seek(0) |
23 | | - nb = nbformat.read(fout, nbformat.current_nbformat) |
| 13 | +def test_ipynb(): |
| 14 | + nb_path = Path(__file__).parent / 'test_nbagg_01.ipynb' |
| 15 | + |
| 16 | + with TemporaryDirectory() as tmpdir: |
| 17 | + out_path = Path(tmpdir, "out.ipynb") |
| 18 | + subprocess.check_call( |
| 19 | + ["jupyter", "nbconvert", "--to", "notebook", |
| 20 | + "--execute", "--ExecutePreprocessor.timeout=500", |
| 21 | + "--output", str(out_path), str(nb_path)], |
| 22 | + env={**os.environ, "IPYTHONDIR": tmpdir}) |
| 23 | + with out_path.open() as out: |
| 24 | + nb = nbformat.read(out, nbformat.current_nbformat) |
24 | 25 |
|
25 | 26 | errors = [output for cell in nb.cells if "outputs" in cell |
26 | 27 | for output in cell["outputs"] |
27 | 28 | if output.output_type == "error"] |
28 | | - return nb, errors |
29 | | - |
30 | | - |
31 | | -def test_ipynb(): |
32 | | - nb, errors = _notebook_run(Path(__file__).parent / 'test_nbagg_01.ipynb') |
33 | | - assert errors == [] |
| 29 | + assert not errors |
0 commit comments