{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "TestingAndDebugging.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "javascript", "language": "javascript", "display_name": "Javascript (Node.js)" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "l03yqyWznTcX", "colab_type": "text" }, "source": [ "# [Testing and Debugging Jupyter Notebooks](https://kolesnikov.ga/Testing_and_Debugging_Jupyter_Notebooks/)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "AkIXccj9npFw", "colab_type": "text" }, "source": [ "## Running Example" ] }, { "cell_type": "code", "metadata": { "id": "5M7kYQYtnRdg", "colab_type": "code", "colab": {} }, "source": [ "def add(a, b):\n", " \"\"\"Return the sum of a and b.\"\"\"\n", " sum = a\n", " return sum\n" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "hsUO8IYtnvTo", "colab_type": "text" }, "source": [ "## Doctest" ] }, { "cell_type": "code", "metadata": { "id": "gWXNlxJanxhi", "colab_type": "code", "outputId": "c3632f18-b676-482e-f711-450029efc0ad", "executionInfo": { "status": "ok", "timestamp": 1572433329178, "user_tz": -540, "elapsed": 881, "user": { "displayName": "Kakigi Katuyuki", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mAPb3OlX2ysmc2JV2nXUX_C3MVRAV6O3-HkTzd6=s64", "userId": "00784428381987829532" } }, "colab": { "base_uri": "https://localhost:8080/", "height": 395 } }, "source": [ "def add(a, b):\n", " \"\"\"Return the sum of a and b.\n", "\n", " >>> add(2, 2)\n", " 4\n", " \"\"\"\n", " sum = a\n", " return sum\n", " \n", "import doctest\n", "doctest.testmod(verbose=True)" ], "execution_count": 3, "outputs": [ { "output_type": "stream", "text": [ "Trying:\n", " add(2, 2)\n", "Expecting:\n", " 4\n", "**********************************************************************\n", "File \"__main__\", line 4, in __main__.add\n", "Failed example:\n", " add(2, 2)\n", "Expected:\n", " 4\n", "Got:\n", " 2\n", "1 items had no tests:\n", " __main__\n", "**********************************************************************\n", "1 items had failures:\n", " 1 of 1 in __main__.add\n", "1 tests in 2 items.\n", "0 passed and 1 failed.\n", "***Test Failed*** 1 failures.\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "TestResults(failed=1, attempted=1)" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "cell_type": "markdown", "metadata": { "id": "2quaRHFZn5xy", "colab_type": "text" }, "source": [ "## Unittest" ] }, { "cell_type": "code", "metadata": { "id": "7-Vm_pZ5n7qt", "colab_type": "code", "outputId": "73b6c9d5-1d47-495c-950b-7dd07a9e52c4", "executionInfo": { "status": "ok", "timestamp": 1572321410496, "user_tz": -540, "elapsed": 926, "user": { "displayName": "Kakigi Katuyuki", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mAPb3OlX2ysmc2JV2nXUX_C3MVRAV6O3-HkTzd6=s64", "userId": "00784428381987829532" } }, "colab": { "base_uri": "https://localhost:8080/", "height": 272 } }, "source": [ "import unittest\n", "\n", "\n", "class TestNotebook(unittest.TestCase):\n", "\n", " def test_add(self):\n", " self.assertEqual(add(2, 2), 5)\n", "\n", "unittest.main(argv=[''], verbosity=2, exit=False)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "test_add (__main__.TestNotebook) ... FAIL\n", "\n", "======================================================================\n", "FAIL: test_add (__main__.TestNotebook)\n", "----------------------------------------------------------------------\n", "Traceback (most recent call last):\n", " File \"\", line 7, in test_add\n", " self.assertEqual(add(2, 2), 5)\n", "AssertionError: 2 != 5\n", "\n", "----------------------------------------------------------------------\n", "Ran 1 test in 0.003s\n", "\n", "FAILED (failures=1)\n" ], "name": "stderr" }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": { "tags": [] }, "execution_count": 4 } ] }, { "cell_type": "markdown", "metadata": { "id": "Jx9HLmkAoBoO", "colab_type": "text" }, "source": [ "## Debugging a Failed Test" ] }, { "cell_type": "code", "metadata": { "id": "KI8xVY1eoEwZ", "colab_type": "code", "outputId": "94113eb6-7a80-476c-c764-d9db40f0c216", "executionInfo": { "status": "ok", "timestamp": 1572433412853, "user_tz": -540, "elapsed": 9336, "user": { "displayName": "Kakigi Katuyuki", "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mAPb3OlX2ysmc2JV2nXUX_C3MVRAV6O3-HkTzd6=s64", "userId": "00784428381987829532" } }, "colab": { "base_uri": "https://localhost:8080/", "height": 719 } }, "source": [ "def add(a, b):\n", " \"\"\"Return the sum of a and b.\n", "\n", " >>> add(2, 2)\n", " 4\n", " \"\"\"\n", " sum = a\n", " import pdb; pdb.set_trace()\n", " return sum\n", " \n", "import doctest\n", "doctest.testmod(verbose=True)" ], "execution_count": 4, "outputs": [ { "output_type": "stream", "text": [ "Trying:\n", " add(2, 2)\n", "Expecting:\n", " 4\n", "> (9)add()\n", "-> return sum\n", "(Pdb) sum\n", "2\n", "(Pdb) q\n", "**********************************************************************\n", "File \"__main__\", line 4, in __main__.add\n", "Failed example:\n", " add(2, 2)\n", "Exception raised:\n", " Traceback (most recent call last):\n", " File \"/usr/lib/python3.6/doctest.py\", line 1330, in __run\n", " compileflags, 1), test.globs)\n", " File \"\", line 1, in \n", " add(2, 2)\n", " File \"\", line 9, in add\n", " return sum\n", " File \"\", line 9, in add\n", " return sum\n", " File \"/usr/lib/python3.6/doctest.py\", line 379, in trace_dispatch\n", " return pdb.Pdb.trace_dispatch(self, *args)\n", " File \"/usr/lib/python3.6/bdb.py\", line 51, in trace_dispatch\n", " return self.dispatch_line(frame)\n", " File \"/usr/lib/python3.6/bdb.py\", line 70, in dispatch_line\n", " if self.quitting: raise BdbQuit\n", " bdb.BdbQuit\n", "1 items had no tests:\n", " __main__\n", "**********************************************************************\n", "1 items had failures:\n", " 1 of 1 in __main__.add\n", "1 tests in 2 items.\n", "0 passed and 1 failed.\n", "***Test Failed*** 1 failures.\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "TestResults(failed=1, attempted=1)" ] }, "metadata": { "tags": [] }, "execution_count": 4 } ] } ] }