473,394 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 1953
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: dogdog | last post by:
I have just downloaded and installed mod_perl with the latest apache. But .. I'm not sure how to make or even use this. I was thinking that it would allow me to perform interactive type actions...
0
by: Diego Zeballos Fraigne | last post by:
Hi there, we have a problem when using NetDDE from a non-interactive session Our solution is a VB6 program which - among other things - requests data from a DDE server. The program is not a...
22
by: SeeBelow | last post by:
Is there any way, in C, of interacting with a running program, but still using code that is portable, at least between linux and Windows? By "interacting" it could be something as simple as...
5
by: sugaray | last post by:
Right now, the code I wrote below seems works fine, i know it's definitely not the best solution, so if there are better solutions to eschew from using goto statements to jump out of multiple loops...
2
by: WJ | last post by:
I have three ASPX pages: 1. "WebForm1.aspx" is interactive, responsible for calling a web site (https://www.payMe.com) with $$$. It is working fine. 2. "WebForm2.aspx" is non-interactive, a...
0
by: pkassianidis | last post by:
Hello everybody, I have written a python script which executes some functions and then returns to the python interactive shell prompt (In other words I use the command "#!/usr/bin/python -i "...
4
by: j_macaroni | last post by:
I know you can start php -a and go into interactive mode. This works well exept you have to keep putting <?php code here ; bla blah code ; Couple of problems with this. 1) Exits php when you...
2
by: bearophileHUGS | last post by:
Recently I have posted this same question on it.comp.lang.python, maybe there aren't solutions, but I'd like to know what you think. Can doctests be added to nested functions too? (This can be...
1
by: nriley | last post by:
Hi, I've got a number of doctests which rely on a certain output format, and since I wrote the tests I've changed the output format. Now, almost all the tests fail. What I'd like is if I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.