Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unittest
- import csv
- from game import BowlingGame,GameNotStarted,GameNotFinished,GameOver
- class TestBowlingGame(unittest.TestCase):
- """ Test fixture """
- def setUp(self):
- self.game = BowlingGame()
- ## Exception Test cases
- def testGameNotStarted(self):
- """ should throw exception when attempting to score on a game that did not start """
- try:
- self.game.score()
- except GameNotStarted:
- pass
- else:
- self.fail ("Expected to have GameNotStarted exception here")
- def testGameNotFinished(self):
- """ should throw exception when attempting to score on a game that did complete """
- try:
- self.game.play(1)
- self.game.score()
- except GameNotFinished:
- pass
- else:
- self.fail ("Expected to have GameNotFinished exception here")
- def testGameOver(self):
- """ No more rolls after the max rolls """
- #play all the 21 rolls.
- for roll in range(21):
- self.game.play(1)
- #now play one more roll
- try:
- self.game.play(1)
- except GameOver:
- pass
- else:
- self.fail ("Expected to have GameOver exception here")
- ## Exception Test cases
- ##BEGIN: Test cases when no spare or strikes
- def testZeroScoreGame(self):
- """ No points scored on the game. Simulating my wife's game """
- #play all the 20 rolls.
- for roll in range(20):
- self.game.play(0)
- self.assertEqual(0, self.game.score())
- def testOnePinDownForAllRolls(self):
- """ 1 points scored on each roll. Simulating my wife's game """
- for roll in range(20):
- self.game.play(1)
- self.assertEqual(20, self.game.score())
- def testTwoPinDownForAllRolls(self):
- for roll in range(20):
- self.game.play(2)
- self.assertEqual(20 * 2, self.game.score())
- ##END: Test cases when no spare or strikes
- ### BEGIN: SPARE TEST CASES ###
- def testGameWithOneSpareAtStart(self):
- """ There is one spare """
- self.game.play(1)
- self.game.play(9) #hurray knocked all down in second roll
- self.game.play(5)
- #only one point for rest of the game
- for roll in range(17):
- self.game.play(1)
- self.assertEqual(15 + 5 + 17, self.game.score())
- def testGameWithOneSpareInMiddle(self):
- """ There is one spare """
- for roll in range(16):
- self.game.play(1) #score of 16
- self.game.play(1) #
- self.game.play(9) #A spare. Frame score: 9 + 1 + 5 = 15
- self.game.play(5) #frame after spare
- self.game.play(4)
- self.assertEqual(16 + 15 + 9, self.game.score())
- def testGameWithOneSpareAtEnd(self):
- """ There is one spare """
- for roll in range(18):
- self.game.play(1)
- self.game.play(9) #hurray
- self.game.play(1)
- self.game.play(4) #extra roll
- self.assertEqual(32, self.game.score())
- ### END: SPARE TEST CASES ###
- ### BEGIN: STRIKE TEST CASES ###
- def testGameWithOneStrikeAtStart(self):
- self.game.play(10) #STRIKE
- self.game.play(5) #Frame score: 15 Bonus : 6 (5+1)
- #only one point for rest of the game
- for roll in range(18):
- self.game.play(1)
- self.assertEqual(21 + 18, self.game.score())
- def testGameWithOneStrikeInMiddle(self):
- for roll in range(16):
- self.game.play(1) #score of 16
- self.game.play(1) #
- self.game.play(10) #A spare. Frame score: 11 Bonus: 9
- self.game.play(5) #frame after spare
- self.game.play(4)
- self.assertEqual(16 + 20 + 9, self.game.score())
- def testGameWithOneStrikeAtEnd(self):
- for roll in range(18):
- self.game.play(1)
- self.game.play(10) #Frame score: 12 Bonus: 6
- self.game.play(2)
- self.game.play(4) #extra roll
- self.assertEqual(18 + 18, self.game.score())
- ### END: STRIKE TEST CASES ###
- ### Two strikes
- def testGameWithTwoStrikeAtEnd(self):
- for roll in range(18):
- self.game.play(1)
- self.game.play(10) #Frame score: 20 Bonus: 18
- self.game.play(10)
- self.game.play(4) #extra roll
- self.assertEqual(18 + 38, self.game.score())
- ### Two Strikes
- ### Data driven tests
- def testDataDriven(self):
- with open('data.csv', 'rb') as csvfile:
- testDatareader = csv.reader(csvfile)
- for gameRow in testDatareader: #for every game in the test data
- bowlingGame = BowlingGame() #start a new game
- expectedScoreColIndex = len(gameRow)-1 #what is the expected score
- expectedScore = int(gameRow[len(gameRow)-1])
- #play each roll in the game
- for index,rollCol in enumerate(gameRow):
- if index < expectedScoreColIndex:
- bowlingGame.play(int(rollCol))
- #now check the score against expected score
- self.assertEqual(bowlingGame.score(),expectedScore)
- ### Data drivent tests
- if __name__ == "__main__":
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement