How to access plugin options within a test? (Python Nose)

后端 未结 2 691
太阳男子
太阳男子 2021-01-06 21:05

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

2条回答
  •  自闭症患者
    2021-01-06 21:40

    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
    

    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.

提交回复
热议问题