[Edu-sig] testing interactive loops

Ian Bicking ianb at colorstudy.com
Wed May 17 04:40:16 CEST 2006


Andrew Harrington wrote:
> We have been discussing testing in tutorials with doctest. 
> There is one place that does not work, I think:  testing an interactive 
> loop, which is a construction I teach in introductory programming.
> 
> It appears that doctest cannot handle interactive input.  I would still 
> like to be able to illustrate a session with interactive input and 
> explicit output at the end, and check it automatically.
> 
> If you just do input redirection and compare the actual output to the 
> desired input, you get messed up because the input is not echoed when 
> you redirect from a file.
>  
> The following does not work, but is the idea you want, to force the echo 
> when stdin become a file:
> 
> orig_raw_input = raw_input
> 
> def raw_input_echo(prompt=""):
>     s = orig_raw_input(prompt)
>     print s
>     return s
> 
> raw_input = raw_input_echo
> 
> def input_echo(prompt=""):
>     return eval(raw_input(prompt))

import __builtin__
__builtin__.raw_input = raw_input

I'm pretty sure __builtins__ (that is automatically available 
everywhere) can't be updated like this, except from the __main__ module 
(where it is the same as __builtin__).  This can be confusing, because 
something will work at the interactive prompt or when run as a script, 
but won't work elsewhere.  But anyway, use __builtin__ and you're good.

-- 
Ian Bicking  |  ianb at colorstudy.com  |  http://blog.ianbicking.org


More information about the Edu-sig mailing list