Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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; 'example:': 0.03; 'from:addr:yahoo.co.uk': 0.04; '"""': 0.07; 'python3': 0.07; 'cest': 0.09; 'doctest': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'language.': 0.14; 'question.': 0.14; '10:45': 0.16; '225': 0.16; 'correctness': 0.16; 'docstrings': 0.16; 'doctests': 0.16; 'itself,': 0.16; 'possible?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'module': 0.19; 'split': 0.19; '>>>': 0.22; 'putting': 0.22; 'separate': 0.22; 'tests': 0.22; 'header:User- Agent:1': 0.23; 'file.': 0.24; 'looks': 0.24; 'skip:" 30': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'bigger': 0.30; 'code': 0.31; 'lines': 0.31; 'usually': 0.31; 'own,': 0.31; 'file': 0.32; 'another': 0.32; 'text': 0.33; 'framework': 0.33; 'something': 0.35; 'done.': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'acceptable': 0.36; 'coverage': 0.36; 'module.': 0.36; 'should': 0.36; 'wrong': 0.37; 'performance': 0.37; 'to:addr:python-list': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'how': 0.40; 'profile': 0.61; 'show': 0.63; 'our': 0.64; 'charset:windows-1252': 0.65; 'started.': 0.68; 'sunday': 0.68; 'skip:* 70': 0.78; 'subject:this': 0.83; '2015': 0.84; 'failures.': 0.84; 'gut': 0.84; 'subject:good': 0.84 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 11:21:36 +0100 References: <878ud6mx4y.fsf@Equus.decebal.nl> <87383em7sn.fsf@Equus.decebal.nl> <87iocakn2f.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-78-147-17-139.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <87iocakn2f.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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430648512 news.xs4all.nl 2962 [2001:888:2000:d::a6]:39060 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89847 On 03/05/2015 10:49, Cecil Westerhof wrote: > Op Sunday 3 May 2015 10:45 CEST schreef Peter Otten: > >> Cecil Westerhof wrote: >> >>> 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. >> >> Definitely use an established testing framework instead of rolling >> your own, and definitely put it into a separate file -- by the time >> there is good coverage the test code is usually much bigger than the >> tested code. > > Yep, the module already has 370 lines of testing code and only 225 of > working code. And I just started. > > >> Be aware that there is also doctest which scans docstrings for text >> resembling interactive Python sessions. Doctests are both tests and >> usage examples, so I think it's good to put a few of these into the >> module. Here's how it works: >> >> $ cat factorial.py >> def factorial(n): >> """Calculate the factorial 1 * 2 * ... * n. >> >>>>> factorial(0) >> 1 >>>>> factorial(1) >> 1 >>>>> factorial(10) >> 3628800 """ return 1 $ python3 -m doctest factorial.py >> ********************************************************************** >> File "/home/peter/clpy/factorial.py", line 8, in factorial.factorial >> Failed example: factorial(10) Expected: 3628800 Got: 1 >> ********************************************************************** >> 1 items had failures: 1 of 3 in factorial.factorial ***Test >> Failed*** 1 failures. $ > > That looks very promising. But I use the test to verify the > correctness and show the performance. Is that also possible? Or should > I split those out. > Get it working correctly and if it's fast enough for your needs then job done. If and only if you actually have a performance issue profile your code to find the bottlenecks, as gut instinct about Python performance is wrong 99.99% of the time. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence