Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.tele2net.at!news.panservice.it!feed.xsnews.nl!border02.ams.xsnews.nl!feeder04.ams.xsnews.nl!abp002.ams.xsnews.nl!frontend-F09-14.ams.news.kpn.nl From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Is this a good way to implement testing Organization: Decebal Computing References: <878ud6mx4y.fsf@Equus.decebal.nl> X-Face: "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR,v+Pti8=Vi/Z"g^?b"E X-Homepage: http://www.decebal.nl/ Date: Sun, 03 May 2015 09:36:40 +0200 Message-ID: <87383em7sn.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:wFOi5l8skZBB+I272Vw1URUfwTg= MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Lines: 69 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1430639089 news.kpn.nl 25004 81.207.62.244@kpn/81.207.62.244:51802 Xref: csiph.com comp.lang.python:89833 Op Sunday 3 May 2015 00:29 CEST schreef Cecil Westerhof: > 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? Thanks for the tips. For most I have to read a ‘little’ first, so I will not implement them immediately. Another question. Is it acceptable to have it in the module itself, or should I put it in something like test_.py? The code for testing is bigger as the code for the implementation, so I am leaning to putting it in a separate file. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof