471,119 Members | 1,205 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

doctests for interactive functions

Hi all,

I have a module of classes for getting input from the user that
satisfies various constraints. For instance, one class is created with
a menu of option, presents them to the user, and rejects any input
other than a menu option; another ensures that the user's input is
interpretable as an integer between specified bounds, etc.

It is a module I wrote up when in the early stages of learning Python.
It's a bit ugly :-) So, I am rewriting it, and this time also am
putting in unit tests (I am using doctest).

Since the classes are for getting input interactively, a
straightforward use of doctest is not going to work. (The tests won't
run automatically.) I've come up with a way to make it work, but I
would like to know from more experienced people if there are better
ways that I am overlooking.

All classes take an optional argument input_function that determines
how the class instance gets its input. If this is not provided, it
defaults to raw_input. doctest tests reassign it so that they can be
run automatically.
So, an actual interactive session might look like:
>>yn = IG.YesNo()
yn.get()
Yes or no?
What about?
You have entered 'What about?'.
You must enter a value in ['n', 'no', 'y', 'yes'].
(Case does not matter.)
Please try again.
Yes or no?
N
>>yn.data
False
>>>

The corresponding doctest I've constructed looks like:
>>inputs = ['NO', 'What about?']
yn = IG.YesNo()
yn.get(input_function=lambda: inputs.pop())
Yes or no?
You have entered 'What about?'.
You must enter a value in ['n', 'no', 'y', 'yes'].
(Case does not matter.)
Please try again.
Yes or no?
>>yn.data
False
That works fine as a unit test. But, it doesn't score too well on the
`executable documentation' metric, as there is the inputs.pop() in the
way, and the user input doesn't make it on to the screen.

Is there any way to make the doctests look more like actual
interactive sessions, while preserving their automatic runability?

Thanks and best,

Brian vdB

Feb 8 '07 #1
2 1840
On 2007-02-08, Brian van den Broek <br***@cc.umanitoba.cawrote:
All classes take an optional argument input_function that
determines how the class instance gets its input. If this is
not provided, it defaults to raw_input. doctest tests reassign
it so that they can be run automatically.
What I've done in these cases is create a file containing my test
input, and before running the doctests I remap sys.stdin to my
file of test data. Then you don't need test code cluttering up
your functions.

--
Neil Cerutti
We don't necessarily discriminate. We simply exclude certain types of people.
--Colonel Gerald Wellman
Feb 8 '07 #2
Neil Cerutti said unto the world upon 02/08/2007 02:25 PM:
On 2007-02-08, Brian van den Broek <br***@cc.umanitoba.cawrote:
>All classes take an optional argument input_function that
determines how the class instance gets its input. If this is
not provided, it defaults to raw_input. doctest tests reassign
it so that they can be run automatically.

What I've done in these cases is create a file containing my test
input, and before running the doctests I remap sys.stdin to my
file of test data. Then you don't need test code cluttering up
your functions.

Hi Neil and all,

Thanks for the suggestion, Neil. I will look into it.

I am not worried about the test code cluttering up my module's source,
as I am using doctest.testfile to run doctests defined in a separate
file anyway. I am looking to produce illustrative examples for the
user manual which also can serve as tests. I'll play around and see if
remapping sys.stdin reduces the gap between the appearance of the
doctest examples and of actual use cases as compared to my current
approach.

Thanks again,

Brian vdB

Feb 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by dogdog | last post: by
reply views Thread by Diego Zeballos Fraigne | last post: by
22 posts views Thread by SeeBelow | last post: by
5 posts views Thread by sugaray | last post: by
2 posts views Thread by WJ | last post: by
reply views Thread by pkassianidis | last post: by
4 posts views Thread by j_macaroni | last post: by
2 posts views Thread by bearophileHUGS | last post: by
1 post views Thread by nriley | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.