def test_main_menu_add(self, input): menu_driven = main.MenuDriven() menu_driven.add_screen = MagicMock() captured_output = io.StringIO() # Create StringIO object sys.stdout = captured_output # and redirect stdout. menu_driven.main_menu() self.assertEqual(captured_output.getvalue(), "1. Add\n2. Edit\n3. Delete\nq. Quit\n\n") sys.stdout = sys.__stdout__ # Reset redirect. assert menu_driven.add_screen.called
def test_main_menu_quit(self, input): menu_driven = main.MenuDriven() captured_output = io.StringIO() # Create StringIO object sys.stdout = captured_output # and redirect stdout. with self.assertRaises(SystemExit) as cm: menu_driven.main_menu() self.assertEqual(cm.exception.code, 1) self.assertEqual(captured_output.getvalue(), "1. Add\n2. Edit\n3. Delete\nq. Quit\n\n") sys.stdout = sys.__stdout__ # Reset redirect.
def test_delete_screen_q(self, mock_get_input): menu_driven = main.MenuDriven() menu_driven.main_menu = MagicMock() menu_driven.book.student_list = self.student_list mock_get_input.side_effect = ['q'] captured_output = io.StringIO() # Create StringIO object sys.stdout = captured_output # and redirect stdout. menu_driven.delete_screen() sys.stdout = sys.__stdout__ # Reset redirect. self.assertTrue(menu_driven.main_menu.called)
def test_enter_details(self, mock_get_input): menu_driven = main.MenuDriven() mock_get_input.side_effect = [ "Nihal", "9876543210", '94.3', '99.6', '84.5' ] captured_output = io.StringIO() # Create StringIO object sys.stdout = captured_output # and redirect stdout. student = menu_driven.enter_details() sys.stdout = sys.__stdout__ # Reset redirect. self.assertEqual(student.name, "Nihal") self.assertEqual(student.phone_number, "9876543210") self.assertEqual(student.marks, [94.3, 99.6, 84.5])
def test_delete_screen(self, mock_get_input): menu_driven = main.MenuDriven() menu_driven.main_menu = MagicMock() path = "test_{}.txt".format(random.randint(1000, 9999)) menu_driven.book.path = path menu_driven.book.student_list = self.student_list mock_get_input.side_effect = ['no match', 'S1'] captured_output = io.StringIO() # Create StringIO object sys.stdout = captured_output # and redirect stdout. menu_driven.delete_screen() sys.stdout = sys.__stdout__ # Reset redirect. self.assertEqual(len(menu_driven.book.student_list), 2) self.assertTrue(menu_driven.main_menu.called) os.remove(path)
def test_add_screen(self, mock_get_input): menu_driven = main.MenuDriven() path = "test_{}.txt".format(random.randint(1000, 9999)) menu_driven.book.path = path menu_driven.main_menu = MagicMock() mock_get_input.return_value = self.student captured_output = io.StringIO() # Create StringIO object sys.stdout = captured_output # and redirect stdout. menu_driven.add_screen() sys.stdout = sys.__stdout__ # Reset redirect. self.assertEqual(self.student.name, menu_driven.book.student_list[-1].name) self.assertEqual(self.student.phone_number, menu_driven.book.student_list[-1].phone_number) self.assertEqual(self.student.marks, menu_driven.book.student_list[-1].marks) self.assertTrue(menu_driven.main_menu.called) os.remove(path)
def test_edit_screen(self, mock_get_input): menu_driven = main.MenuDriven() path = "test_{}.txt".format(random.randint(1000, 9999)) menu_driven.book.path = path menu_driven.main_menu = MagicMock() menu_driven.book.student_list = self.student_list mock_get_input.side_effect = [ 'no match', 'S1', 'Snew', '1234567890', '97', '98', '99' ] captured_output = io.StringIO() # Create StringIO object sys.stdout = captured_output # and redirect stdout. menu_driven.edit_screen() sys.stdout = sys.__stdout__ # Reset redirect. self.assertEqual('Snew', menu_driven.book.student_list[-1].name) self.assertEqual('1234567890', menu_driven.book.student_list[-1].phone_number) self.assertEqual([97, 98, 99], menu_driven.book.student_list[-1].marks) self.assertTrue(menu_driven.main_menu.called) os.remove(path)