Okay, this took me far too long to figure this out. Mainly due to my own misunderstanding of Python terminology regarding modules (For the record, a ‘module’ is a file not a directory of python files) and mainly because I couldn’t find a specific example for what I wanted to achieve. My goal was this: Given a directory full of unit tests, run all the unit tests that can be found. I wanted to be able to drop in new unit tests without having to include them somewhere.

This is what I managed to come up with:

#!/usr/bin/python

import unittest2 as unittest

if __name__ == "__main__":
    all_tests = unittest.TestLoader().discover('tests', pattern='*.py')
    unittest.TextTestRunner().run(all_tests)

It uses the (apparently) new discover method. I’m still using Python 2.6 which doesn’t have access to this method so I had to install unittest2 which is a backport from Python 2.7. If you do happen to be running Python 2.7, you can simply run this (found in the unittest documentation):

python -m unittest discover project_directory ‘*.py’

4 hours later and it’s 2 lines of code (WHICH I KNEW IT WOULD BE! AAARGH!). No matter. I won’t forget my module lesson in a hurry.