{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Unittests" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def add(a, b):\n", " return a + b" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "test_add (__main__.TestNotebook.test_add) ... FAIL\n", "\n", "======================================================================\n", "FAIL: test_add (__main__.TestNotebook.test_add)\n", "----------------------------------------------------------------------\n", "Traceback (most recent call last):\n", " File \"/var/folders/hk/s8m0bblj0g10hw885gld52mc0000gn/T/ipykernel_33771/2216555184.py\", line 6, in test_add\n", " self.assertEqual(add(2, 2), 5)\n", "AssertionError: 4 != 5\n", "\n", "----------------------------------------------------------------------\n", "Ran 1 test in 0.001s\n", "\n", "FAILED (failures=1)\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import unittest\n", "\n", "\n", "class TestNotebook(unittest.TestCase):\n", " def test_add(self):\n", " self.assertEqual(add(2, 2), 5)\n", "\n", "\n", "unittest.main(argv=[\"\"], verbosity=2, exit=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternativ kann auch [ipython-unittest](https://github.com/JoaoFelipe/ipython-unittest) verwendet werden. Dies ermöglicht die Verwendung der folgenden *Cell Magics* in iPython:\n", "\n", "* `%%unittest_main` führt Testfälle aus, die in einer Zelle definiert sind\n", "* `%%unittest_testcase` erstellt einen Testfall mit der in einer Zelle definierten Funktion und führt ihn aus\n", "* `%%unittest` konvertiert Python `assert` in Unittest-Funktionen\n", "* `%%external` um externe Unittests durchzuführen\n", "* `%%write {mode}` um externe Dateien zu schreiben" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%reload_ext ipython_unittest" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/unittest.status+json": { "color": "yellow", "message": "", "previous": 0 }, "text/plain": [] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/unittest.status+json": { "color": "lightgreen", "message": "..\n----------------------------------------------------------------------\nRan 2 tests in 0.000s\n\nOK\n", "previous": 0 }, "text/plain": [ "Success" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "..\n", "----------------------------------------------------------------------\n", "Ran 2 tests in 0.000s\n", "\n", "OK\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%unittest_main\n", "class MyTest(unittest.TestCase):\n", " def test_1_plus_1_equals_2(self):\n", " sum = 1 + 1\n", " self.assertEqual(sum, 2)\n", "\n", " def test_2_plus_2_equals_4(self):\n", " self.assertEqual(2 + 2, 4)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/unittest.status+json": { "color": "yellow", "message": "", "previous": 0 }, "text/plain": [] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/unittest.status+json": { "color": "lightgreen", "message": "..\n----------------------------------------------------------------------\nRan 2 tests in 0.000s\n\nOK\n", "previous": 0 }, "text/plain": [ "Success" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "..\n", "----------------------------------------------------------------------\n", "Ran 2 tests in 0.000s\n", "\n", "OK\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%unittest_testcase\n", "def test_1_plus_1_equals_2(self):\n", " sum = 1 + 1\n", " self.assertEqual(sum, 2)\n", "\n", "def test_2_plus_2_equals_4(self):\n", " self.assertEqual(2 + 2, 4)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/unittest.status+json": { "color": "yellow", "message": "", "previous": 0 }, "text/plain": [] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/unittest.status+json": { "color": "lightgreen", "message": "..\n----------------------------------------------------------------------\nRan 2 tests in 0.000s\n\nOK\n", "previous": 0 }, "text/plain": [ "Success" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "..\n", "----------------------------------------------------------------------\n", "Ran 2 tests in 0.000s\n", "\n", "OK\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%unittest\n", "\"1 plus 1 equals 2\"\n", "sum = 1 + 1\n", "assert sum == 2\n", "\"2 plus 2 equals 4\"\n", "assert 2 + 2 == 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Standardmäßig trennt Docstring in dieser Magie die Unittest-Methoden. Wenn jedoch keine Docstrings verwendet werden, erstellen die *Cell Magics* für jede `assert`-Methode." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Diese *Cell Magics* unterstützen optionale Argumente:\n", "\n", "* `-p` (`--previous`) `P`\n", "\n", " setzt den Cursor auf P Zellen vor (Standardwert: `-1` entspricht der nächsten Zelle)\n", "\n", " Dies funktioniert jedoch nur, wenn auch [jupyter_dojo](https://github.com/JoaoFelipe/jupyter-dojo) installiert ist.\n", " \n", " \n", "* `-s` (`--stream`) `S`\n", "\n", " legt den *Ooutput-Stream* fest (Standardwert: `sys.stdout`)\n", " \n", " \n", "* `-t` (`--testcase`) `T`\n", "\n", " definiert den Namen des TestCase für `%%unittest` und `%%unittest_testcase`\n", " \n", " \n", "* `-u` (`--unparse`)\n", "\n", " gibt den Quellcode nach den Transformationen aus\n", "\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 }