Nosetest包括不需要的父目录

时间:2011-06-01 07:54:03

标签: python nose

我正在尝试将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 个答案:

答案 0 :(得分:5)

我想你期待以下行为。

nose.importer: DEBUG: Add path /projects/myproject
nose.importer: DEBUG: insert /projects/myproject into sys.path

为什么不尝试使用--match--exclude模式来限制测试集?

尝试:

--exclude myproject/myproject

我检查了nose.importer的源代码:鼻子recursivly add_path父母的specs包。 除非你创建一个特定的导入器,否则我认为你无法绕过这个... 我不知道这是不可能的鼻子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)