Issue 24247: Docs: "unittest discover" modifies sys.path (original) (raw)
"unittest discover" does add some directories to sys.path
later some modules can not work with modified sys.path
Here module pdb
is trying to import standard module cmd
but does import cmd
module from tests
directory:
"unittest discover" should not modify sys.path in any way.
$ mkdir /tmp/t $ cd /tmp/t $ mkdir tests $ mkdir tests/cmd $ touch tests/cmd/init.py $ echo "import pdb" > tests/test_nothing.py $ python -m unittest discover tests E
ERROR: test_nothing (unittest.loader.ModuleImportFailure)
ImportError: Failed to import test module: test_nothing Traceback (most recent call last): File "/usr/lib/python2.7/unittest/loader.py", line 254, in _find_tests module = self._get_module_from_name(name) File "/usr/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name import(name) File "/tmp/t/tests/test_nothing.py", line 1, in import pdb File "/usr/lib/python2.7/pdb.py", line 59, in class Pdb(bdb.Bdb, cmd.Cmd): AttributeError: 'module' object has no attribute 'Cmd'
Ran 1 test in 0.000s
FAILED (errors=1)
It did that because you did not specify a top level directory. Without that, the cwd is not on the path and that breaks many environments.
We should probably document it better. The workaround for your needs is to either just run 'unittest discover', or run 'unittest discover tests -t .', not 'unittest discover tests'.
The behaviour is however something I believe to be correct and essential to most user experiences.