how to handle Exceptions (Errors): tests and solutions
Exception Handling in tests
Exception Handling in tests: tests
The code in exceptions/tests/test_exceptions.py from how to test that an Exception is raised
1import src.exceptions
2import unittest
3
4
5class TestExceptions(unittest.TestCase):
6
7 @staticmethod
8 def assert_raises(code, exception):
9 try:
10 exec(code)
11 except exception:
12 pass
13 else:
14 raise AssertionError
15
16 def test_catching_module_not_found_error(self):
17 self.assert_raises(
18 'import does_not_exist', ModuleNotFoundError
19 )
20 with self.assertRaises(ModuleNotFoundError):
21 import does_not_exist
23 def test_catching_name_error(self):
24 self.assert_raises('not_defined', NameError)
25 with self.assertRaises(NameError):
26 not_defined
27
28 def test_catching_attribute_error(self):
29 self.assert_raises(
30 'src.exceptions.does_not_exist', AttributeError
31 )
32 with self.assertRaises(AttributeError):
33 src.exceptions.does_not_exist
34
35 def test_catching_type_error(self):
36 self.assert_raises(
37 "src.exceptions.function_name('the input')",
38 TypeError
39 )
40 with self.assertRaises(TypeError):
41 src.exceptions.function_name('the input')
43 def test_catching_index_error(self):
44 self.assert_raises("'a string'[8]", IndexError)
45 self.assert_raises("'a string'[-9]", IndexError)
46
47 with self.assertRaises(IndexError):
48 'a string'[8]
49 with self.assertRaises(IndexError):
50 'a string'[-9]
51
52 self.assert_raises(
53 "(0, 1, 2, 'n')[100]", IndexError
54 )
55 self.assert_raises(
56 "(0, 1, 2, 'n')[-100]", IndexError
57 )
58
59 with self.assertRaises(IndexError):
60 (0, 1, 2, 'n')[100]
61 with self.assertRaises(IndexError):
62 (0, 1, 2, 'n')[-100]
64 def test_catching_key_error(self):
65 self.assert_raises(
66 "{'key': 'value'}['not_in_dictionary']",
67 KeyError
68 )
69 with self.assertRaises(KeyError):
70 {'key': 'value'}['not_in_dictionary']
71
72 def test_catching_zero_division_error(self):
73 self.assert_raises('1 / 0', ZeroDivisionError)
74 with self.assertRaises(ZeroDivisionError):
75 1 / 0
76
77 def test_catching_exceptions(self):
78 self.assert_raises('raise Exception', Exception)
79 with self.assertRaises(Exception):
80 raise Exception
81
82
83# Exceptions seen
84# AssertionError
85# ModuleNotFoundError
86# NameError
87# AttributeError
88# TypeError
89# SyntaxError
90# IndexError
91# KeyError
92# ZeroDivisionError
Exception Handling in tests: solution
The code in src/exceptions/__init__.py from how to test that an Exception is raised
Exception Handling in programs tests and solutions
Exception Handling in programs: tests
The code in exceptions/tests/test_exceptions.py from how to handle Exceptions in programs
1import src.exceptions
2import unittest
3
4
5class TestExceptions(unittest.TestCase):
6
7 @staticmethod
8 def assert_raises(code, exception):
9 try:
10 exec(code)
11 except exception:
12 pass
13 else:
14 raise AssertionError(f'{exception} not raised')
15
16 def test_catching_module_not_found_error(self):
17 self.assert_raises(
18 'import does_not_exist', ModuleNotFoundError
19 )
20 with self.assertRaises(ModuleNotFoundError):
21 import does_not_exist
23 def test_catching_name_error(self):
24 self.assert_raises('not_defined', NameError)
25 with self.assertRaises(NameError):
26 not_defined
27
28 def test_catching_attribute_error(self):
29 self.assert_raises(
30 'src.exceptions.does_not_exist', AttributeError
31 )
32 with self.assertRaises(AttributeError):
33 src.exceptions.does_not_exist
34
35 def test_catching_type_error(self):
36 self.assert_raises(
37 "src.exceptions.function_name('the input')",
38 TypeError
39 )
40 with self.assertRaises(TypeError):
41 src.exceptions.function_name('the input')
43 def test_catching_index_error(self):
44 self.assert_raises("'a string'[8]", IndexError)
45 self.assert_raises("'a string'[-9]", IndexError)
46
47 with self.assertRaises(IndexError):
48 'a string'[8]
49 with self.assertRaises(IndexError):
50 'a string'[-9]
51
52 self.assert_raises(
53 "(0, 1, 2, 'n')[100]", IndexError
54 )
55 self.assert_raises(
56 "(0, 1, 2, 'n')[-100]", IndexError
57 )
58
59 with self.assertRaises(IndexError):
60 (0, 1, 2, 'n')[100]
61 with self.assertRaises(IndexError):
62 (0, 1, 2, 'n')[-100]
64 def test_catching_key_error(self):
65 self.assert_raises(
66 "{'key': 'value'}['not_in_dictionary']",
67 KeyError
68 )
69 with self.assertRaises(KeyError):
70 {'key': 'value'}['not_in_dictionary']
71
72 def test_catching_zero_division_error(self):
73 self.assert_raises('1 / 0', ZeroDivisionError)
74 with self.assertRaises(ZeroDivisionError):
75 1 / 0
76
77 def test_catching_exceptions(self):
78 self.assert_raises('raise Exception', Exception)
79 with self.assertRaises(Exception):
80 raise Exception
81
82 def test_catching_exceptions_w_messages(self):
83 with self.assertRaisesRegex(
84 Exception, 'BOOM!!!'
85 ):
86 src.exceptions.raise_exception()
88 def test_catching_failure(self):
89 self.assertEqual(
90 src.exceptions.an_exception_handler(
91 src.exceptions.raise_exception
92 ),
93 'failed'
94 )
95
96 def test_catching_success(self):
97 self.assertEqual(
98 src.exceptions.an_exception_handler(
99 src.exceptions.function_name
100 ),
101 'succeeded'
102 )
103
104
105# Exceptions seen
106# AssertionError
107# ModuleNotFoundError
108# NameError
109# AttributeError
110# TypeError
111# SyntaxError
112# IndexError
113# KeyError
114# ZeroDivisionError
Exception Handling in programs: solutions
The code in src/exceptions/__init__.py from how to handle Exceptions in programs