如何让py.test或nose在所有python文件中查找测试?

时间:2010-09-08 17:46:00

标签: python nose pytest

我确实有几个小模块,其中包含测试,py.testnose不会查找它们,因为它们的文件名中不包含test

如何说服py.testnose在所有python文件中寻找测试,递归 - '''包括那些在文件名中没有test的文件''' ?

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

如果无法做到这一点,我可以使用哪种其他解决方案来获得相同的结果。

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

4 个答案:

答案 0 :(得分: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文件。

答案 1 :(得分:5)

您还可以查看Nose,它将发现测试而无需使用固定的文件名约定。

您可以使用以下代码绕过用于过滤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一样,您应该运行测试。请注意,您实际上正在加载所有模块并在其中搜索测试。注意模块加载的任何副作用。

答案 2 :(得分:5)

在项目的根目录下放置一个“setup.cfg”文件,它包含以下两行:

[pytest]
python_files=*.py

然后py.test从所有*.py文件中选择测试。这里解释了:pytest docs

与鼻子:

nosetests --all-modules

答案 3 :(得分:1)

documentation

  

默认情况下,遍历所有不以点开头的目录,查找test _ *。py和* _test.py文件。这些Python文件是在它们的包名下导入的。

您能确保代码的情况如此吗?

<强>更新

(Caveat Emptor:我没有尝试/测试过这个)如何使用提供的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对象访问命令行选项。

相关问题