{ "cells": [ { "cell_type": "markdown", "id": "995ec942", "metadata": {}, "source": [ "# Hypothesis\n", "\n", "[Hypothesis](https://hypothesis.readthedocs.io/en/latest/) ist eine Bibliothek, mit der ihr Tests schreiben könnt, die anhand einer Quelle von Beispielen parametrisiert werden. Anschließend werden einfache und nachvollziehbare Beispiele generiert, anhand derer eure Tests fehlschlagen können und ihr mit geringen Aufwänden Fehler finden könnt." ] }, { "cell_type": "markdown", "id": "ff87e099", "metadata": {}, "source": [ "## Beispiel\n", "\n", "Zum Testen von Listen mit Fließkommazahlen werden viele Beispiele ausprobiert, jedoch im Report nur ein einfaches Beispiel für jeden Bug (eindeutiger exception type und Position) angegeben:" ] }, { "cell_type": "code", "execution_count": 1, "id": "d897c0b9", "metadata": {}, "outputs": [], "source": [ "from hypothesis import given\n", "from hypothesis.strategies import lists, floats" ] }, { "cell_type": "code", "execution_count": 2, "id": "1a1f22e3", "metadata": {}, "outputs": [], "source": [ "# Add ipython magics\n", "import ipytest\n", "import pytest\n", "\n", "\n", "ipytest.autoconfig()" ] }, { "cell_type": "code", "execution_count": 3, "id": "eef3a5c2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "F [100%]\n", "============================================= FAILURES =============================================\n", "____________________________________________ test_mean _____________________________________________\n", "\n", " @given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))\n", "> def test_mean(ls):\n", "\n", "/var/folders/hk/s8m0bblj0g10hw885gld52mc0000gn/T/ipykernel_37502/1742712940.py:2: \n", "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \n", "\n", "ls = [1.922532935891866e+307, 1.797693134860272e+308]\n", "\n", " @given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))\n", " def test_mean(ls):\n", " mean = sum(ls) / len(ls)\n", "> assert min(ls) <= mean <= max(ls)\n", "E assert inf <= 1.797693134860272e+308\n", "E + where 1.797693134860272e+308 = max([1.922532935891866e+307, 1.797693134860272e+308])\n", "E Falsifying example: test_mean(\n", "E ls=[1.922532935891866e+307, 1.797693134860272e+308],\n", "E )\n", "\n", "/var/folders/hk/s8m0bblj0g10hw885gld52mc0000gn/T/ipykernel_37502/1742712940.py:4: AssertionError\n", "========================================= warnings summary =========================================\n", "../../../../../../.local/share/virtualenvs/python-311-6zxVKbDJ/lib/python3.11/site-packages/_pytest/config/__init__.py:1204\n", " /srv/jupyter/.local/share/virtualenvs/python-311-6zxVKbDJ/lib/python3.11/site-packages/_pytest/config/__init__.py:1204: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: hypothesis\n", " self._mark_plugins_for_rewrite(hook)\n", "\n", "-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html\n", "===================================== short test summary info ======================================\n", "FAILED t_c7729a99e261490d9a679829703295f9.py::test_mean - assert inf <= 1.797693134860272e+308\n", "1 failed, 1 warning in 2.14s\n" ] } ], "source": [ "%%ipytest\n", "\n", "@given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))\n", "def test_mean(ls):\n", " mean = sum(ls) / len(ls)\n", " assert min(ls) <= mean <= max(ls)" ] }, { "cell_type": "markdown", "id": "967345d5", "metadata": {}, "source": [ "## Installation\n", "\n", "``` bash\n", "$ uv add hypothesis\n", "```\n", "\n", "Alternativ kann Hypothesis auch mit Erweiterungen installiert werden, z.B.:\n", "\n", "``` bash\n", "$ uv add hypothesis[numpy,pandas]\n", "```" ] }, { "cell_type": "markdown", "id": "0da1b6a6", "metadata": {}, "source": [ "
\n", "\n", "**Bemerkung:**\n", "\n", "Falls ihr uv noch nicht installiert habt, findet ihr eine Anleitung hierzu unter [Jupyter Notebook installieren](../install.rst).\n", "
\n", "
\n", "\n", "**Siehe auch:**\n", "\n", "* [Hypothesis for the Scientific Stack](https://hypothesis.readthedocs.io/en/latest/numpy.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 } }, "nbformat": 4, "nbformat_minor": 5 }