{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mock\n", "\n", "[Mock-Objekte](https://de.wikipedia.org/wiki/Mock-Objekt) fördern Tests, die auf dem Verhalten von Objekten basieren. Die Python-Bibliothek [mock](https://docs.python.org/3/library/unittest.mock.html) ermöglicht euch, Teile des zu testenden Systems durch Scheinobjekte zu ersetzen und Aussagen über deren Verwendung zu treffen." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installation\n", "\n", "[mock](https://docs.python.org/3/library/unittest.mock.html) ist seit Python 3.3 in der Python-Standardbibliothek enthalten. Für ältere Versionen von Python könnt ihr sie installieren mit:\n", "\n", "```bash\n", "$ bin/python -m pip install mock\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Beispiel\n", "\n", "In unserem Beispiel wollen wir prüfen, ob die Arbeitstage von Montag bis Freitag korrekt ermittelt werden." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Zunächst importieren wir `datetime` und `Mock`:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "from unittest.mock import Mock" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Dann definieren wir zwei Testtage:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "monday = datetime(year=2021, month=10, day=11)\n", "saturday = datetime(year=2021, month=10, day=16)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Nun definieren wir eine Methode zur Überprüfung der Arbeitstage, wobei die `datetime`-Bibliothek von Python Montage als `0` und Sonntage als `6` behandelt:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def is_workingday():\n", " today = datetime.today()\n", " return (0 <= today.weekday() < 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Dann mocken wir `datetime`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "datetime = Mock()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Schließlich testen wir unsere beiden Mock-Objekte:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "datetime.today.return_value = monday\n", "assert is_workingday()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "datetime.today.return_value = saturday\n", "assert not is_workingday()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "ename": "AssertionError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[7], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m datetime\u001b[38;5;241m.\u001b[39mtoday\u001b[38;5;241m.\u001b[39mreturn_value \u001b[38;5;241m=\u001b[39m monday\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_workingday()\n", "\u001b[0;31mAssertionError\u001b[0m: " ] } ], "source": [ "datetime.today.return_value = monday\n", "assert not is_workingday()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "