Skip to content
Snippets Groups Projects
Commit 55deb252 authored by David Hendriks's avatar David Hendriks
Browse files

updating test routines

parent f45f176b
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:fd5b1a83-7212-4aca-b317-991bf289fba8 tags:
``` python
def add(a, b):
return a + b
```
%% Cell type:code id:4d842395-3e17-48e8-b613-9856365e9796 tags:
``` python
add(5, 6)
```
%% Output
11
%% Cell type:code id:f2afc967-a66a-4a47-bfc5-0d6c17826794 tags:
``` python
1 / 0
```
%% Output
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-3-bc757c3fda29> in <module>
----> 1 1 / 0
ZeroDivisionError: division by zero
%% Cell type:markdown id:8491b29d-375d-458f-8a46-fc822422d8f3 tags:
hello
%% Cell type:code id:601a89e6-5ca6-4725-8834-5e975ba76726 tags:
``` python
```
import unittest
import nbformat
import os
from nbconvert.preprocessors import ExecutePreprocessor
from binarycpython.utils.functions import temp_dir
TMP_DIR = temp_dir('testing', 'test_notebooks')
def run_notebook(notebook_path):
"""
Function to run notebooks and get the errors
"""
# https://www.blog.pythonlibrary.org/2018/10/16/testing-jupyter-notebooks/
nb_name, _ = os.path.splitext(os.path.basename(notebook_path))
dirname = os.path.dirname(notebook_path)
with open(notebook_path) as f:
nb = nbformat.read(f, as_version=4)
proc = ExecutePreprocessor(timeout=600, kernel_name='python3')
proc.allow_errors = True
proc.preprocess(nb, {'metadata': {'path': '/'}})
output_path = os.path.join(TMP_DIR, '{}_all_output.ipynb'.format(nb_name))
with open(output_path, mode='wt') as f:
nbformat.write(nb, f)
errors = []
for cell in nb.cells:
if 'outputs' in cell:
for output in cell['outputs']:
if output.output_type == 'error':
errors.append(output)
return nb, errors
class TestNotebook(unittest.TestCase):
"""
Class that contains the notebook test calls
"""
def test_example(self):
notebook_name = 'example.ipynb'
nb, errors = run_notebook(notebook_name)
msg = "\nNotebook: {}\n\n".format(notebook_name) + "\n".join(["{}: {}\n{}".format(el['ename'], el['evalue'], '\n'.join(el['traceback'])) for el in errors])
self.assertEqual(errors, [], msg=msg)
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment