def test_user_roll_again(self, mock_input): user_input_data = ['y', 'Y', 'n', 'q', 'pizza', '121221'] expected_responses = [True, True, False, False, False, False] mock_input.side_effect = user_input_data test_player = Player('test') for expected_response in expected_responses: actual_again = test_player.user_roll_again() self.assertEqual(expected_response, actual_again)
def test_create_players(self, mock_names, mock_int_input): players = game.create_players() expectedPlayers = [Player('a'), Player('b'), Player('c'), Player('d')] # Assert the correct number of players is created self.assertEqual(len(players), len(expectedPlayers)) # And that they have the correct names for actual, expected in zip(players, expectedPlayers): self.assertEqual(actual.name, expected.name)
def test_win(self): test_player = Player('test') test_player.score = 95 test_player.win(6) #should win self.assertTrue(test_player.winner) self.assertEqual(101, test_player.score) test_player = Player('test') test_player.score = 15 test_player.win(6) #should not win self.assertFalse(test_player.winner) self.assertEqual(15, test_player.score) #Score should not increase.
def test_play_game_lose(self): # 1. player rolls a 1 on first roll, turn ends . Score should stay same, not winner test_player = Player('test') test_player.score = 10 test_player.roll_dice = MagicMock(return_value=1) won = test_player.play() self.assertIsNone(won) self.assertFalse(test_player.winner) self.assertEqual(10, test_player.score)
def test_play_game_roll_win(self): # 6. player rolls 2-6 on first roll and accumulates enough points to win. Score should increase, user is winner test_player = Player('test') test_player.score = 99 test_player.roll_dice = MagicMock(return_value=4) won = test_player.play() self.assertTrue(won) self.assertTrue(test_player.winner) self.assertEqual(103, test_player.score)
def create_players(): players = [] number_of_players = ui.positive_int_input('How many players?', 2) names = ui.get_unique_names(number_of_players, 'Player') players = [ Player(name) for name in names ] # List comprehensions are also my favorite thing about Python :) return players
def test_play_game(self): #Create some players with a mock Play method player_1 = Player('test1') player_1.play = MagicMock(side_effect=['', '', '', True]) player_2 = Player('test2') player_2.play = MagicMock(side_effect=['', '', '', '']) player_3 = Player('test3') player_3.play = MagicMock(side_effect=['', '', '', '']) # Test that play continues until a player returns True game.play([player_1, player_2, player_3])
def test_display_winners(self, mock_print): player_1 = Player('test1') player_1.score = 10 player_2 = Player('test2') player_2.winner = True player_2.score = 101 player_3 = Player('test3') player_3.score = 20 # Verify mock_print is called with a message saying player 2 won game.results([player_1, player_2, player_3]) call_args = mock_print.call_args_list mock_print.assert_any_call( '**** Player name: test2 score: 101 **** is the winner!! *****') mock_print.assert_any_call('Player name: test1 score: 10') mock_print.assert_any_call('Player name: test3 score: 20')
def test_play_game_roll_roll_pass(self): #3. player rolls 2-6 on first roll. does not accumulate enough points to win. player rolls again 2-6. Player elects to end turn. Score should increase, not winner test_player = Player('test') test_player.score = 10 test_player.roll_dice = MagicMock(side_effect=[4, 3]) test_player.user_roll_again = MagicMock(side_effect=[True, False]) won = test_player.play() self.assertIsNone(won) self.assertFalse(test_player.winner) self.assertEqual(17, test_player.score)
def test_play_game_roll_lose(self): # 5. player rolls 2-6 on first roll. does not accumulate enough points to win. player rolls again 1. Score should stay the same, not winner test_player = Player('test') test_player.score = 10 rolls = [4, 1] user_says = [True] test_player.roll_dice = MagicMock(side_effect=rolls) test_player.user_roll_again = MagicMock(side_effect=user_says) won = test_player.play() self.assertIsNone(won) self.assertFalse(test_player.winner) self.assertEqual(10, test_player.score)
def test_play_game_roll_many(self): # 4. player rolls 2-6 on first roll. does not accumulate enough points to win. player rolls again 2-6. Player repeats several times, each time 2-6. Player elects to end turn. Score should increase, not winner test_player = Player('test') test_player.score = 10 rolls = [4, 3, 6, 2, 3, 6, 5, 2] user_says = [True, True, True, True, True, True, True, False] test_player.roll_dice = MagicMock(side_effect=rolls) test_player.user_roll_again = MagicMock(side_effect=user_says) won = test_player.play() self.assertIsNone(won) self.assertFalse(test_player.winner) self.assertEqual(10 + sum(rolls), test_player.score)
def test_play_game_roll_roll_win(self): # 7. player rolls 2-6 on first roll. does not accumulate enough points to win. player rolls again 2-6, accumulates enough points to win. Score should increase, user is winner test_player = Player('test') test_player.score = 91 rolls = [6, 5] user_says = [True] test_player.roll_dice = MagicMock(side_effect=rolls) test_player.user_roll_again = MagicMock(side_effect=user_says) won = test_player.play() self.assertTrue(won) self.assertTrue(test_player.winner) self.assertEqual(102, test_player.score)