{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Running nosetests\n", "\n", "## Testing systems\n", "\n", "Testing systems is important as much as testing software. \n", "\n", "You should start system projects using tools like ansible (more in the next lesson), but in old legacy project it's not always possible (see Test Drive Deployment with python and nosetest from Roberto Polli ).\n", "\n", "Nose is a very simple test framework that I love to use for testing infrastructures.\n", "\n", "\n", "## Nosetest\n", "\n", "- Now run the following command from a shell\n", " \n", " # nosetests -vs 03_parsing_test.py\n", "\n", " - Nose is a test framework\n", " - Nose runs every file matching test_*\n", " - Nose runs every function matching test_*\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Test scripts are there ;)\n", "from os import chdir\n", "chdir(\"../scripts\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# %load 02_nosetests_simple.py\n", "\"\"\"Introduction to nosetests - 01\n", "\n", " Run this script with\n", " #nosetest -v scriptname.py\n", "\"\"\"\n", "\n", "\n", "def setup():\n", " print(\"This function is run before the testsuite, while\")\n", "\n", "\n", "def teardown():\n", " print(\"after all tests\")\n", "\n", "\n", "def test_one():\n", " # just name a function like test_* to execute it!\n", " a = 1\n", " assert a == 1\n", "\n", "\n", "def test_two():\n", " # This test fails and the backtrace is printed\n", " a = 2\n", " assert a == 1, \"Expecting a == 1. Was %r\" % a\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#\n", "# Let's run it!\n", "#\n", "!nosetests -v 02_nosetests_simple.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Complete Test Script: I\n", "\n", " - a more flexible script is 02_nosetests_full.py which uses a Test class\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# %load 02_nosetests_full.py\n", "\"\"\"An articulated nosetest script using classes\n", "\n", " - setup_class() and setup() increase flexibility\n", "\"\"\"\n", "\n", "import os\n", "import errno\n", "\n", "# Import assert_* tools\n", "from nose.tools import *\n", "\n", "class TestB(object):\n", "\n", " @classmethod\n", " def setup_class(self):\n", " # Run once at startup, eg. create database structure\n", " print(\"\\nsetup testsuite environment\")\n", " open(\"/tmp/test2.out\", \"w\").write(\"0\")\n", "\n", " @classmethod\n", " def teardown_class(self):\n", " # Run once at teardown, eg. drop database\n", " print(\"\\ncleanup testsuite environment\")\n", " os.unlink(\"/tmp/test2.out\")\n", "\n", " def setup(self):\n", " # Before every test, like populate a table\n", " print(\"\\nbefore_every_test\")\n", "\n", " def teardown(self):\n", " # After every test, eg truncate a table\n", " print(\"\\nafter_every_test\")\n", "\n", " def test_a(self):\n", " assert os.path.isfile(\"/tmp/test2.out\")\n", "\n", " def test_b(self):\n", " assert False\n", "\n", " #\n", " # Bonus examples:\n", " #\n", " # - testing exceptions\n", " # - parametrized tests \n", " #\n", " @raises(IOError)\n", " def test_except(self):\n", " assert open('/tmp/MISSING')\n", "\n", " def test_except_complex(self):\n", " with assert_raises(IOError) as ex:\n", " fh = open('/tmp/MISSING')\n", " # Verify exception *and* errno!\n", " assert_equals(ex.exception.errno, errno.ENOENT)\n", "\n", " def test_parametrized(self):\n", " # Runs 3 tests using the nosetest generator syntax.\n", " for i in range(3):\n", " # You have to yield a method to make it work into \n", " # a class\n", " yield self.harn_greater, i, 0 \n", "\n", " def harn_greater(self, a, b):\n", " assert_greater(a, b)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Try it!\n", "!nosetests -v -s 02_nosetests_full.py # -s shows STDOUT " ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }