Tips on using Nose
Tools
nose provides a nice module call nose.tools.py which minics methods of the TestCase class, and more!
for example checking for an exception in the unittest package will be something like this
from unittest import TestCase
class TestMyApp(TestCase):
def test_create(self):
...
self.failUnlessRaises(NotImplementedError, source.create)
in nose you will use nose.tools like this
import nose.tools
@nose.tools.raises(NotImplementedError)
def test_create():
...
by the way raises takes a list too so you can invoke it as
@raises(TypeError, ValueError)
many other nice things are there so be sure to check it out tools.py
Using the -w flag
The -w flag seems to be pretty much essential for turbogears projects, and probably for most other large python projects (including Nose itself it seems). This option tells nose to search for tests within a certain path only. It is described in the documentation as "Changing the Working Directory", however this does not refer to the working directory in the sense you would expect, ie. if you include "print os.getcwd()" in one of your tests, it will print the directory you are running nose from. Bear this in mind when you're importing modules in your tests.
Using module style test names
If you want to use the package.module:Class.method style nameing (and for the next tip you do) be aware that if you have used the -w flag then these modules are searched for relative to the directory(s) you provided.
Emacs integration with the PDB plugin
Those last two points weren't very exciting, but I had to understand them before I could make this work. Emacs has special support for the PDB debugger, which jumps around your source code as you debug your program. If you have a test that is failing, this is obviously exactly what you want. Unfortunately you need to be quite careful to give emacs a command line that will work. The Grand Unified Debugger (GUD) mode will look for filenames in the commandline you pass it, and try to change to that directory. This may be very useful for gdb, but in python modules are located relative to the current working directory, so your tests will fail to import code. The aim is therefore to avoid anything that looks like a path to a file in a different directory in your commandline. Something like the following should work:
type "M-x pdb" Emacs will prompt you: "Run pdb like this: pdb" Delete pdb, and instead type your nosetests commandline: "nosetests --pdb -wpathtoyourtests testmodule:TestClass" or something of the sort. It's important not to have a space in the -w flag, or emacs will change to the path before running nosetests, and you don't want that.
Automated testing of code with nosy
nosy is a little python script that will run nose whenever your source changes.
Eclipse automated testing
If you want to automatically test your code with eclipse, here is a recipe.
