Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'result,': 0.07; 'subject:code': 0.07; 'suppose': 0.07; 'test,': 0.07; 'arguments,': 0.09; 'part,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'function?': 0.16; 'inconvenient': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'stringio': 0.16; 'supplying': 0.16; 'result.': 0.19; 'input': 0.22; 'options': 0.25; 'this:': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'testing': 0.29; '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; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:* 10': 0.61; 'simple': 0.61; 'times': 0.62; 'provide': 0.64; 'more': 0.64; 'hints': 0.68; 'subject:Testing': 0.84; 'subject::': 0.85; 'lot,': 0.93; 'technique': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re:Testing interactive code using raw_input Date: Mon, 10 Mar 2014 12:40:19 -0400 (EDT) Organization: news.gmane.org References: <531de147$0$29994$c3e8da3$5496439d@news.astraweb.com> X-Gmane-NNTP-Posting-Host: dpc6744198232.direcpc.com X-Newsreader: PiaoHong Usenet NewsReaders 1.36 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394469366 news.xs4all.nl 2925 [2001:888:2000:d::a6]:47523 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68160 Steven D'Aprano Wrote in message: > 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? > How about reassigning sys.stdin to a StringIO buffer? -- DaveA