Nosetest包含不需要的父目录
我想把nosetests限制在一个特定的文件夹里,但在测试运行的时候,它却把我目标文件夹的父文件夹也包含进来了,这样就出现了错误。
这是测试运行输出的关键内容:
nose.importer: DEBUG: Add path /projects/myproject/myproject/specs
nose.importer: DEBUG: Add path /projects/myproject/myproject
nose.importer: DEBUG: Add path /projects/myproject
nose.importer: DEBUG: insert /projects/myproject into sys.path
我正在使用buildout
和pbp.recipe.noserunner
。以下是我/projects/myproject/buildout.cfg
文件中相关的部分:
[specs]
recipe = pbp.recipe.noserunner
eggs =
pbp.recipe.noserunner
${buildout:eggs}
figleaf
pinocchio
working-directory =
myproject/specs
defaults =
-vvv
--exe
--include ^(it|ensure|must|should|specs?|examples?)
--include (specs?(.py)?|examples?(.py)?)$
--with-spec
--spec-color
我还尝试把where=myproject/specs
设置为defaults
参数之一,想要限制导入,但还是没有成功。
有没有人能给我一些建议,看看我哪里出错了?
补充:
我尝试过用--exclude
来排除父文件夹,但还是没有效果。
1 个回答
5
我想你可能期待以下的行为。
nose.importer: DEBUG: Add path /projects/myproject
nose.importer: DEBUG: insert /projects/myproject into sys.path
为什么不试试用 --match
或者 --exclude
这样的模式来限制测试集呢?
试试这个:
--exclude myproject/myproject
我查看了 nose.importer 的源代码:nose 会递归地添加父级包到规格中。我觉得你无法绕过这个,除非你创建一个特定的导入器……我不知道这在 nose 的 API 中是否可行。
def add_path(path, config=None):
"""Ensure that the path, or the root of the current package (if
path is in a package), is in sys.path.
"""
# FIXME add any src-looking dirs seen too... need to get config for that
log.debug('Add path %s' % path)
if not path:
return []
added = []
parent = os.path.dirname(path)
if (parent
and os.path.exists(os.path.join(path, '__init__.py'))):
added.extend(add_path(parent, config))
elif not path in sys.path:
log.debug("insert %s into sys.path", path)
sys.path.insert(0, path)
added.append(path)
if config and config.srcDirs:
for dirname in config.srcDirs:
dirpath = os.path.join(path, dirname)
if os.path.isdir(dirpath):
sys.path.insert(0, dirpath)
added.append(dirpath)
return added
def remove_path(path):
log.debug('Remove path %s' % path)
if path in sys.path:
sys.path.remove(path)