title: python unittest for argparse
body: <p>I have a function inside a module that creates an <code>argparse</code>:</p>
  
  <pre><code>def get_options(prog_version='1.0', prog_usage='', misc_opts=None):
       options = [] if misc_opts is None else misc_opts
       parser = ArgumentParser(usage=prog_usage) if prog_usage else ArgumentParser()
       parser.add_argument('-v', '--version', action='version', version='%(prog)s {}'.format(prog_version))
       parser.add_argument('-c', '--config', dest='config', required=True, help='the path to the configuration file')
  
      for option in options:
          if 'option' in option and 'destination' in option:
              parser.add_argument(option['option'],
                                  dest=option.get('destination', ''),
                                  default=option.get('default', ''),
                                  help=option.get('description', ''),
                                  action=option.get('action', 'store'))
  
      return parser.parse_args()
  </code></pre>
  
  <p>A sample <code>myapp.py</code> would be:</p>
  
  <pre><code>my_options = [
      {
          "option": "-s",
          "destination": "remote_host",
          "default": "127.0.0.1",
          "description": "The remote server name or IP address",
          "action": "store"
      },
  ]
  
  # Get Command Line Options
  options = get_options(misc_opts=my_options)
  print options.config
  print options.remote_host
  </code></pre>
  
  <p>and this will be called as: </p>
  
  <pre><code>$&gt; python myapp.py -c config.yaml
  $&gt; config.yaml
     127.0.0.1
  </code></pre>
  
  <p>Now, I am trying to create a unit test for this function but my problem is that I can't pass command line parameters via test code.</p>
  
  <pre><code># mytest.py
  import unittest
  from mymodule import get_options
  
  class argParseTestCase(unittest.TestCase):
       def test_parser(self):
           options = get_options()
           # ...pass the command line arguments...
           self.assertEquals('config.yaml', options.config) # ofcourse this fails because I don't know how I will pass the command line arguments
  </code></pre>
  
  <p>My problem is that I need to pass the command line arguments to <code>get_options()</code> but I don't know how to do it properly.</p>
  
  <p>Expected proper call: <code>python mytest.py</code> (<code>-c config.yaml</code> should be passed inside the test code somehow.)</p>
  
  <p>What is "working"/not working right now:</p>
  
  <ol>
  <li><code>python mytest.py -c config.yaml</code> is also not working. Returns <code>AttributeError: 'module' object has no attribute 'config'</code> since it expects me to call <code>argParseTestCase</code> instead. In other words, <code>python mytest.py -c argParseTestCase</code> "works" but would ofcourse be an return <code>AssertionError: 'config.yaml' != 'argParseTestCase'</code></li>
  <li><p><code>python mytest.py -v</code> to run the unit test in verbose mode also fails. It returns: </p>
  
  <blockquote>
    <p>test_parser (<strong>main</strong>.argParseTestCase) ... mytest.py 1.0 ERROR 
    ERROR: test_parser (<strong>main</strong>.argParseTestCase)<br>
    Traceback (most recent call last): 
    File "tests/unit_tests/mytest.py", line 376, in test_parser options = get_options() 
    File "/root/test/lib/python2.7/site-packages/mymodule.py", line 61, in get_options return parser.parse_args()<br>
    File "/usr/local/lib/python2.7/argparse.py", line 1701, in parse_args args, argv = self.parse_known_args(args, namespace)<br>
    File "/usr/local/lib/python2.7/argparse.py", line 1733, in parse_known_args namespace, args = self._parse_known_args(args, namespace)<br>
    File "/usr/local/lib/python2.7/argparse.py", line 1939, in _parse_known_args start_index = consume_optional(start_index)<br>
    File "/usr/local/lib/python2.7/argparse.py", line 1879, in consume_optional take_action(action, args, option_string)<br>
    File "/usr/local/lib/python2.7/argparse.py", line 1807, in take_action action(self, namespace, argument_values, option_string)<br>
    File "/usr/local/lib/python2.7/argparse.py", line 1022, in <strong>call</strong> parser.exit(message=formatter.format_help())<br>
    File "/usr/local/lib/python2.7/argparse.py", line 2362, in exit _sys.exit(status) 
    SystemExit: 0</p>
  </blockquote></li>
  </ol>