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


Groups > comp.lang.python > #68160

Re:Testing interactive code using raw_input

From Dave Angel <davea@davea.name>
Subject Re:Testing interactive code using raw_input
Date 2014-03-10 12:40 -0400
Organization news.gmane.org
References <531de147$0$29994$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.8010.1394469366.18130.python-list@python.org> (permalink)

Show all headers | 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