Testing interactive code using raw_input

S

Steven D'Aprano

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?
 
O

Oscar Benjamin

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?

Use a subprocess?


Oscar
 
D

Dave Angel

Steven D'Aprano said:
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?
 
E

Ethan Furman

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?

pexpect?
 
P

Peter Otten

Steven said:
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?

https://pypi.python.org/pypi/mock

In Python 3 this is part of the standard library:

from unittest import mock

def sum_it():
return "{} + {} = {}".format(input(), input(), input())

with mock.patch('builtins.input', side_effect="123"):
assert sum_it() == "1 + 2 = 3"

I have not yet used it myself so far; instead I did something like

def sum_it(input=input):
...

and then passed a hand-crafted mock input function to increase coverage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top