1from unittest.mock import patch, mock_open
2from bangaranga import does_the_banga_rang, TheBangaDoesNotRangError
3import unittest
4
5class TestBangaranga(unittest.TestCase):
6
7 def setUp(self):
8 self.filename = "coolfile.txt"
9
10 def tearDown(self):
11 pass
12
13 def test_bang_the_ranga_normal_string(self):
14 with patch("builtins.open", mock_open(read_data="banga the ranga")):
15 result = does_the_banga_rang(self.filename)
16 self.assertEqual(result, 2)
17
18 def test_bang_the_ranga_random__character_case(self):
19 with patch("builtins.open", mock_open(read_data="bANG A the RAnga")):
20 result = does_the_banga_rang(self.filename)
21 self.assertEqual(result, 3)
22
23 def test_bang_the_ranga_does_find_shortest_case(self):
24 with patch("builtins.open", mock_open(read_data="bang a banga ranga bangaranga")):
25 result = does_the_banga_rang(self.filename)
26 self.assertEqual(result, 1)
27
28 def test_bang_the_ranga_reversed(self):
29 with patch("builtins.open", mock_open(read_data="ranga the banga")):
30 result = does_the_banga_rang(self.filename)
31 self.assertEqual(result, 0)
32
33 def test_bang_the_ranga_no_bangaranga(self):
34 with patch("builtins.open", mock_open(read_data="What an interesting string carrying very useful information")):
35 result = does_the_banga_rang(self.filename)
36 self.assertEqual(result, 0)
37
38 def test_bang_the_ranga_bangaranga_but_part_of_other_words(self):
39 with patch("builtins.open", mock_open(read_data="bangaran garanga")):
40 result = does_the_banga_rang(self.filename)
41 self.assertEqual(result, 0)
42
43 def test_bang_the_ranga_OSError(self):
44 with patch("builtins.open") as mock_file:
45 mock_file.side_effect = OSError("An unfortunate thing to happen")
46
47 with self.assertRaises(TheBangaDoesNotRangError):
48 does_the_banga_rang(self.filename)
49
50 def test_bang_the_ranga_IOError(self):
51 with patch("builtins.open") as mock_file:
52 mock_file.side_effect = IOError(":(")
53
54 with self.assertRaises(TheBangaDoesNotRangError):
55 does_the_banga_rang(self.filename)
56
57if __name__ == "__main__":
58 unittest.main()
....F.....
======================================================================
FAIL: test_student_tests_catch_bad_implementation_matches_partial_words (test.StudentTestSuiteEvaluationTests.test_student_tests_catch_bad_implementation_matches_partial_words)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 98, in test
self.assertTrue(
~~~~~~~~~~~~~~~^
bad_result["failed_test_ids"],
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
f"Student tests passed against incorrect implementation: {implementation_name!r}.\n{format_student_output(bad_result['output'])}",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
AssertionError: set() is not true : Student tests passed against incorrect implementation: 'bad_implementation_matches_partial_words'.
| Output:
| ----------------------------------------------------------------------
| Ran 8 tests in 0.011s
|
| OK
----------------------------------------------------------------------
Ran 10 tests in 0.199s
FAILED (failures=1)
31.05.2026 18:20
31.05.2026 18:19
31.05.2026 18:20