I wrote my first Nose plugin this weekend and I’ve got to say it was dead simple.

I was looking around for ways to keep Nose from searching for tests in certain directories. You see Nose is a nosey little critter that will scour every nook and cranny of your directory structure looking for unittests to run. Really, it wants your code to work and it loves making dots. But, see I knew better and knew if that little Nose test discoverer ventured down a few rabbit holes it would end up in a world of pain–segfaults or worse. I just wanted to avoid the whole mess and just have Nose exclude a few directories from its massive testhunt.

I asked a few people I know who use Nose regularly about my options. “Write your own plugin,” was the consensus. Nose’s architecture makes it very easy to write plugins for just about every aspect of its behavior…check out its plugin api. The project documentation is also very helpful in this regard.

I spent most of my time trying to figure out how to test my testing plugin. In the end it only took a couple of hours to go from Nose novice to having written a packaged, testable Nose plugin. So, here’s a quick example of how you would use this new nose-exclude plugin:

 $ ls test_dir
dir_with_tests    dir_with_bad_tests
there_be_dragons_here    more_tests

In this example, I want Nose to ignore a couple directories and not even bother searching them. You would run:

 $ nosetests --exclude-dir=test_dir/dir_with_bad_tests \
 --exclude-dir=test_dir/there_be_dragons_here test_dir

You could further specify to exclude subdirectories if you wanted that level of control. There is also an –exclude-dir-file= option available that allows you to specify a file containing paths to be excluded.

$ nosetests --exclude-dir-file=exclude_dirs.txt test_dir

nose-exclude is pretty simple and covers a fairly basic use case so hopefully others will find it useful. For now nose-exclude is available on bitbucket, but will be up on PyPI shortly.