Some sugar for your python tests using nose, sniffer and some other good stuff

This short tutorial will introduce you to the basics of testing your python code using nosetests. It is meant as an introduction only. I'll assume you know about unit testing, how to lay out your tests etc etc. Here I'm really just introducing you to some useful tools to make your TDD process more fun

For the purposes of this tutorial we'll be testing the following code:

class Foo:

    def bar(self):
        return "bar"

import unittest  

class FooTestCase(unittest.TestCase):

    def test_bar(self):
        """Bar method returns "bar" """
        assert Foo().bar() == "bar", "Should return bar"

Setting up

As per usual, create a virtualenv and install what you need on there:

virtualenv-3.4 /path/to/env  
source /path/to/env/bin/activate  

We'll then install some dependencies:

You can install all of those into your venv with pip.

Get better output with pinocchio

The main thing I use pinocchio for is the spec output that it can render. For example:

nosetests --with-spec --with-color  

Gives us:

Foo test case  
- Bar method returns "bar"

Ran 1 test in 0.004s


Notice how it uses our docstring for foo() in the spec description.

Autorun the tests with Sniffer

Sniffer allows us to run our tests automatically every time we make a change. Usage is simple. You can just type sniffer. However, we want to also pass in some arguments to our nosetests. You can do this by prefixing the command with -x. For example:

sniffer -x--with-spec -x--spec-color  

Now, whenever we make changes to our code, sniffer will re-run the tests. \o/