在nose或pytes中收集以编程方式生成的测试套件的好方法

2024-10-01 09:16:22 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有这样一个测试套件:

class SafeTests(unittest.TestCase):
    # snip 20 test functions

class BombTests(unittest.TestCase):
    # snip 10 different test cases

我目前正在做以下工作:

^{pr2}$

我有个大问题,还有一点很有趣

  • 我想用鼻子或py.测试(哪一个都不重要)
  • 我有大量不同的应用程序公开这些测试 套房通过入口。在

    我想能够聚合这些自定义测试跨所有安装 所以我不能使用一个聪明的命名约定。我不 尤其是关心这些通过入口点暴露出来,但是我 是否关心是否能够在中跨应用程序运行测试 站点包。(不只是导入。。。每个模块。)

我不在乎维持当前对 unittest.TestCase,破坏依赖关系实际上是一个目标。在


编辑这是为了确认@Oleksiy关于将参数传递给 nose.run实际上有一些注意事项。在

不起作用的事情:

  • 传递所有要执行的文件(这很奇怪)
  • 传递所有想要执行的模块。(这要么执行 没什么,做错了,或者太多了。有趣的0、1或 很多,也许吧?)在
  • 在之前传入模块:目录必须来 首先,否则你会得到重复的测试。在

这种脆弱是荒谬的,如果你有改善它的想法,我欢迎 评论,或者我设置 a github repo with my experiments trying to get this to work。在

除此之外,下面的工作,包括多个项目 安装到站点包中:

#!python
import importlib, os, sys
import nose

def runtests():
    modnames = []
    dirs = set()
    for modname in sys.argv[1:]:
        modnames.append(modname)

        mod = importlib.import_module(modname)
        fname = mod.__file__
        dirs.add(os.path.dirname(fname))

    modnames = list(dirs) + modnames

    nose.run(argv=modnames)

if __name__ == '__main__':
    runtests()

{right当保存到cd3}文件中时:

runtests.py project.tests otherproject.tests

Tags: 模块pytestimport站点unittesttestcaseruntests
3条回答

对于nose,您可以同时使用attribute插件选择要运行的测试,这对于选择要运行的测试非常有用。我将保留这两个测试并为它们分配属性:

from nose.plugins.attrib import attr

@attr("safe")
class SafeTests(unittest.TestCase):
    # snip 20 test functions

class BombTests(unittest.TestCase):
    # snip 10 different test cases

对于您的生产代码,我只需要使用nosetests -a safe调用nose,或者在您的os生产测试环境中设置NOSE_ATTR=safe,或者调用nose对象上的run方法,根据您的-a命令行选项在python中本机运行它:

^{pr2}$

最后,如果由于某种原因您的测试没有被发现,您可以使用@istest属性(from nose.tools import istest)显式地将它们标记为test

如果你的问题是,“如何让pytest‘看到’一个测试?”,则需要在每个测试文件和每个测试用例(即函数)前添加“test”。然后,只需在pytest命令行上传递要搜索的目录,它将递归地搜索匹配“test”的文件_三十、 是的,从中收集“test_XXX”函数并运行它们。在

至于文档,您可以尝试启动here.

如果您不喜欢默认的pytest测试集合方法,可以使用方向here.对其进行自定义

结果弄得一团糟:鼻子几乎只使用 TestLoader.load_tests_from_names函数(它是唯一的函数^{}) 所以既然我想从一个任意的python对象装载东西,我 似乎需要自己写一个算出来要用什么样的加载函数。在

此外,还要正确地使nosetests脚本正常工作 我需要进口大量的东西。我一点也不确定 是做事最好的方式,甚至不是那种。但这是一个脱光 对我有用的示例(无错误检查,更详细):

import sys
import types
import unittest

from nose.config import Config, all_config_files
from nose.core import run
from nose.loader import TestLoader
from nose.suite import ContextSuite
from nose.plugins.manager import PluginManager

from myapp import find_test_objects

def load_tests(config, obj):
    """Load tests from an object

    Requires an already configured nose.config.Config object.

    Returns a nose.suite.ContextSuite so that nose can actually give
    formatted output.
    """

    loader = TestLoader()
    kinds = [
        (unittest.TestCase, loader.loadTestsFromTestCase),
        (types.ModuleType, loader.loadTestsFromModule),
        (object, loader.loadTestsFromTestClass),
    ]
    tests = None
    for kind, load in kinds.items():
        if isinstance(obj, kind) or issubclass(obj, kind):
            log.debug("found tests for %s as %s", obj, kind)
            tests = load(obj)
            break

    suite = ContextSuite(tests=tests, context=obj, config=config)

def main():
    "Actually configure the nose config object and run the tests"
    config = Config(files=all_config_files(), plugins=PluginManager())
    config.configure(argv=sys.argv)

    tests = []
    for group in find_test_objects():
        tests.append(load_tests(config, group))

    run(suite=tests)

相关问题 更多 >