2010-09-08 48 views
4

我确实有几个小模块,里面有测试,py.testnose不寻找它们,因为它们的文件名中不包含test如何使py.test或nose在所有python文件中查找测试?

我该如何说服py.testnose以递归方式在所有python文件中查找测试 - '''包括文件名'''中没有test的测试?

在源文件中,我保留标准命名约定:class testSomeName与方法def test_some_name

如果这是不可能的,我可以使用什么其他解决方案来获得相同的结果。

我不想手动创建包含测试的所有文件的列表,我想要一个支持发现的解决方案。

回答

5

你也可以看看Nose将发现测试,而无需使用一个固定的文件名约定。

您可以使用以下代码绕过用于过滤鼻子中文件的正则表达式。 创建一个Python模块(即my_nosetests.py

import nose 
from nose.plugins.base import Plugin 

class ExtensionPlugin(Plugin): 

    name = "ExtensionPlugin" 

    def options(self, parser, env): 
     Plugin.options(self,parser,env) 

    def configure(self, options, config): 
     Plugin.configure(self, options, config) 
     self.enabled = True 

    def wantFile(self, file): 
     return file.endswith('.py') 

    def wantDirectory(self,directory): 
     return True 

    def wantModule(self,file): 
     return True 


if __name__ == '__main__': 
    includeDirs = ["-w", ".", ".."] 
    nose.main(addplugins=[ExtensionPlugin()], argv=sys.argv.extend(includeDirs)) 

现在运行my_nosetests.py,如果你正在运行nosetests,你应该有你的测试运行。请注意,您实际上正在加载所有模块并在其中搜索测试。注意模块加载的任何副作用。

+0

鼻子是件好事! – dmitko 2010-09-08 18:26:40

+0

你可以给一个鼻子的解决方案,我会接受它! – sorin 2010-09-09 13:22:50

+0

使用鼻子在根目录中使用nosetests或将目录作为参数传递。默认情况下它会递归。 – Rod 2010-09-09 13:49:58

1

documentation

默认情况下不以点开头的所有目录遍历,寻找测试_ *。PY和* _test.py文件。这些Python文件在其包名下导入。

你能确定这是你的代码的情况吗?

更新

(买者自负:我还没有试过/测试这一点)如何使用用于收集的目录和文件的hooks

py.test要求收集的文件和目录下面的两个基本挂钩:

def pytest_collect_directory(path, parent): 
    """ return Collection node or None for the given path. """ 

def pytest_collect_file(path, parent): 
    """ return Collection node or None for the given path. """ 

无论是对于一个给定的路径返回collection node。所有钩子实现的所有返回节点都将参与收集和运行协议。所述parent对象是父节点,并且可以被用于通过parent.config对象来访问命令行选项。

+1

我做了RTFM和准确,这是问题。我想改变这种行为,我有单位测试,但它们都嵌入在模块文件中,并从__main__中调用。我相信在将来我会想要移动它们,但目前我只是想扫描它们,即使在不使用这种命名的文件中。 – sorin 2010-09-08 17:57:04

+1

@Sorin:更新了我的答案。往上看。 – 2010-09-08 18:17:19

6

随着py.test这很简单。与此内容创建一个conftest.py文件:

# content of conftest.py file at root of your project 
def pytest_collect_file(path, parent): 
    if path.ext == ".py": 
     return parent.Module(path, parent) 

这将延长征收过程为每个“py”为文件的测试“模块”节点。把它放到conftest.py文件使它在具体项目的扩展,如果你键入它自动加载:

py.test 

作参考之用,也可键入:

py.test --collectonly 

,看看有什么测试和文件收集,例如输出:

<Directory 'morecollect'> 
    <Module 'conftest.py'> 
    <Directory 'pkg'> 
    <Module 'test_x.py'> 
     <Function 'test_hello2'> 
    <Module 'x.py'> # this is collected because of our conftest.py extension 
    <Function 'test_hello'> 

如果需要的话,你也可以包上面conftest.py文件作为installable plugin,使通过安装插件可用的扩展名。在这种情况下,根本不需要任何conftest.py文件。

+0

目前我无法测试这个方法是否工作,因为py.test崩溃了,看到这个bug:http://bitbucket.org/hpk42/py-trunk/issue/119/fail- to-impot -__ init__py-internalerror – sorin 2010-09-14 11:53:29

+0

嘿索林。好的,Ronny和我刚刚发布了一个新版本,附有一些修复程序,包括您看到的问题。做“easy_install -U py”应该带给你。感谢报道,玩得开心。霍尔格 – hpk42 2010-09-14 15:41:39

5

将一个文件“setup.cfg”在项目的根目录,它包含这两条线:

[pytest] 
python_files=*.py 

然后py.test选择所有*.py文件测试。在这里它的解释:pytest docs

与鼻:

nosetests --all-modules 
相关问题