为什么鼻子测试无法在sys.path中找到一个元素? [英] Why can't nosetests find one the elements in sys.path?

查看:105
本文介绍了为什么鼻子测试无法在sys.path中找到一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列使用 nose 进行的单元测试.对于某些测试,我想从sys.path中删除模块的路径,因此与我正在测试的内容没有冲突.

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

如果我运行Python解释器并调用sys.path,则'/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.

为什么鼻子无法在sys.path中找到路径?

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

推荐答案

创建脚本get_mod_py_path.py,以设置PYTHONPATH.在这种情况下,它将删除冲突的路径.

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

然后在调用鼻子测试的bash中使用它.

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" 

这篇关于为什么鼻子测试无法在sys.path中找到一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆