How to parse command line arguments from python nosetests plugin to testcase

201 views
Skip to first unread message

fzhurd

unread,
Apr 22, 2016, 12:45:13 AM4/22/16
to nose-users
Hi, all,


I am writing a simple nosetests plugin. The aim is to parse the arguments from command line, then this parsed value could be used in the testcase.

Some previous posts advised to use global variable, but I still could not figure it out. Anything wrong with my code? Thanks.


test_plugin file:


import nose
from nose.plugins import Plugin
PARAMETERS={}

class Parameters_Example(Plugin):
    global PARAMETERS
    name = 'parameters_example'
    score = 1
    enabled = True

def options(self, parser, env):

    super(Parameters_Example, self).options(parser, env)
    parser.add_option('--param',
                     dest='param',
                     action='append',
                     help='Input all required parameters')

def configure(self, options, config):

    super(Parameters_Example, self).configure(options, config)
    global PARAMETERS
    PARAMETERS=options.param

def begin(self):
    pass

if __name__ == '__main__':
        nose.main(addplugins=[Parameters_Example()]) 

tests.py 

from unittest import TestCase
  from test_plugin import PARAMETERS

  print PARAMETERS

  class Test_Example(TestCase):
       def setUp(self):
       pass
  def testb(self):            
       pass
 To run the tests with plugin:
./test_plugin.py -sv tests.py --with-parameters_example --param HOST:'localhost'

But, the tests.py still could not receive the value from the plugin, it prints out empty.

Thanks.

Reply all
Reply to author
Forward
0 new messages