[Tutor] Unit testing individual modules

Patti Scott pscott_74 at yahoo.com
Tue Nov 19 17:06:40 CET 2013


Python 2.4
self-study with Python Programming: An Introduction to Computer Science by Zelle
 
I am trying to import a program that has a conditional execution statement at the end so I can troubleshoot individual modules in the main() program.
 
Below is the textbook example program.  When I try to import the program from Idle, I get 
 
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in -toplevel-
    import rball
ImportError: No module named rball
 
(Also if I try to import with .py or .pyc)
 
From the commandline interface, I can import the program, but __name__  is still  "__main__" and I haven't successfully unit tested any of the individual functions.
 
 
 
# rball.py
# textbook example of top-down design for Monte Carlo raquetball simulation
from random import random
def main():
    #printIntro()
    #probA, probB, n = getInputs()
    #winsA, winsB = simNGames(n,probA,probB)
    #printSummary(winsA, winsB)
    def printIntro():
        print "This program simulates a game of racquetball between two"
        print 'players called "A" and "B."  The ability of each player is'
        print "indicated by a probability (a number between 0 and 1) that"
        print "the player wins the point when serving.  Player A always"
        print "has the first serve."
    def getInputs():
        #Returns the three simulation parameters
        n = input("How many games to simulate? ")
        a = input("What is the probability player A wins service? ")
        b = input("What is the probability player B wins service? ")
        return a, b, n
    
    def simNGames(n, probA, probB):
        #simulates n games of racquetball between players whose
        #   abilities are represented by the prob. of winning serivce
        #Returns total number of wins for A and B
        winsA = winsB = 0
        for i in range(n):
            scoreA, scoreB = simOneGame(probA, probB)
            if scoreA > scoreB:
                winsA = winsA + 1
            else:
                winsB = winsB + 1
        return winsA, winsB
    def simOneGame(probA, probB):
        #Simulates a single game between players whose
        #   abilities are represented by the prob. of winning service.
        # Returns final scores for A and B, one game
        serving = 'A'
        scoreA = scoreB = 0
        while not gameOver(scoreA, scoreB):
            if serving == 'A':
                if random()< probA:
                    scoreA = scoreA + 1
                else:
                    serving = 'B'
            else:
                if random() < probB:
                    scoreB = scoreB + 1
                else:
                    serving = 'A'
        return scoreA, scoreB
    def gameOver(a,b):
        # a and b represent running scores for one racquetball game
        # Returns True if game over, False otherwise
        return a==15 or b==15
    def printSummary(winsA, winsB):
        # Prints a summary of wins for each player.
        n = winsA + winsB
        print
        print "Games simulated:", n
        print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
        print "Wins for B: %d (%0.1f%%)" % (winsB, float(winsB)/n*100)
    printIntro()
    probA, probB, n = getInputs()
    winsA, winsB = simNGames(n,probA,probB)
    printSummary(winsA, winsB)
if __name__ == "__main__": main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131119/1dd497ee/attachment.html>


More information about the Tutor mailing list