Tydliggjort vilket testfall som körs och resultatet av just detta testfall

This commit is contained in:
2024-07-13 11:00:40 +02:00
parent 15e964b52c
commit 4b937c6dac
+8 -22
View File
@@ -31,52 +31,38 @@ class TestTowerOfHanoi(unittest.TestCase):
""" """
def test_valid_input(self): def test_valid_input(self):
print("=========================") self.assertEqual(tower_of_hanoi(3, 'A', 'B', 'C'), None)
print("test_valid_input:")
self.assertEqual(tower_of_hanoi(3, 'A', 'B', 'C'), None) # Example with 3 disks
print("=========================\n")
def test_edge_case_single_disk(self): def test_edge_case_single_disk(self):
print("=========================")
print("test_edge_case_single_disk:")
print("=========================\n")
expected_output = "Move disk 1 from A to C\n" expected_output = "Move disk 1 from A to C\n"
captured_output = io.StringIO() captured_output = io.StringIO()
sys.stdout = captured_output sys.stdout = captured_output
tower_of_hanoi(1, 'A', 'B', 'C') tower_of_hanoi(1, 'A', 'B', 'C')
sys.stdout = sys.__stdout__ # Reset stdout sys.stdout = sys.__stdout__ # Reset stdout
self.assertEqual(captured_output.getvalue(), expected_output) self.assertEqual(captured_output.getvalue(), expected_output)
def test_invalid_input_zero_disks(self): def test_invalid_input_zero_disks(self):
print("=========================")
print("test_invalid_input_zero_disks:")
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
tower_of_hanoi(0, 'A', 'B', 'C') tower_of_hanoi(0, 'A', 'B', 'C')
print("=========================\n")
def test_prompt_for_integer_valid(self): def test_prompt_for_integer_valid(self):
print("=========================")
print("test_prompt_for_integer_valid:")
with io.StringIO("3\n") as mock_stdin: with io.StringIO("3\n") as mock_stdin:
sys.stdin = mock_stdin sys.stdin = mock_stdin
self.assertEqual(prompt_for_integer(), 3) self.assertEqual(prompt_for_integer(), 3)
print("\n=========================\n")
def test_prompt_for_integer_invalid_negative(self): def test_prompt_for_integer_invalid_negative(self):
print("=========================")
print("test_prompt_for_integer_invalid_negative:")
with io.StringIO("-2\n1\n") as mock_stdin: with io.StringIO("-2\n1\n") as mock_stdin:
sys.stdin = mock_stdin sys.stdin = mock_stdin
self.assertEqual(prompt_for_integer(), 1) # Should eventually get a valid integer self.assertEqual(prompt_for_integer(), 1) # Should eventually get a valid integer
print("\n=========================\n")
def test_prompt_for_integer_invalid_string(self): def test_prompt_for_integer_invalid_string(self):
print("=========================")
print("test_prompt_for_integer_invalid_string:")
with io.StringIO("abc\n3\n") as mock_stdin: with io.StringIO("abc\n3\n") as mock_stdin:
sys.stdin = mock_stdin sys.stdin = mock_stdin
self.assertEqual(prompt_for_integer(), 3) # Should eventually get a valid integer self.assertEqual(prompt_for_integer(), 3) # Should eventually get a valid integer
print("\n=========================\n")
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() runner = unittest.TextTestRunner(verbosity=2) # Set verbosity to 2 for more details
unittest.main(testRunner=runner)