[issue17457] Unittest discover fails with namespace packages and builtin modules

Popa Claudiu report at bugs.python.org
Mon Mar 18 13:15:30 CET 2013


New submission from Popa Claudiu:

There is a problem with unittest discovering and namespace packages. Given the following folder structure, where a namespace package X lies, the following command fails with the following error:

-testbug
   - flufl (namespace package with some tests in it, importable with __import__)
      - test_a.py
      - test_b.py


C:\>py -3 -m unittest discover flufl
Traceback (most recent call last):
  File "C:\Python33\lib\runpy.py", line 160, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python33\lib\runpy.py", line 73, in _run_code
    exec(code, run_globals)
  File "C:\Python33\lib\unittest\__main__.py", line 12, in <module>
    main(module=None)
  File "C:\Python33\lib\unittest\main.py", line 124, in __init__
    self.parseArgs(argv)
  File "C:\Python33\lib\unittest\main.py", line 144, in parseArgs
    self._do_discovery(argv[2:])
  File "C:\Python33\lib\unittest\main.py", line 242, in _do_discovery
    self.test = loader.discover(start_dir, pattern, top_level_dir)
  File "C:\Python33\lib\unittest\loader.py", line 205, in discover
    start_dir = os.path.abspath(os.path.dirname((the_module.__file__)))
AttributeError: 'module' object has no attribute '__file__'

This happens because TestLoader.discover assumes that the given dotted package name has the attribute __file__, which seems to not be true in the case of namespace packages. The same error occurs when giving to `discover` a builtin module.
The attached patch tries naively to solve this issue, but it assume in TestLoader._find_tests that it should iterate over all subfolders (the commented line from the patch), unlike the previous way of checking the presence of __init__.py file.
Thanks in advance for your response.

----------
components: Library (Lib)
files: unittest.patch
keywords: patch
messages: 184450
nosy: Popa.Claudiu
priority: normal
severity: normal
status: open
title: Unittest discover fails with namespace packages and builtin modules
type: behavior
versions: Python 3.3, Python 3.4
Added file: http://bugs.python.org/file29441/unittest.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17457>
_______________________________________


More information about the Python-bugs-list mailing list