Why can't nosetests find one the elements in s

2019-07-28 05:10发布

问题:

I have a series of unit tests that I'm running with nose. For some of my tests, I'd like to remove a module's path from the sys.path so there is no conflict with what I am testing.

sys.path.remove('/path/to/remove/from/sys/path')

If I run the Python interpreter and call sys.path, the '/path/to/remove/from/sys/path' is there in the list. However, once nosetests is called, the above code cannot find it and gives me a "not found in list" error.

Why is nose not able to find the path in sys.path?

回答1:

Didn't you mean this?

sys.path.remove('/path/to/remove/from/sys/path')

If nose can't find it in sys.path then it wasn't there... nose does lots of diddling with sys.path on its own. Why not print sys.path and see what it actually is when run under nose



回答2:

Create a script, get_mod_py_path.py, to set the PYTHONPATH. In this case, it is dropping the conflicted path.

import os
import sys

# Remove the global Python modules from the PYTHONPATH.
path = os.environ['PYTHONPATH'].split(os.pathsep)
if os.environ['GLOB_PY_MODULES'] in path: 
    path.remove(os.environ['GLOB_PY_MODULES'])

# Construct the new path and print it. 
path = ':'.join(path)
print path

Then use it in a bash that calls nosetests.

PYTHONPATH=`python get_mod_py_path.py`   
nosetests --verbosity=1 --with-gae --where="../tests/unit" --gae-application="../app"