Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'parameters': 0.04; 'elif': 0.05; "'',": 0.07; '__name__': 0.09; 'lawrence': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'unhandled': 0.09; 'url:github': 0.09; 'way:': 0.09; 'language.': 0.14; "'__main__':": 0.16; 'keywords)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sys.exit(1)': 0.16; '{0}': 0.16; 'language': 0.16; 'wrote:': 0.18; 'library': 0.18; 'module': 0.19; 'print': 0.22; 'header :User-Agent:1': 0.23; 'error': 0.23; 'skip:{ 20': 0.24; 'url:moin': 0.24; 'define': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'code': 0.31; "skip:' 10": 0.31; 'url:wiki': 0.31; 'file': 0.32; 'url:python': 0.33; 'framework': 0.33; 'third': 0.33; 'moment': 0.34; 'could': 0.34; 'test': 0.35; 'but': 0.35; 'acceptable': 0.36; 'false': 0.36; 'url:org': 0.36; 'unit': 0.37; 'wrong': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'url:3': 0.61; 'our': 0.64; 'charset:windows-1252': 0.65; 'here': 0.66; 'subject:this': 0.83; 'subject:good': 0.84; 'journey': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Is this a good way to implement testing Date: Sun, 03 May 2015 00:17:56 +0100 References: <878ud6mx4y.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-92-24-219-104.ppp.as43234.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <878ud6mx4y.fsf@Equus.decebal.nl> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430608701 news.xs4all.nl 2926 [2001:888:2000:d::a6]:33932 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89814 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