如何使py.test或鼻子在所有python文件中查找测试? [英] How to make py.test or nose to look for tests inside all python files?

查看:106
本文介绍了如何使py.test或鼻子在所有python文件中查找测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I do have several small modules where the tests are inside them and py.test or nose does not look for them because they do not contain test in their filename.

我怎样才能说服py.testnose在所有python文件中查找测试,然后递归-'''包括文件名中没有test的文件''

How can I convince py.test or nose to look for tests inside all python files, recursively - '''including the ones that do not have test in their filenames'''?

在源文件中,我确实遵守标准命名约定:class testSomeName和方法def test_some_name.

Inside the source files I do keep the standard naming convention: class testSomeName with methods def test_some_name.

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

If this is not possible, what other solution can I use to obtain the same result.

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

I do not want to manually create a list of all files containing the test, I want a solution that supports discovery.

推荐答案

您还可以查看

You can also have a look at Nose which will discover tests without having to use a fixed file name convention.

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

You can bypass the regexp used to filter files in nose with the following code. Create a python module (i.e. 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))

现在像运行nosetests一样运行my_nosetests.py,应该运行测试.请注意,您实际上是在加载所有模块并在其中搜索测试.提防模块加载的任何副作用.

Now run my_nosetests.py as if you were running nosetests and you should have your tests running. Be aware that you are in fact loading all modules and searching for tests in them. Beware of any side effect of module loading.

这篇关于如何使py.test或鼻子在所有python文件中查找测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆