Created
June 19, 2019 15:32
-
-
Save bd2357/bf0536644018e89d97f86b0bd1713201 to your computer and use it in GitHub Desktop.
Testing and Debugging Jupyter Notebooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Example of DOCTEST in Jupyter Notebook\n", | |
"===================================\n", | |
"[Based on this page](https://kolesnikov.ga/Testing_and_Debugging_Jupyter_Notebooks/)\n", | |
"\n", | |
"Just add doctest as normal\n", | |
"Then add a doctest runner at the bottom of the notebook (see import doctest below)\n", | |
"\n", | |
"Inserting a python debugger trace point will get triggered in doctest or unittest" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def add(a, b):\n", | |
" \"\"\"Return the sum of a and b.\n", | |
" \n", | |
" >>> add(3, 4)\n", | |
" 7\n", | |
" \"\"\"\n", | |
" sum = a + b\n", | |
" #import pdb; pdb.set_trace() # to break into debugger\n", | |
" return sum\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Example of UNITTEST in Jupyter Notebook\n", | |
"===================================\n", | |
"\n", | |
"Write the test class and your tests. The Class runner can be put at the bottom of notebook to collect all the test classes." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import unittest\n", | |
"\n", | |
"class TestAdd(unittest.TestCase):\n", | |
" def test_add(self):\n", | |
" self.assertEqual(add(2,3), 5)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Another Class to test\n", | |
"------------------------------" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def mult(a, b):\n", | |
" \"\"\"Return the product of a and b.\n", | |
" \n", | |
" >>> mult(3, 4)\n", | |
" 12\n", | |
" \"\"\"\n", | |
" return a*b" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"A Second Test Class\n", | |
"-----------------------------\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class TestMult(unittest.TestCase):\n", | |
" def test_mult(self):\n", | |
" self.assertEqual(mult(2,3), 6)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Running the testmod() function will run all the Doctests in the notebook" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"TestResults(failed=0, attempted=2)" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import doctest\n", | |
"doctest.testmod()\n", | |
"#doctest.testmod(verbose=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Running the unittest.main() with the special empty argv and exit=False so it won't halt the Notebook\n", | |
"Setting verbosity will show more." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"..\n", | |
"----------------------------------------------------------------------\n", | |
"Ran 2 tests in 0.003s\n", | |
"\n", | |
"OK\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<unittest.main.TestProgram at 0x6272940>" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#unittest.main(argv=[''], verbosity=2, exit=False)\n", | |
"unittest.main(argv=[''], exit=False)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"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.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment