如何在测试中访问插件选项? (Python鼻子) [英] How to access plugin options within a test? (Python Nose)

查看:73
本文介绍了如何在测试中访问插件选项? (Python鼻子)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用鼻子实现自动测试框架.目的是添加一些命令行选项以传递到测试中,例如主机名.我们将这些测试作为集成测试针对Web应用程序运行.

We are trying to implement an automated testing framework using nose. The intent is to add a few command line options to pass into the tests, for example a hostname. We run these tests against a web app as integration tests.

因此,我们创建了一个简单的插件,为解析器添加了一个选项:

So, we've created a simple plugin that adds a option to the parser:

import os
from nose.plugins import Plugin

class test_args(Plugin):
    """
    Attempting to add command line parameters.
    """
    name = 'test_args'
    enabled = True

    def options(self, parser, env=os.environ):
        super(test_args, self).options(parser, env)
        parser.add_option("--hostname",
                    action="store",
                    type="str",
                    help="The hostname of the server")

    def configure(self, options, conf):
        self.hostname = options.hostname    

当我们运行鼻子测试时该选项现在可用...但是我不知道如何在测试用例中使用它?这可能吗?我找不到任何有关如何在测试案例中访问选项或配置的文档.

The option is available now when we run nosetests...but I can't figure out how to use it within a test case? Is this possible? I can't find any documentation on how to access options or the configuration within a test case.

添加命令行参数纯粹是出于开发/调试代码的目的.我们计划将配置文件用于在Bamboo中进行自动运行.但是,在开发集成测试和调试问题时,最好随时更改配置.但是我们需要首先弄清楚如何实际使用这些选项...我觉得我只是缺少一些基本知识,或者我是盲人...

The adding of the command line arguments is purely for development/debugging code purposes. We plan to use config files for our automated runs in bamboo. However, when developing integration tests and also debugging issues, it is nice to change the config on the fly. But we need to figure out how to actually use the options first...I feel like I'm just missing something basic, or I'm blind...

理想情况下,我们可以扩展 testconfig插件,以从中传递配置参数:

Ideally we could extend the testconfig plugin to make passing in config arguments from this:

--tc=key:value

收件人:

--key=value

如果有更好的方法可以做到这一点,那么我就会不知所措.

If there is a better way to do this then I'm all ears.

推荐答案

所以我找到了如何使它工作的方法:

So I've found out how to make this work:

import os
from nose.plugins import Plugin

case_options = None

class test_args(Plugin):
    """
    Attempting to add command line parameters.
    """
    name = 'test_args'
    enabled = True

    def options(self, parser, env=os.environ):
        super(test_args, self).options(parser, env)
        parser.add_option("--hostname",
                    action="store",
                    type="str",
                    help="The hostname of the server")

    def configure(self, options, conf):
        global case_options 
        case_options = options

使用此功能,您可以在测试用例中执行以下操作以获取选项:

Using this you can do this in your test case to get the options:

    from test_args import case_options

要解决不同的配置文件问题,我发现您可以使用像INI文件一样编写的setup.cfg文件来传递默认命令行参数.您也可以传入-c config_file.cfg以选择其他配置.这应该可以很好地满足我们的需求.

To solve the different config file issues, I've found you can use a setup.cfg file written like an INI file to pass in default command line parameters. You can also pass in a -c config_file.cfg to pick a different config. This should work nicely for what we need.

这篇关于如何在测试中访问插件选项? (Python鼻子)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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