Advertisement
rajaramanathan

Bowling Game Unit Test Module

Nov 9th, 2012
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.54 KB | None | 0 0
  1. import unittest  
  2. import csv
  3. from game import BowlingGame,GameNotStarted,GameNotFinished,GameOver
  4.  
  5. class TestBowlingGame(unittest.TestCase):  
  6.     """ Test fixture """
  7.  
  8.     def setUp(self):  
  9.         self.game = BowlingGame()  
  10.  
  11. ## Exception Test cases
  12.     def testGameNotStarted(self):  
  13.         """ should throw exception when attempting to score on a game that did not start """
  14.         try:
  15.             self.game.score()
  16.         except GameNotStarted:
  17.             pass
  18.         else:
  19.             self.fail ("Expected to have GameNotStarted exception here")
  20.  
  21.     def testGameNotFinished(self):  
  22.         """ should throw exception when attempting to score on a game that did complete """
  23.         try:
  24.             self.game.play(1)
  25.             self.game.score()
  26.         except GameNotFinished:
  27.             pass
  28.         else:
  29.             self.fail ("Expected to have GameNotFinished exception here")
  30.            
  31.     def testGameOver(self):
  32.         """ No more rolls after the max rolls """
  33.        
  34.         #play all the 21 rolls.
  35.         for roll in range(21):
  36.             self.game.play(1)
  37.        
  38.         #now play one more roll
  39.         try:
  40.             self.game.play(1)
  41.         except GameOver:
  42.             pass
  43.         else:
  44.             self.fail ("Expected to have GameOver exception here")
  45. ## Exception Test cases
  46.  
  47. ##BEGIN: Test cases when no spare or strikes
  48.     def testZeroScoreGame(self):
  49.         """ No points scored on the game. Simulating my wife's game """
  50.         #play all the 20 rolls.
  51.         for roll in range(20):
  52.             self.game.play(0)
  53.         self.assertEqual(0, self.game.score())
  54.  
  55.     def testOnePinDownForAllRolls(self):
  56.         """ 1 points scored on each roll. Simulating my wife's game """
  57.         for roll in range(20):
  58.             self.game.play(1)
  59.         self.assertEqual(20, self.game.score())
  60.  
  61.     def testTwoPinDownForAllRolls(self):
  62.         for roll in range(20):
  63.             self.game.play(2)
  64.         self.assertEqual(20 * 2, self.game.score())
  65. ##END: Test cases when no spare or strikes
  66.  
  67. ### BEGIN: SPARE TEST CASES ###
  68.     def testGameWithOneSpareAtStart(self):
  69.         """ There is one spare """
  70.        
  71.         self.game.play(1)
  72.         self.game.play(9) #hurray knocked all down in second roll
  73.         self.game.play(5)
  74.         #only one point for rest of the game
  75.         for roll in range(17):
  76.             self.game.play(1)
  77.         self.assertEqual(15 + 5 + 17, self.game.score())
  78.        
  79.     def testGameWithOneSpareInMiddle(self):
  80.         """ There is one spare """
  81.        
  82.         for roll in range(16):
  83.             self.game.play(1)  #score of 16
  84.  
  85.         self.game.play(1) #
  86.         self.game.play(9) #A spare. Frame score: 9 + 1 + 5 = 15
  87.  
  88.         self.game.play(5) #frame after spare
  89.         self.game.play(4)
  90.  
  91.         self.assertEqual(16 + 15 + 9, self.game.score())
  92.  
  93.     def testGameWithOneSpareAtEnd(self):
  94.         """ There is one spare """
  95.        
  96.         for roll in range(18):
  97.             self.game.play(1)
  98.  
  99.         self.game.play(9) #hurray
  100.         self.game.play(1)
  101.         self.game.play(4) #extra roll
  102.         self.assertEqual(32, self.game.score())
  103. ### END: SPARE TEST CASES ###
  104.  
  105. ### BEGIN: STRIKE TEST CASES ###
  106.     def testGameWithOneStrikeAtStart(self):
  107.         self.game.play(10) #STRIKE
  108.         self.game.play(5)  #Frame score: 15 Bonus : 6 (5+1)
  109.        
  110.         #only one point for rest of the game
  111.         for roll in range(18):
  112.             self.game.play(1)
  113.         self.assertEqual(21 + 18, self.game.score())
  114.        
  115.     def testGameWithOneStrikeInMiddle(self):
  116.         for roll in range(16):
  117.             self.game.play(1)  #score of 16
  118.  
  119.         self.game.play(1) #
  120.         self.game.play(10) #A spare. Frame score: 11 Bonus: 9
  121.  
  122.         self.game.play(5) #frame after spare
  123.         self.game.play(4)
  124.  
  125.         self.assertEqual(16 + 20 + 9, self.game.score())
  126.  
  127.     def testGameWithOneStrikeAtEnd(self):
  128.         for roll in range(18):
  129.             self.game.play(1)
  130.  
  131.         self.game.play(10) #Frame score: 12 Bonus: 6
  132.         self.game.play(2)
  133.         self.game.play(4) #extra roll
  134.         self.assertEqual(18 + 18, self.game.score())
  135. ### END: STRIKE TEST CASES ###
  136.  
  137. ### Two strikes
  138.     def testGameWithTwoStrikeAtEnd(self):
  139.         for roll in range(18):
  140.             self.game.play(1)
  141.  
  142.         self.game.play(10) #Frame score: 20 Bonus: 18
  143.         self.game.play(10)
  144.         self.game.play(4) #extra roll
  145.         self.assertEqual(18 + 38, self.game.score())
  146. ### Two Strikes
  147.  
  148. ### Data driven tests
  149.     def testDataDriven(self):
  150.          with open('data.csv', 'rb') as csvfile:
  151.             testDatareader = csv.reader(csvfile)
  152.             for gameRow in testDatareader: #for every game in the test data
  153.                 bowlingGame = BowlingGame() #start a new game
  154.                 expectedScoreColIndex = len(gameRow)-1 #what is the expected score
  155.                 expectedScore = int(gameRow[len(gameRow)-1])
  156.                 #play each roll in the game
  157.                 for index,rollCol in enumerate(gameRow):
  158.                     if index < expectedScoreColIndex:
  159.                         bowlingGame.play(int(rollCol))
  160.                 #now check the score against expected score
  161.                 self.assertEqual(bowlingGame.score(),expectedScore)
  162. ### Data drivent tests
  163.  
  164.  
  165. if __name__ == "__main__":  
  166.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement