diff --git a/binarycpython/tests/example.ipynb b/binarycpython/tests/example.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..57ce0148138a19e0c9cffd6872673c3f44d627d7 --- /dev/null +++ b/binarycpython/tests/example.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "fd5b1a83-7212-4aca-b317-991bf289fba8", + "metadata": {}, + "outputs": [], + "source": [ + "def add(a, b):\n", + " return a + b" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "4d842395-3e17-48e8-b613-9856365e9796", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add(5, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "f2afc967-a66a-4a47-bfc5-0d6c17826794", + "metadata": {}, + "outputs": [ + { + "ename": "ZeroDivisionError", + "evalue": "division by zero", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-3-bc757c3fda29>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" + ] + } + ], + "source": [ + "1 / 0" + ] + }, + { + "cell_type": "markdown", + "id": "8491b29d-375d-458f-8a46-fc822422d8f3", + "metadata": {}, + "source": [ + "hello" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "601a89e6-5ca6-4725-8834-5e975ba76726", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/binarycpython/tests/test_notebooks.py b/binarycpython/tests/test_notebooks.py new file mode 100644 index 0000000000000000000000000000000000000000..61c809651c6aee1a04dab9fe865d12305c56d91d --- /dev/null +++ b/binarycpython/tests/test_notebooks.py @@ -0,0 +1,58 @@ +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()