class FleeTest(unittest.TestCase): """Test the functionality of the Monster class flee function.""" def setUp(self): self.new_monster = Monster("Cookie") def tearDown(self): del self.new_monster @patch('builtins.input', return_value='Y') def test_flee_y_upper(self, input_value): """Test if True is returned if the user inputs 'Y'.""" self.assertEqual(self.new_monster.flee(), True, "Does not flee Tokyo when input is 'Y'.") @patch('builtins.input', return_value='y') def test_flee_y_lower(self, input_value): """Test if True is returned if the user inputs 'y'.""" self.assertEqual(self.new_monster.flee(), True, "Does not flee Tokyo when input is 'y'.") @patch('builtins.input', return_value='N') def test_flee_n_upper(self, input_value): """Test if False is returned if the user inputs 'N'.""" self.assertEqual(self.new_monster.flee(), False, "Does flee Tokyo when input is 'N'.") @patch('builtins.input', return_value='n') def test_flee_n_lower(self, input_value): """Test if True is returned if the user inputs 'n'.""" self.assertEqual(self.new_monster.flee(), False, "Does flee Tokyo when input is 'n'.") # Test that user is prompted again if they input anything other than a # single Y/y or N/n. Also check that the following message is displayed # following the invalid input: "Please enter only 'Y', 'y', 'N', or 'n'." @patch('builtins.input', side_effect=['2barf', 'y']) def test_flee_n_lower(self, input_value): """Simulate the user entering '2barf' as input first, then after being reprompted entering 'y'.Check that True is returned.""" self.assertEqual(self.new_monster.flee(), True, "Does not flee Tokyo when first valid input is 'y'.")
class MonsterFleeTest(unittest.TestCase): """Test Monster Class flee functions""" def setUp(self): print(self.shortDescription()) self.test_monster = Monster("The Boogie Monster") @patch('builtins.input', return_value='y') def test_want_to_flee_ideal_yes(self, input_patch): """tests prompt flee function, we want to flee""" self.assertTrue(self.test_monster.flee()) @patch('builtins.input', return_value='n') def test_want_to_flee_ideal_no(self, input_patch): """tests prompt flee function, we don't want to flee""" self.assertFalse(self.test_monster.flee()) @patch('builtins.input', return_value="yy") def test_want_to_flee_multiple_ideal_yes(self, input_patch): """tests prompt flee function, but we enter multiple ideal inputs to flee""" self.assertTrue(self.test_monster.flee()) @patch('builtins.input', return_value="nn") def test_want_to_flee_multiple_ideal_no(self, input_patch): """tests prompt flee function, but we enter multiple ideal inputs to flee""" self.assertFalse(self.test_monster.flee()) @patch('builtins.input', side_effect=["waka", "stuffs", "y"]) def test_want_to_flee_bad_yes(self, input_patch): """tests prompt flee function, but we enter multiple bad inputs to flee but do flee""" self.assertTrue(self.test_monster.flee()) @patch('builtins.input', side_effect=["sasha", "zipp", "n"]) def test_want_to_flee_bad_no(self, input_patch): """tests prompt flee function, but we enter multiple bad inputs to flee, but don't flee""" self.assertFalse(self.test_monster.flee())