55

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 command line:

     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 documentation I can't figure out what to do with the loader object once I have it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aaron
  • 3,249
  • 4
  • 35
  • 51

3 Answers3

91

I ran into the same issue when running python -m unittest discover. Here is a good checklist to verify your setup. Nose is more flexible with the allowed configurations, but not necessarily better.

  1. Make sure all files/directories start with test. Do not use test-something.py, since that is not a valid Python module name. Use test_something.py.

  2. If you are putting your tests in a sub-directory (e.g. test/), make sure you create a test/__init__.py file so python will treat the directory as a package.

  3. All class test cases definitions must be extend unittest.TestCase. For example,

    class DataFormatTests(unittest.TestCase)
    
  4. All test cases methods definitions must start with test_

     def test_data_format(self):
    
MrBartusek
  • 19
  • 1
  • 6
cmcginty
  • 113,384
  • 42
  • 163
  • 163
61

Once you have discovered tests, you can run them with a test runner.

For Python 2:

import unittest2
loader = unittest2.TestLoader()
tests = loader.discover('.')
testRunner = unittest2.runner.TextTestRunner()
testRunner.run(tests)

For Python 3:

import unittest
loader = unittest.TestLoader()
tests = loader.discover('.')
testRunner = unittest.runner.TextTestRunner()
testRunner.run(tests)

Running the above code will print the test results to standard out.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kortina
  • 5,821
  • 4
  • 24
  • 28
  • 12
    This is the one I was looking for, and the most direct answer to the original question. It looks slightly different in 2.7 though; `test_loader = unittest.defaultTestLoader.discover( '.' )`; then `test_runner = unittest.TextTestRunner()`; then `test_runner.run( test_loader )` and you're good. – Paul Apr 17 '12 at 13:15
  • 4
    This is the recommended approach if you need to programmatically invoke a test runner, say from a custom script or `Command` instance. – Filip Dupanović Jun 21 '13 at 07:03
27

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

Note

Command line usage

In Python 2.7 you invoke the unittest command line features (including test discover) with python -m unittest <args>. 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.

Try 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 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 Jul 21 '10 at 17:10
  • @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 Jul 21 '10 at 18:37