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