lundi 23 mars 2015

unit test for option parser

I want to write a unit test code for OptionParser. Below is my code



import sys
import os
from optparse import OptionParser
import socket
import shutil
import subprocess
from subprocess import PIPE
import glob
from startup import MYHOME
sys.path.insert(0, MYHOME)
HOSTNAME = socket.gethostname()
def main():
usage = "createconfigfiles.py -h for help on usage"
parser = OptionParser(usage=usage)
parser.add_option("-a", "--all",
action="store_true", dest="all", default=False,
help="copy all config files for %s" % HOSTNAME)
parser.add_option("-f", "--first",
action="store_true", dest="first", default=False,
help="create and copy all config files when installing
%s" % HOSTNAME)
(opts, args) = parser.parse_args()
def getActualhost():
hostname = HOSTNAME
if opts.target:
target_list = getTargetlist()
target = opts.target
if target in target_list:
hostname = target
else:
print '%s is not a known target' % target
print 'please use any of:', target_list
return
return hostname


How can write a unit test for the optionparser. I want to make sure that my main method(which contains command line arguments)working properly. Thanks


Aucun commentaire:

Enregistrer un commentaire