Skip to main content
 首页 » 编程设计

python之nosetests 在 "_test"文件夹中发现代码

2025年05月04日14虾米哥

我正在使用 nosetests 来发现和运行我所有的测试代码。我想将所有测试代码放在名为“_test”的文件夹中,但 nosetests 不会使用默认设置在该文件夹中发现我的测试代码。

我发现您可以告诉 nosetests 使用正则表达式寻找替代模式来发现您的测试。出于某种原因,我无法使用以下划线开头的文件夹名称进行 nosetests 工作。有什么想法吗?

请您参考如下方法:

你不能。

nose 的默认行为是忽略 setup.py 和所有以下划线或句点开头的文件或文件夹。增加 nose 输出的冗长程度明确表明了这一点:

$ nosetests --verbosity=3 
nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$'] 

虽然可以使用 --ignore 选项覆盖文件的默认行为 - 例如,通过将其设置为不会发生的模式:

$ nosetests --verbosity=3 --ignore='^$' 

...这不适用于目录,因为a hardcoded exclusion of initial underscores :

            if is_dir: 
                # this hard-coded initial-underscore test will be removed: 
                # http://code.google.com/p/python-nose/issues/detail?id=82 
                if entry.startswith('_'): 
                    continue 

正如您从评论中看到的那样,nose 开发人员打算在某个时候取消此限制,但由于评论中链接的问题在 2008 年被关闭为“已修复”,该计划似乎已搁置。