Skriver ut testfallets namn

This commit is contained in:
2024-07-13 00:51:05 +02:00
parent 549a3290cd
commit 15e964b52c
+18 -1
View File
@@ -31,9 +31,15 @@ class TestTowerOfHanoi(unittest.TestCase):
""" """
def test_valid_input(self): def test_valid_input(self):
print("=========================")
print("test_valid_input:")
self.assertEqual(tower_of_hanoi(3, 'A', 'B', 'C'), None) # Example with 3 disks 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
@@ -42,24 +48,35 @@ class TestTowerOfHanoi(unittest.TestCase):
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() unittest.main()