how to handle Exceptions (Errors): tests and solutions
Exception Handling tests
the code in exceptions/tests/test_exceptions.py from how to handle Exceptions (Errors) in programs
1import src.exceptions
2import unittest
3
4
5class TestExceptions(unittest.TestCase):
6
7 def test_catching_module_not_found_error_in_tests(self):
8 with self.assertRaises(ModuleNotFoundError):
9 import does_not_exist
10
11 def test_catching_name_error_in_tests(self):
12 with self.assertRaises(NameError):
13 does_not_exist
14
15 def test_catching_attribute_error_in_tests(self):
16 with self.assertRaises(AttributeError):
17 src.exceptions.does_not_exist
18
19 def test_catching_type_error_in_tests(self):
20 with self.assertRaises(TypeError):
21 src.exceptions.function_name('the input')
22
23 def test_catching_index_error_in_tests(self):
24 a_list = [1, 2, 3, 'n']
25 with self.assertRaises(IndexError):
26 a_list[4]
27 with self.assertRaises(IndexError):
28 a_list[-5]
29
30 def test_catching_key_error_in_tests(self):
31 with self.assertRaises(KeyError):
32 {'key': 'value'}['does_not_exist']
33
34 def test_catching_zero_division_error_in_tests(self):
35 with self.assertRaises(ZeroDivisionError):
36 1 / 0
37
38 def test_catching_exceptions_in_tests(self):
39 with self.assertRaises(Exception):
40 raise Exception
41
42 def test_catching_exceptions_w_messages(self):
43 with self.assertRaisesRegex(
44 Exception, 'BOOM!!!'
45 ):
46 src.exceptions.raise_exception()
47
48 def test_catching_failure(self):
49 self.assertEqual(
50 src.exceptions.an_exception_handler(
51 src.exceptions.raise_exception
52 ),
53 'failed'
54 )
55
56 def test_catching_success(self):
57 self.assertEqual(
58 src.exceptions.an_exception_handler(
59 src.exceptions.does_not_raise_exception
60 ),
61 'succeeded'
62 )
63
64
65# Exceptions seen
66# AssertionError
67# ModuleNotFoundError
68# NameError
69# AttributeError
70# TypeError
71# IndexError
72# KeyError
73# ZeroDivisionError
Exception Handling solutions
the solutions in exceptions/src/exceptions.py from how to handle Exceptions (Errors) in programs
1def function_name():
2 return None
3
4
5def raise_exception():
6 raise Exception('BOOM!!!')
7
8
9def does_not_raise_exception():
10 return None
11
12
13def an_exception_handler(a_function):
14 try:
15 a_function()
16 except Exception:
17 return 'failed'
18 else:
19 return 'succeeded'
Exception Handling with the Calculator tests
the code in calculator/tests/test_calculator.py from how to handle Exceptions (Errors) in programs
1import random
2import src.calculator
3import unittest
4
5
6def a_random_number():
7 return random.triangular(-1000.0, 1000.0)
8
9
10class TestCalculator(unittest.TestCase):
11
12 random_first_number = a_random_number()
13 random_second_number = a_random_number()
14
15 def test_addition(self):
16 self.assertEqual(
17 src.calculator.add(
18 self.random_first_number,
19 self.random_second_number
20 ),
21 self.random_first_number+self.random_second_number
22 )
23
24 def test_subtraction(self):
25 self.assertEqual(
26 src.calculator.subtract(
27 self.random_first_number,
28 self.random_second_number
29 ),
30 self.random_first_number-self.random_second_number
31 )
32
33 def test_multiplication(self):
34 self.assertEqual(
35 src.calculator.multiply(
36 self.random_first_number,
37 self.random_second_number
38 ),
39 self.random_first_number*self.random_second_number
40 )
41
42 def test_division(self):
43 try:
44 self.assertEqual(
45 src.calculator.divide(
46 self.random_first_number,
47 self.random_second_number
48 ),
49 self.random_first_number/self.random_second_number
50 )
51 except ZeroDivisionError:
52 self.assertEqual(
53 src.calculator.divide(self.random_first_number, 0),
54 'brmph?! cannot divide by 0. Try again...'
55 )
56
57
58# Exceptions seen
59# AssertionError
60# NameError
61# AttributeError
62# TypeError
63# ZeroDivisionError
Exception Handling with the Calculator solutions
the solutions in calculator/src/calculator.py from how to handle Exceptions (Errors) in programs
1def subtract(first_input, second_input):
2 return first_input - second_input
3
4
5def multiply(first_input, second_input):
6 return first_input * second_input
7
8
9def divide(first_input, second_input):
10 try:
11 return first_input / second_input
12 except ZeroDivisionError:
13 return 'brmph?! cannot divide by 0. Try again...'
14
15
16def add(first_input, second_input):
17 return first_input + second_input