Nosetest including unwanted parent directories

纵然是瞬间 提交于 2019-12-19 15:44:41

问题


I'm trying to limit nosetests to a specific directory, however during the test run it's including the parent directories of the dir I'm targetting and in doing so throws errors.

Here's the key elements of output from the test run:

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

I'm using buildout with pbp.recipe.noserunner. Here's the relevant /projects/myproject/buildout.cfg section:

[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

I've also tried setting where=myproject/specs as one of the defaults parameters to help limit the import but still no joy.

Any suggestions on where I'm going wrong?

Edit:

I've tried to --exclude the parent directories but no joy.


回答1:


I suppose that you are expecting the following behavior.

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

Why not try a --match or an --exclude pattern to restrict the tests set ?

Try:

--exclude myproject/myproject

I check the source code of nose.importer : nose recursivly add_path the parents packages of specs. I think that you cannot bypass this unless you create a specific importer... I do not not know if this is possible this the 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)


来源:https://stackoverflow.com/questions/6197940/nosetest-including-unwanted-parent-directories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!