ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] unit testing raw_input()
by Alan Gauld other posts by this author
Apr 18 2006 11:01PM messages near this date
Re: [Tutor] unit testing raw_input() | Re: [Tutor] unit testing raw_input()
>  Suppose I had a function like the following:
#############
def y_n(prompt="Answer yes or no"):
    while True:
        answer = raw_input(prompt)
        if answer in ['y', 'Y', 'yes']:
            print "You said yes!"
            break
        elif answer in ['n', 'N', 'no']:
            print "You said no!"
            break
        else:
            print "%s is an invalid answer."%answer
################
>  How could I go about to write an automated test for it?

Create a data file with all of the inputs you need and use input 
redirection to run it. Assuming its called y_n.py:

$ python y_n.py <y_n.in >  y_n.out

This is the easiest way of testing interactive programs.
This has limitations for unit testing if you have more 
than one function per module however, in that case 
you need to write a driver module that imports yours 
and takes as a first input the function you want to test...
In Python the driver can sit inside the 
if __name__ == __main__ stanza

Alan G

_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Andre Roberge
Andre Roberge
Danny Yoo
Alan Gauld
Michael

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved