Python unittest and discovery

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):
    

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.