views:

125

answers:

2

I have directories, which contain files named like: 'test_foo.py'

Each file is a test case.

I would like to 1) run all the tests in a directory from the command line. I am using unittest2, since we are running Python 2.5.1. From one of these directories I tried typing this at the commandline:

python -m unittest2 discover -p 'test_*.py'

and several different variants. I get no error, but nothing happens. I was expecting all the tests within all the test cases in that directory to run and get results.

2) I also tried having a script in the directory where I did this:

loader = unittest2.TestLoader()
t = loader.discover('.')

If I print the t variable, I can see my test cases, but from the docs I can't figure out what to do with the loader object once I have it.

+1  A: 

I highly recommend using nose for this. It will detect and run your tests.

anthony
I have seen this. Why do people recommend it, because unittest is so clunky? I ultimately want to use this with Selenium RC. Any drawbacks to nose?
Aaron
nose has a huge headstart on unittest2. Until recently, unittest had no discovery, so third-party tools like nose were the only game in town.
Ned Batchelder
I am now using nose. Yes it is much much easier to use than unittest.
Aaron
+1  A: 

Give how you're trying to use unittest2 from the command line on Python< 2.7`, I think you may have missed the note on this page:

Note

Command line usage

In Python 2.7 you invoke the unittest command line features (including test discover) with python -m unittest . As unittest is a package, and the ability to invoke packages with python -m ... is new in Python 2.7, we can't do this for unittest2.

Instead unittest2 comes with a script unit2. Command line usage:

unit2 discover unit2 -v test_module There is also a copy of this script called unit2.py, useful for Windows which uses file-extensions rather than shebang lines to determine what program to execute files with. Both of these scripts are installed by distutils.

Have you tried the unit2 script which this note recommends as the alternative for older Pythons to the "run package as main script" feature of Python 2.7? Maybe its sources could also be useful to find out exactly how to discover-and-run tests from your own code, if that's what you'd rather do.

Alex Martelli
Thanks Alex! I missed that note. And now it works. Have you used nose? Do you recommend it?Great books btw, thanks for writing them.
Aaron
@Aaron, you're welcome! Yes, I've happily used nose in the past (I yet don't know whether I'll _replace_ or _supplement_ it with the new unittest functionality -- I'm not all that familiar with the latter yet!).
Alex Martelli