Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'result,': 0.07; 'subject:code': 0.07; 'suppose': 0.07; 'test,': 0.07; 'arguments,': 0.09; 'part,': 0.09; 'subject:using': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'cc:name:python list': 0.16; 'function?': 0.16; 'inconvenient': 0.16; 'supplying': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.18; 'result.': 0.19; 'input': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'options': 0.25; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'testing': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "d'aprano": 0.31; 'factor': 0.31; 'steven': 0.31; 'anyone': 0.31; 'stuff': 0.32; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'e.g.': 0.38; 'does': 0.39; 'how': 0.40; 'march': 0.61; 'skip:* 10': 0.61; 'simple': 0.61; 'times': 0.62; 'provide': 0.64; 'more': 0.64; 'hints': 0.68; 'oscar': 0.84; 'subject:Testing': 0.84; 'lot,': 0.93; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=OoKcYd3JsMMys+odv0+vbRhK4kIiITD6c55c4FTpoHU=; b=dPBA3f/BsA3HXABrcL1V890u/OS+zmSzaGicMrChxhcmvxlGxv2iVQainmaCIY9ZTc L36O6SOFupnoexUnB4oMkvPjGOR5MKTknlq6nA/ei9N+tLHzCCuXYCC8LG011i4e7dZB FKRDrtMzI3I0VrPkqLEGi4WJpeYKPZb2ysqZw+hL7+EZKqfsmO+0hzoXOgSlhgLz1vSN ricTq7QWAGRpi9+hf1R6pZdK7Rz0pX0nTc/Lyd+fh4P2Qgw6r4g9G25CRVWiOuKh2QT5 YW7/rrtn6RFTtB+z8SIpADuwlW4TS1778U/VH55s++lnlPYYWXXfd/7rVcOWR0hJqpgy FFHw== X-Received: by 10.58.178.238 with SMTP id db14mr2942645vec.25.1394467526022; Mon, 10 Mar 2014 09:05:26 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <531de147$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <531de147$0$29994$c3e8da3$5496439d@news.astraweb.com> From: Oscar Benjamin Date: Mon, 10 Mar 2014 16:05:02 +0000 Subject: Re: Testing interactive code using raw_input To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394467535 news.xs4all.nl 2925 [2001:888:2000:d::a6]:46480 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68153 On 10 March 2014 15:59, Steven D'Aprano wrote: > Does anyone have any good hints for testing interactive code that uses > raw_input, or input in Python 3? > > A simple technique would be to factor out the interactive part, e.g. like > this: > > # Before > def spam(): > answer = raw_input(prompt) > return eggs(answer) + cheese(answer) + toast(answer) > > # After > def spam(): > answer = raw_input(prompt) > return func(answer) > > def func(s): > return eggs(s) + cheese(s) + toast(s) > > > > and then test func. But how about times where it is inconvenient to > factor out the raw_input stuff out of the function? E.g. suppose you have > a function that takes some arguments, gathers some more values > interactively, processes the lot, and then returns a result. With an > automated test, I can provide the arguments, and check the result, but > what are my options for *automatically* supplying input to raw_input? Use a subprocess? Oscar