how to make a calculator 2
I want to add an exception handler to the calculator project for ZeroDivisionError_ in test_division
preview
Here are the tests I have by the end of the chapter
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 self.assertEqual(
44 src.calculator.divide(
45 self.random_first_number,
46 self.random_second_number
47 ),
48 self.random_first_number/self.random_second_number
49 )
50 with self.assertRaises(ZeroDivisionError):
51 src.calculator.divide(self.random_first_number, 0)
52
53
54# Exceptions seen
55# AssertionError
56# NameError
57# AttributeError
58# TypeError
open the project
I change directory to the
calculatorfoldercd calculatorthe terminal shows I am in the
calculatorfolder.../pumping_python/calculatorI activate the virtual environment
source .venv/bin/activateAttention
on Windows without Windows Subsystem for Linux use
.venv/bin/activate.ps1NOTsource .venv/bin/activate.venv/scripts/activate.ps1the terminal shows
(.venv) .../pumping_python/calculatorI use
pytest-watchto run the testspytest-watchthe terminal shows
rootdir: .../pumping_python/calculator collected 4 items tests/test_calculator.py .... [100%] ============================ 4 passed in X.YZs =============================I hold ctrl on the keyboard and click on
tests/test_calculator.pyto open it in the editor
test catching ZeroDivisionError in test_calculator.py
RED: make it fail
I add a new assertion to show that the divide function raises ZeroDivisionError_ in
42 def test_division(self):
43 self.assertEqual(
44 src.calculator.divide(
45 self.random_first_number,
46 self.random_second_number
47 ),
48 self.random_first_number/self.random_second_number
49 )
50 src.calculator.divide(self.random_first_number, 0)
51
52
53# Exceptions seen
the terminal shows ZeroDivisionError_
ZeroDivisionError: float division by zero
GREEN: make it pass
I add assertRaises
48 self.random_first_number/self.random_second_number
49 )
50 with self.assertRaises(ZeroDivisionError):
51 src.calculator.divide(self.random_first_number, 0)
52
53
54# Exceptions seen
the test passes
close the project
I close
test_calculator.pyin the editorI click in the terminal and exit the tests with ctrl+c on the keyboard, the terminal shows
(.venv) .../pumping_python/calculatorI deactivate the virtual environment
deactivatethe terminal goes back to the command line,
(.venv)is no longer on the left side.../pumping_python/calculatorI change directory to the parent of
calculatorcd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory
review
I can use assertRaises to catch Exceptions in tests and tested the following
How many questions can you answer after going through this chapter?
code from the chapter
what is next?
you know
rate pumping python
If this has been a 7 star experience for you, please leave a 5 star review. It helps other people get into the book too