Please send questions to
st10@humboldt.edu .
# requirements:
# 1. You want to write a GameDie class
# 2. GameDie has a constructor, it expects a desired number of
# sides, and it produces a game die instance with that many
# sides, which has been rolled 0 times, and whose top
# is None (since it hasn't been rolled yet)
# 3. GameDie includes accessors methods such as getNumSides,
# which returns the number of sides of that die,
# getNumRolls, how many times it has been rolled,
# and getLastRoll, the value of the latest roll
# 4. Its roll method should produce a value between 1 and the
# number of sides, should have the effect of changing the
# number of rolls and the last roll values for that die.
# this is the modiule to import to use PyUnit
import unittest
# here's the class to be tested
class GameDie:
def __init__(self, desiredNumSides):
self.numSides = desiredNumSides
self.numRolls = 0
def getNumSides(self):
return self.numSides
def getNumRolls(self):
return self.numRolls
# in PyUnit, test cases are represented by the TestCase class in
# the unittest module
class NumSidesTestCase(unittest.TestCase):
# override the runTest method
def runTest(self):
penta = GameDie(5)
assert penta.getNumSides() == 5, 'incorrect number of sides'
# construct an instance of this test case class:
aNumSidesTestCase = NumSidesTestCase()
# construct a TextTestRunner
aNumSidesRunner = unittest.TextTestRunner()
# and ask the runner to run your test case
aNumSidesRunner.run(aNumSidesTestCase)
class NumRollsTestCase(unittest.TestCase):
def runTest(self):
penta = GameDie(5)
assert penta.getNumRolls() == 0, 'incorrect number of rolls'
# construct an instance of this test case class:
aNumRollsTestCase = NumRollsTestCase()
# construct a TextTestRunner
aNumRollsRunner = unittest.TextTestRunner()
# and ask the runner to run your test case
aNumRollsRunner.run(aNumRollsTestCase)