Author gagern
Recipients docs@python, gagern
Date 2011-02-15.08:33:26
SpamBayes Score 7.04992e-14
Marked as misclassified No
Message-id <>
If I follow the documentation at by putting the following two snippets of code in my module file:

def load_tests(loader, standard_tests, pattern='test*.py'):
    # top level directory cached on loader instance
    this_dir = os.path.dirname(__file__)
    package_tests =, pattern=pattern)
    return standard_tests

if __name__ == "__main__":

then the application will fail with an obscure error message:

ERROR: __main__ (unittest.loader.LoadTestsFailure)
TypeError: object of type 'NoneType' has no len()

Ran 1 test in 0.000s

Monkeypatching unittest.loader._make_failed_load_tests to display a stack trace, I got this:

Traceback (most recent call last):
  File "/usr/lib64/python2.7/unittest/", line 71, in loadTestsFromModule
    return load_tests(self, tests, None)
  File "", line 15, in load_tests
    package_tests =, pattern=pattern)
  File "/usr/lib64/python2.7/unittest/", line 204, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/usr/lib64/python2.7/unittest/", line 247, in _find_tests
    if not self._match_path(path, full_path, pattern):
  File "/usr/lib64/python2.7/unittest/", line 235, in _match_path
    return fnmatch(path, pattern)
  File "/usr/lib64/python2.7/", line 43, in fnmatch
    return fnmatchcase(name, pat)
  File "/usr/lib64/python2.7/", line 75, in fnmatchcase
    res = translate(pat)
  File "/usr/lib64/python2.7/", line 87, in translate
    i, n = 0, len(pat)
TypeError: object of type 'NoneType' has no len()

The error is due to the fact that pattern is passed as None to load_tests, but apparently doesn't loke a None pattern.

I would suggest that
a) discover internally translates None to the default of 'test*.py' or
b) the documentation is changed to cater for this common use case, i.e. by including a "pattern is None" case distinction in its load_tests snippet.

In case b) is implemented but not a), it would be nice to have a more expressive error message by catching the error somewhat sooner.
Date User Action Args
2011-02-15 08:33:27gagernsetrecipients: + gagern, docs@python
2011-02-15 08:33:27gagernsetmessageid: <>
2011-02-15 08:33:26gagernlinkissue11218 messages
2011-02-15 08:33:26gagerncreate