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