{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Doctests" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Trying:\n", " add(7,6)\n", "Expecting:\n", " 13\n", "ok\n", "1 items had no tests:\n", " __main__\n", "1 items passed all tests:\n", " 1 tests in __main__.add\n", "1 tests in 2 items.\n", "1 passed and 0 failed.\n", "Test passed.\n" ] }, { "data": { "text/plain": [ "TestResults(failed=0, attempted=1)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import doctest\n", "\n", "\n", "def add(a, b):\n", " \"\"\"\n", " This is a test:\n", " >>> add(7,6)\n", " 13\n", " \"\"\"\n", " return a + b\n", "\n", "\n", "doctest.testmod(verbose=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Debugging" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import doctest\n", "\n", "\n", "doctest.testmod()\n", "\n", "\n", "def multiply(a, b):\n", " \"\"\"\n", " This is a test:\n", " >>> multiply(2, 2)\n", " 5\n", " \"\"\"\n", " import pdb\n", "\n", " pdb.set_trace()\n", " return a * b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. `import pdb` importiert den Python Debugger\n", "2. `pdb.set_trace()` erstellt einen sog. *Breakpoint*, der den Python-Debugger startet.\n", "\n", "
\n", "\n", "**See also:**\n", "\n", "* [pdb – The Python Debugger](https://docs.python.org/3/library/pdb.html)\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.11 Kernel", "language": "python", "name": "python311" }, "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.11.4" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }