{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Debuging example: make the unittests work" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Code with bugs in...\n", "def textToHex(text):\n", " \"\"\"This code should convert ASCII text to the unicode hex\"\"\"\n", " out = []\n", " for c in text:\n", " out.append(ord(c))\n", " return \" \".join(\"{:x}\".format(i) for i in out)\n", "\n", "def runningAverage(data,window):\n", " \"\"\"Take the running average of a set of numbers, with a given window\"\"\"\n", " out = [0. for i in range(len(data))]\n", " for i in range(len(data)):\n", " for j in range(i-window,i+window+1):\n", " if( j >= 0 and j < len(data) ):\n", " out[i] += data[j]\n", " out[i] /= (2*window + 1)\n", " return out\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run tests in the following cell" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "FF.\n", "======================================================================\n", "FAIL: testRunningAverage (__main__.ExampleCode)\n", "Test the running average function with a window of one\n", "----------------------------------------------------------------------\n", "Traceback (most recent call last):\n", " File \"\", line 33, in testRunningAverage\n", " self.assertEqual(runningAverage(data,window),rData)\n", "AssertionError: Lists differ: [1.1666666666666667, 2.4, 5.73... != [1.75, 2.4, 5.733333333333333,...\n", "\n", "First differing element 0:\n", "1.1666666666666667\n", "1.75\n", "\n", "- [1.1666666666666667, 2.4, 5.733333333333333, 4.8999999999999995]\n", "+ [1.75, 2.4, 5.733333333333333, 7.35]\n", "\n", "======================================================================\n", "FAIL: testTextToHex (__main__.ExampleCode)\n", "Test the textToHex function above\n", "----------------------------------------------------------------------\n", "Traceback (most recent call last):\n", " File \"\", line 14, in testTextToHex\n", " self.assertEqual(textToHex(\"Hello world\"), '0x48 0x65 0x6c 0x6c 0x6f 0x20 0x77 0x6f 0x72 0x6c 0x64')\n", "AssertionError: '48 65 6c 6c 6f 20 77 6f 72 6c 64' != '0x48 0x65 0x6c 0x6c 0x6f 0x20 0x77 0x6f 0x72 0x6c 0x64'\n", "\n", "----------------------------------------------------------------------\n", "Ran 3 tests in 0.004s\n", "\n", "FAILED (failures=2)\n" ] } ], "source": [ "# The unittest suite allows you to write tests that your code must pass\n", "# After every change to the code you can test to check the code still works\n", "import unittest \n", "\n", "class ExampleCode(unittest.TestCase):\n", " \"\"\"Example of how to use unittest in Jupyter.\"\"\"\n", " \n", " def testTrue(self):\n", " \"\"\"A simple test that should always pass\"\"\"\n", " self.assertEqual('foo'.upper(), 'FOO')\n", " \n", " def testTextToHex(self):\n", " \"\"\"Test the textToHex function above\"\"\"\n", " self.assertEqual(textToHex(\"Hello world\"), '0x48 0x65 0x6c 0x6c 0x6f 0x20 0x77 0x6f 0x72 0x6c 0x64')\n", " \n", " def testRunningAverage(self):\n", " \"\"\"Test the running average function with a window of zero\"\"\"\n", " # some input data\n", " data = [1.,2.5,3.7,11.]\n", " # a window of one either side of the value for average\n", " window = 0\n", " # expected output is the same as the input \n", " self.assertEqual(runningAverage(data,window),data)\n", "\n", " def testRunningAverage(self):\n", " \"\"\"Test the running average function with a window of one\"\"\"\n", " # some input data\n", " data = [1.,2.5,3.7,11.]\n", " # a window of one either side of the value for average\n", " window = 1\n", " # expected output (note corrections at the ends of the array)\n", " rData = [(1.+2.5)/2, (1.+2.5+3.7)/3, (2.5+3.7+11.)/3, (3.7+11.)/2]\n", " self.assertEqual(runningAverage(data,window),rData)\n", "\n", "if __name__ == '__main__':\n", " unittest.main(argv=['first-arg-is-ignored'], exit=False)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "ename": "ImportError", "evalue": "cannot import name 'fit'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mscipy\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mfit\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mImportError\u001b[0m: cannot import name 'fit'" ] } ], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }