def test_perfect_game(self): """Run a unit test where every throw hits every pin """ game = BowlingGame() self.throw_many(game, 12, 10) game.calculate_score() self.assertEqual(game.score, 300)
def test_all_gutters(self): """Run a unit test where every throw hits zero pins """ game = BowlingGame() self.throw_many(game, 20 ,0 ) game.calculate_score() self.assertEqual(game.score,0)
def test_all_ones(self): """Run a unit test where every throw hits one pin """ game = BowlingGame() number_of_times = 20 pins = 1 self.throw_many(game, number_of_times, pins) game.calculate_score() self.assertEqual(game.score, 20)
class TestBowlingGame: @pytest.fixture def game(self): self.the_game = BowlingGame() def test_should_score_zero_when_no_rolls(self, game): assert_that(self.the_game.get_score()).is_equal_to(0) def test_should_score_20_when_pin_down_20_times(self, game): self.roll(20, 1) assert_that(self.the_game.get_score()).is_equal_to(20) def test_should_score_0_when_no_pin_down_20_times(self, game): self.roll(20, 0) assert_that(self.the_game.get_score()).is_equal_to(0) def test_should_score_spare(self, game): self.roll(2, 5) self.roll(1, 4) self.roll(17, 0) assert_that(self.the_game.get_score()).is_equal_to(18) def test_should_score_strike(self, game): self.roll1([10, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) assert_that(self.the_game.get_score()).is_equal_to(26) def roll(self, throws, pins): for i in range(0, throws): self.the_game.roll(pins) def roll1(self, list_of_pins): for pin in list_of_pins: self.the_game.roll(pin) pass
def test_for_spare(self): """Run a unit test where a spare is awarded (bonus points awarded after all 10 pins hit within two throws) """ game = BowlingGame() game.throw(4) game.throw(6) game.throw(7) game.throw(0) for _ in range(16): game.throw(0) game.calculate_score() self.assertEqual(game.score, 24)
def test_bowlingGameOnlyDecetsAStrikeWhenAppropriate(): game = BowlingGame() game.roll(5) game.roll(5) game.roll(4) game.roll(4) game.roll(5) assert game.totalScore() == 27
def test_spare_score(self): player = Player("Test", BowlingGame()) player.bowlingGame.roll(8) player.bowlingGame.roll(2) player.bowlingGame.roll(2) player.bowlingGame.roll(1) self.assertEqual(15, player.bowlingGame.score())
def test_for_strike(self): """Run a unit test where a strike is awarded (bonus points are awarded following all 10 pins being hit) """ game=BowlingGame() game.throw(10) game.throw(4) game.throw(2) self.throw_many(game, 17,0) game.calculate_score() self.assertEqual(game.score, 22)
def test_strike_brief(self): """test another configuration that would grant a strike, as shown in the brief """ game=BowlingGame() game.throw(10) game.throw(3) game.throw(6) self.throw_many(game, 17, 0) game.calculate_score() self.assertEqual(game.score, 28)
def test_successive_strikes(self): """test for successive strikes calculating, as shown in the brief """ game=BowlingGame() game.throw(10) game.throw(10) game.throw(4) game.throw(2) self.throw_many(game, 16, 0) game.calculate_score() self.assertEqual(game.score, 46)
def test_last_frame_strike(self): player = Player("Test", BowlingGame()) player.bowlingGame.roll(2) player.bowlingGame.roll(2) for i in range(8): player.bowlingGame.roll(2) player.bowlingGame.roll(2) player.bowlingGame.roll(10) player.bowlingGame.roll(2) player.bowlingGame.roll(2) self.assertEqual(50, player.bowlingGame.score())
def test_last_frame_spare(self): player = Player("Test", BowlingGame()) player.bowlingGame.roll(2) player.bowlingGame.roll(2) for i in range(8): player.bowlingGame.roll(2) player.bowlingGame.roll(2) player.bowlingGame.roll(6) player.bowlingGame.roll(4) player.bowlingGame.roll(2) self.assertEqual(48, player.bowlingGame.score())
def test_BowlingGameDetectsSparesOnlyWhenNotInTheSameFrame(): game = BowlingGame() game.roll(4) game.roll(5) game.roll(5) game.roll(4) assert game.totalScore() == 18
class BowlingGameTest(unittest.TestCase): def setUp(self): self._game = BowlingGame() def rollMany(self, n, pincount): for i in range(n): self._game.roll(pincount) # First test def testGutterGame(self): self.rollMany(20,0) assert self._game.getScore() == 0 # Second test def testAllOnes(self): self.rollMany(20,1) assert self._game.getScore() == 20 def testRollSpare(self): self._game.roll(5) self._game.roll(5) self._game.roll(3) self.rollMany(17,0) assert self._game.getScore() == 16
def test_different_throws (self): """Run a unit test where a variety of pins are hit """ game = BowlingGame() game.throw(6) game.throw(0) game.throw(7) game.throw(0) game.throw(2) for _ in range(15): game.throw(0) game.calculate_score() self.assertEqual(game.score, 15)
def test_strike_score(self): player = Player("Test", BowlingGame()) player.bowlingGame.roll(10) player.bowlingGame.roll(2) player.bowlingGame.roll(2) self.assertEqual(18, player.bowlingGame.score())
def test_all_strike(self): player = Player("Test", BowlingGame()) for i in range(10): player.bowlingGame.roll(10) self.assertEqual(300, player.bowlingGame.score())
def testGutterGame(): game = BowlingGame() for i in range(20): game.bowl(0) Assert.Equal(0, game.score())
def test_frames(self): # normal: 10 frames bg = BowlingGame() bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2) bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2) self.assertEqual(bg.get_frame_count(), 10) self.assertEqual(bg.score(), 40) # normal: 11 frames bg = BowlingGame() bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2) bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2) self.assertEqual(bg.get_frame_count(), 10) self.assertEqual(bg.score(), 40) # spare: 11 frames bg = BowlingGame() bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2) bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 8, 2) self.assertEqual(bg.get_frame_count(), 10) self.assertEqual(bg.score(), 50) # strike: 11 frames bg = BowlingGame() bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2) bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 10, 4, 5) self.assertEqual(bg.get_frame_count(), 10) self.assertEqual(bg.score(), 64)
def test_spare_game(self): # start with a spare bg = BowlingGame() bg._roll_many(4, 6, 5, 0) self.assertEqual(bg.score(), 20) self.assertEqual(bg.get_frame_count(), 2) # mid-game with a spare bg = BowlingGame() bg._roll_many(2, 3, 5, 5, 2, 7) self.assertEqual(bg.score(), 26) self.assertEqual(bg.get_frame_count(), 3) # end with a spare bg = BowlingGame() bg._roll_many(3, 5, 9, 1) self.assertEqual(bg.score(), 18) self.assertEqual(bg.get_frame_count(), 2)
class BowlingGameTest(unittest.TestCase): def setUp(self): self.game = BowlingGame() def rollMany(self, pins, times): for i in xrange(0, times): self.game.roll(pins) def rollSpare(self): self.game.roll(5) self.game.roll(5) def rollStrike(self): self.game.roll(10) def test_worstGame(self): self.rollMany(0, 20) self.assertEqual(0, self.game.get_score()) def test_onePin(self): self.rollMany(1, 20) self.assertEqual(20, self.game.get_score()) def test_spare(self): self.rollSpare() self.game.roll(2) self.rollMany(0, 17) self.assertEqual(14, self.game.get_score()) def test_strike(self): self.rollStrike() self.game.roll(3) self.game.roll(4) self.rollMany(0, 17) self.assertEqual(24, self.game.get_score()) def test_double_strike(self): self.rollStrike() self.rollStrike() self.game.roll(4) self.game.roll(2) self.rollMany(0, 16) self.assertEqual(46, self.game.get_score()) def test_perfect_game(self): self.rollMany(10, 12) self.assertEqual(300, self.game.get_score())
def test_BowlingGameAddsTheFirstRollToTheScore(): for rollValue in range(0,9): game = BowlingGame() game.roll(rollValue) assert game.totalScore() == rollValue
def test_BowlingGameDoublesTheNextRollAfterASpare(): game = BowlingGame() game.roll(5) game.roll(5) game.roll(6) assert game.totalScore() == 22
def testZero(): game = BowlingGame() Assert.Equal(0, game.score())
class BowlingGameTest(unittest.TestCase): def setUp(self): self._game = BowlingGame() def rollMany(self, n, pincount): for i in range(n): self._game.roll(pincount) # first test def testGutterGame(self): self.rollMany(20,0) assert self._game.getScore() == 0 # second test def testAllOnes(self): self.rollMany(20,1) assert self._game.getScore() == 20 def testRollSpare(self): self._game.roll(5) self._game.roll(5) self._game.roll(3) self.rollMany(17,0) assert self._game.getScore() == 16 def testOneStrike(self): self.rollStrike() self._game.roll(3) self._game.roll(4) self.rollMany(16,0) assert self._game.getScore() == 24 def testPerfectGame(self): self.rollMany(12,10) assert self._game.getScore() == 300 def rollSpare(self): self._game.roll(5) self._game.roll(5) def rollStrike(self): self._game.roll(10)
from BowlingGame import BowlingGame from Player import Player player = Player("Nakia", BowlingGame()) player.bowlingGame.roll(10) score = player.bowlingGame.score()
class TestBowlingGame(unittest.TestCase): def setUp(self): self.game = BowlingGame() def testAllGuttersReturns0(self): self.roll_times(0, 20) self.assertEquals(0, self.game.score()) def testAll1sReturns20(self): self.roll_times(1, 20) self.assertEquals(20, self.game.score()) def testItHandlesSpares(self): self.roll_spare() self.game.roll(5) self.roll_times(0, 17) self.assertEquals(20, self.game.score()) def testItHandlesStrikes(self): self.roll_strike() self.game.roll(5) self.game.roll(3) self.roll_times(0, 17) self.assertEquals(26, self.game.score()) def testPerfectGameIs300(self): self.roll_times(10, 12) self.assertEquals(300, self.game.score()) def testDutch200Is200(self): for frame in range(10): self.roll_strike() self.roll_spare() self.assertEquals(200, self.game.score()) def testGutterUntilLastFrameTurkeyIs30(self): self.roll_times(0, 18) self.roll_strike() self.roll_strike() self.roll_strike() self.assertEquals(30, self.game.score()) def roll_spare(self): self.game.roll(1) self.game.roll(9) def roll_strike(self): self.game.roll(10) def roll_times(self, pins_toppled, times): for _ in range(times): self.game.roll(pins_toppled)
class BowlingGameTests(TestCase): def setUp(self): self.game = BowlingGame() def test_all_gutter(self): for i in range(20): self.game.roll(0) self.assertEqual(0, self.game.score, "All gutters should get final score 0") def test_all_ones(self): for i in range(20): self.game.roll(1) self.assertEqual(20, self.game.score, "All ones should get 20 final score") def test_spare_then_3_pins(self): self.game.roll(9) self.game.roll(1) self.game.roll(3) for i in range(17): self.game.roll(0) self.assertEqual(16, self.game.score, "Should be 16 - spare then 3 pins down") def test_strike_then_3_4_pins(self): self.game.roll(10) self.game.roll(3) self.game.roll(4) for i in range(17): self.game.roll(0) self.assertEqual(24, self.game.score, "Should be 24 - strike then 3 and 4 pins down") def test_all_strikes(self): for i in range(12): self.game.roll(10) self.assertEqual(300, self.game.score, "All ones should get 300 final score")
from random import randrange from BowlingGame import BowlingGame if __name__ == '__main__': bg = BowlingGame() # letting a random game play itself, just cause :) for frame in range(10): roll1 = randrange(0, 11) bg.roll(roll1) print 'Frame #{}: {}'.format(frame + 1, roll1) if roll1 == 10: # strike if frame == 9: # the 10th frame roll2 = randrange(0, 11) bg.roll(roll2) print 'Frame #{}: {}'.format(frame + 1, roll2) if roll2 == 10: # another strike so roll 3 has 10 pins up roll3 = randrange(0, 11) else: roll3 = randrange(0, 10 - roll2) bg.roll(roll3) print 'Frame #{}: {}'.format(frame + 1, roll3) else: continue else: roll2 = randrange(0, 10 - roll1) print 'Frame #{}: {}'.format(frame + 1, roll2) bg.roll(roll2) print 'Final Score: {}'.format(bg.score()) print 'Pins Toppled: {}'.format(bg.pins_toppled)
def test_strike_game(self): # start with a strike bg = BowlingGame() bg._roll_many(10, 5, 4) self.assertEqual(bg.score(), 28) self.assertEqual(bg.get_frame_count(), 2) # mid-game with a strike bg = BowlingGame() bg._roll_many(7, 1, 10, 3, 3) self.assertEqual(bg.score(), 30) self.assertEqual(bg.get_frame_count(), 3) # end-game with a strike bg = BowlingGame() bg._roll_many(4, 1, 10) self.assertEqual(bg.score(), 15) self.assertEqual(bg.get_frame_count(), 2)
def test_sample_game(self): """testing an example game, with a mix of open frames, spares and strikes """ game=BowlingGame() game.throw(10) game.throw(7) game.throw(3) game.throw(7) game.throw(2) game.throw(9) game.throw(1) game.throw(10) game.throw(10) game.throw(10) game.throw(2) game.throw(3) game.throw(6) game.throw(4) game.throw(7) game.throw(3) game.throw(3) game.calculate_score() self.assertEqual(game.score, 168)
def test_normal_game(self): bg = BowlingGame() bg._roll_many(3, 6, 1, 7, 4, 4) self.assertEqual(bg.score(), 25) self.assertEqual(bg.get_frame_count(), 3)
def test_frame_score(self): player = Player("Test", BowlingGame()) player.bowlingGame.roll(2) player.bowlingGame.roll(2) self.assertEqual(4, player.bowlingGame.score())
def setUp(self): self.game = BowlingGame()
def test_bowlingGameIdentifiesAStrike(): game = BowlingGame() game.roll(10) game.roll(5) game.roll(4) assert game.totalScore() == 28
def game(self): self.the_game = BowlingGame()
def test_BowlingGameSumsNewRollToTotal(): game = BowlingGame() game.roll(2) game.roll(3) assert game.totalScore() == 5
def new_game(self): return BowlingGame()