【问题标题】:Nosetest including unwanted parent directoriesNosetest 包括不需要的父目录
【发布时间】:2011-06-08 20:03:19
【问题描述】:

我正在尝试将鼻子测试限制到特定目录,但是在测试运行期间它包括我要定位的目录的父目录,这样做会引发错误。

以下是测试运行输出的关键元素:

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

我正在使用 buildoutpbp.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 父目录但没有乐趣。

【问题讨论】:

    标签: python nose


    【解决方案1】:

    我想你期待以下行为。

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

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

    试试:

    --exclude myproject/myproject
    

    我检查了nose.importer 的源代码:nose recursivly add_path 父包规范。 我认为除非您创建特定的进口商,否则您无法绕过这一点... 我不知道鼻子 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)
    

    【讨论】:

    • 谢谢;我应该意识到最好的调查方法是阅读源代码!
    • 那么,请确保您没有不需要的__init__.py(或__init__.pyc),对吧?
    猜你喜欢
    • 2014-08-07
    • 2015-10-18
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    相关资源
    最近更新 更多