Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68160

Re:Testing interactive code using raw_input

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 <python-python-list@m.gmane.org>
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 <davea@davea.name>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8010.1394469366.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


 Steven D'Aprano <steve+comp.lang.python@pearwood.info> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Testing interactive code using raw_input Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-10 15:59 +0000
  Re: Testing interactive code using raw_input Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-03-10 16:05 +0000
  Re:Testing interactive code using raw_input Dave Angel <davea@davea.name> - 2014-03-10 12:40 -0400
  Re: Testing interactive code using raw_input Peter Otten <__peter__@web.de> - 2014-03-10 17:57 +0100
    Re: Testing interactive code using raw_input Steven D'Aprano <steve@pearwood.info> - 2014-03-11 08:26 +0000
  Re: Testing interactive code using raw_input Ethan Furman <ethan@stoneleaf.us> - 2014-03-10 09:53 -0700
  Re: Testing interactive code using raw_input Ben Finney <ben+python@benfinney.id.au> - 2014-03-11 10:39 +1100

csiph-web