[Edu-sig] testing interactive loops

Andrew Harrington aharrin at luc.edu
Tue May 16 22:31:35 CEST 2006


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))

This fails because it makes raw_input a global for the current module, 
but not a built_in, so it fails when you import and use a module using  
raw_input.
 
I guess we could make it work in little code fragment tests, where you 
append the user's fragment to the code above, in the same module, but it 
is not a general solution.

Is there any better way to do this?

A kludge for tutorial doc tests would be to mark the prompt strings 
somehow in the tutorial source, for instance making it always end with 
:: or ?? so the test string can be parsed to find the responses that 
come after the prompts, and put them in the input file and not in the 
output file.  For example, the test string
... Sum numbers!
... Enter a number (or 0 to quit):: 3
... Enter a number (or 0 to quit):: 4
... Enter a number (or 0 to quit):: 8
... Enter a number (or 0 to quit):: 0
... The sum is 15!

might be processed for web display in the tutorial undoubling the ::

... Sum numbers!
... Enter a number (or 0 to quit): 3
... Enter a number (or 0 to quit): 4
... Enter a number (or 0 to quit): 8
... Enter a number (or 0 to quit): 0
... The sum is 15!

and the tutorial parser would also generate an input file
3
4
8
0

and output file

Sum numbers!
Enter a number (or 0 to quit):
Enter a number (or 0 to quit):
Enter a number (or 0 to quit):
Enter a number (or 0 to quit):
The sum is 15!

Then run the code with redirection and compare the desired and actual 
output.  There still is the issue of the program hanging with the given 
input ... 

How does regular doctest deal with infinite loops or waiting for 
nonexistent keyboard input?

Better ideas?

-- 
  Andrew N. Harrington
  Computer Science Department      Undergraduate Program Director
  Loyola University Chicago        http://www.cs.luc.edu/~anh
  512B Lewis Towers (office)       Office Phone: 312-915-7982
  Snail mail to Lewis Towers 416   Dept. Fax:    312-915-7998
  820 North Michigan Avenue        aharrin at luc.edu
  Chicago, Illinois 60611          



More information about the Edu-sig mailing list