{ "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", "