On 02/05/2015 23:29, Cecil Westerhof wrote:
Still on my journey to learn Python.
At the moment I define the test functionality in the following way:
if __name__ == '__main__':
keywords = [
'all',
'factorial',
'fibonacci',
'happy',
'lucky',
]
keywords_msg = [
'--all',
'--factorial',
'--fibonacci',
'--happy',
'--lucky',
]
(options,
extraParams) = getopt.getopt(sys.argv[1:], '', keywords)
progname = split(sys.argv[0])[1]
if len(options) > 1 or len(extraParams) != 0:
error = '{0}: Wrong parameters ({1})'. \
format(progname, ' '.join(sys.argv[1:]))
usage = ' {0} {1}'.format(progname, ' | '.join(keywords_msg))
print(error, file = sys.stderr)
print(usage, file = sys.stderr)
sys.exit(1)
do_all = do_factorial = do_fibonacci = do_happy = do_lucky = False
if len(options) == 0:
do_all = True
else:
action = options[0][0]
if action == '--all':
do_all = True
elif action == '--factorial':
do_factorial = True
elif action == '--fibonacci':
do_fibonacci = True
elif action == '--happy':
do_happy = True
elif action == '--lucky':
do_lucky = True
else:
print >> sys.stderr, progname + ': Unhandled parameter ' +
action
sys.exit(1)
if do_all or do_factorial:
.
.
.
Is this an acceptable way of working?
For code like the above I prefer the third party docopt module
https://github.com/docopt/docopt although you could also try
https://docs.python.org/3/library/argparse.html#module-argparse
The standard library unit testing framework is here
https://docs.python.org/3/library/unittest.html#module-unittest but also
see https://wiki.python.org/moin/PythonTestingToolsTaxonomy
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
--
https://mail.python.org/mailman/listinfo/python-list