Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3a.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; '"""': 0.07; 'python3': 0.07; 'doctest': 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; 'question.': 0.14; 'docstrings': 0.16; 'doctests': 0.16; 'itself,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'module': 0.19; '>>>': 0.22; 'putting': 0.22; 'separate': 0.22; 'tests': 0.22; 'header:User- Agent:1': 0.23; 'file.': 0.24; 'skip:" 30': 0.26; 'header:X -Complaints-To:1': 0.27; 'testing': 0.29; 'bigger': 0.30; 'code': 0.31; 'usually': 0.31; 'own,': 0.31; 'file': 0.32; 'another': 0.32; 'text': 0.33; 'framework': 0.33; 'something': 0.35; 'test': 0.35; 'there': 0.35; 'acceptable': 0.36; 'coverage': 0.36; 'module.': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:* 70': 0.78; 'subject:this': 0.83; 'failures.': 0.84; 'subject:good': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Is this a good way to implement testing Date: Sun, 03 May 2015 10:45:29 +0200 Organization: None References: <878ud6mx4y.fsf@Equus.decebal.nl> <87383em7sn.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8571.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430642740 news.xs4all.nl 2904 [2001:888:2000:d::a6]:57530 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89837 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. 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. $