pythonunit-testingargparsepython-unittestparameterized-unit-test

Python 2.6 Unittest assistance with parameters and argparse, how to solve?


I am trying to run a basic unit test on Python 2.6 that takes arguments with argparse.

I am limited in my environment and cannot install any further libraries or use any modules for testing but unittest.

However I believe the answer lies here:

How do you write tests for the argparse portion of a python module?

However I am having trouble refactoring the provided main answer with my code.

Without refactoring my example code I have provided, can someone please show me the light and show me how to write a unittest for the below code, that takes the -H and -S on the fly?

Thanks in advance.


#!python
import argparse
import sys

try:
    HOSTNAME = sys.argv[2]
    SOMESTRING = sys.argv[3]
except IndexError:
    print "Please Enter the Hostname and Somestring"


def argparse_msg():
    return "testscript_example -H somehost -S somestring"


def check_arg(args=None):
    parser = argparse.ArgumentParser(description="A Test Example", usage=argparse_msg())
    parser.add_argument("-H", "--host",
                        help="HostName",
                        required=True)

    parser.add_argument("-S", "--somestring",
                        help="HostName",
                        required=True)

    results = parser.parse_args(args)

    return (results.host, results.somestring)


def message_test():
    print HOSTNAME + " " + SOMESTRING


def main():
    message_test()


if __name__ == "__main__":
    HOSTNAME, SOMESTRING = check_arg(sys.argv[1:])
    main()

Solution

  • To facilitate test class, I have modified the code as following:

    Updated code receiver.py:

    #!python
    import argparse
    import sys
    
    
    def argparse_msg():
        return "testscript_example -H somehost -S somestring"
    
    
    def check_arg(args=None):
        parser = argparse.ArgumentParser(description="A Test Example", usage=argparse_msg())
        parser.add_argument("-H", "--host",
                            help="HostName",
                            required=True)
        parser.add_argument("-S", "--somestring",
                            help="HostName",
                            required=True)
        results = parser.parse_args(args)
        return (results.host, results.somestring)
    
    
    def message_test(HOSTNAME, SOMESTRING):
        return "{} {}".format(HOSTNAME, SOMESTRING)
    
    
    def main(HOSTNAME, SOMESTRING):
        return message_test(HOSTNAME, SOMESTRING)
    
    
    if __name__ == "__main__":
        HOSTNAME, SOMESTRING = check_arg(sys.argv[1:])
        print(main(HOSTNAME, SOMESTRING))
    

    Output of running receiver.py:

    python receiver.py -H localhost -S demo
    localhost demo
    

    Test file (test_receiver.py):

    from receiver import check_arg
    import unittest
    
    class ParserTest(unittest.TestCase):
    
        def test_main(self):
            HOSTNAME, SOMESTRING = check_arg(['-H', 'test', '-S', 'sample string'])
            self.assertEqual(HOSTNAME, 'test')
            self.assertEqual(SOMESTRING, 'sample string')        
    
    if __name__ == '__main__':
        unittest.main()
    

    Output of running the test_receiver.py:

    python test_receiver.py
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    
    OK