python unittest for argparse

Ich habe eine Funktion in einem Modul, die ein @ erstelargparse:

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()

Eine Probemyapp.py wäre

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

und dies wird aufgerufen als:

$> python myapp.py -c config.yaml
$> config.yaml
   127.0.0.1
gt; python myapp.py -c config.yaml
$> python myapp.py -c config.yaml
$> config.yaml
   127.0.0.1
gt; config.yaml 127.0.0.1

Nun versuche ich, einen Komponententest für diese Funktion zu erstellen, aber mein Problem ist, dass ich keine Befehlszeilenparameter über den Testcode übergeben kann.

# 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

Mein Problem ist, dass ich die Befehlszeilenargumente an @ übergeben muget_options() aber ich weiß nicht, wie ich es richtig machen soll.

Erwarteter richtiger Aufruf:python mytest.py (-c config.yaml sollte irgendwie im Testcode übergeben werden.)

Was "funktioniert" / funktioniert gerade nicht:

python mytest.py -c config.yaml funktioniert auch nicht. Kehrt zurückAttributeError: 'module' object has no attribute 'config' da es erwartet, dass ich @ anruargParseTestCase stattdessen. Mit anderen Worten,python mytest.py -c argParseTestCase "funktioniert" aber wäre natürlich eine RückkehrAssertionError: 'config.yaml' != 'argParseTestCase'

python mytest.py -v, um den Komponententest im ausführlichen Modus auszuführen, schlägt ebenfalls fehl. Es gibt zurück:

test_parser Mai .argParseTestCase) ... mytest.py 1.0 FEHLER FEHLER: test_parser Mai .argParseTestCase)
Traceback (letzter Aufruf zuletzt): Datei "tests / unit_tests / mytest.py", Zeile 376, in test_parser options = get_options () Datei "/root/test/lib/python2.7/site-packages/mymodule.py" ", Zeile 61, in get_options return parser.parse_args ()
Datei "/usr/local/lib/python2.7/argparse.py", Zeile 1701, in parse_args args, argv = self.parse_known_args (args, Namespace)
Datei "/usr/local/lib/python2.7/argparse.py", Zeile 1733, in Namespace parse_known_args, args = self._parse_known_args (args, Namespace)
Datei "/usr/local/lib/python2.7/argparse.py", Zeile 1939, in _parse_known_args start_index = consume_optional (start_index)
File "/usr/local/lib/python2.7/argparse.py", Zeile 1879, in consume_optional take_action (action, args, option_string)
Datei "/usr/local/lib/python2.7/argparse.py", Zeile 1807, in Aktion "take_action" (self, Namespace, Argumentwerte, Optionszeichenfolge)
Datei "/usr/local/lib/python2.7/argparse.py", Zeile 1022, inAnru parser.exit (message = formatter.format_help ())
File "/usr/local/lib/python2.7/argparse.py", Zeile 2362, in exit _sys.exit (status) SystemExit: 0

Antworten auf die Frage(4)

Ihre Antwort auf die Frage