Microwave
I want to make a Microwave that HEATINGs up food or stays off, if the inputs are
is the door open?
has the start button been pushed?
this is the truth table I get
door |
start button |
output |
|---|---|---|
open |
pushed |
OFF |
open |
NOT pushed |
OFF |
closed |
pushed |
HEATING |
closed |
NOT pushed |
OFF |
preview
These are the tests I have at the end of the chapter
1
requirements
start the project
I name this project
microwaveI open a terminal
I make a directory for the project
mkdir microwavethe terminal goes back to the command line
I change directory to the project
cd microwavethe terminal is my friend, and shows I am in the
microwavefolder.../pumping_python/microwaveI make a directory for the source code
mkdir srcthe terminal goes back to the command line
I make a Python file to hold the source code in the
srcdirectorytouch src/microwave.pyNew-Item src/microwave.pythe terminal goes back to the command line
I make a directory for the tests
mkdir teststhe terminal goes back to the command line
I make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line
I make a Python file for the tests in the
testsdirectorytouch tests/test_microwave.pyNew-Item tests/test_microwave.pythe terminal goes back to the command line
I open
test_microwave.pyin the editor of the Integrated Development Environment (IDE)Tip
I can open a file from the terminal in the Integrated Development Environment (IDE) with the name of the program and the name of the file. That means if I type this in the terminal
code tests/test_microwave.pyVisual Studio Code opens
test_microwave.pyin the editorI add the first failing test to
test_microwave.py1import unittest 2 3 4class TestMicrowave(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(True)I make a requirements file for the Python packages I need, in the terminal
echo "pytest" > requirements.txtthe terminal goes back to the command line
I add pytest-watcher to the file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line
I set up the project with uv
uv initthe terminal shows
Initialized project `microwave`then goes back to the command line
I remove
main.pyfrom the project because I do not use itrm main.pythe terminal goes back to the command line
I install the Python packages that I wrote in the requirements file
uv add --requirement requirements.txtthe terminal shows that it installed the Python packages
I use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows
================================ FAILURES ================================ ______________________ TestMicrowave.test_failure ________________________ self = <tests.test_microwave.TestMicrowave testMethod=test_failure> def test_failure(self): > self.assertFalse(True) E AssertionError: True is not false tests/test_microwave.py:7: AssertionError ======================== short test summary info ========================= FAILED tests/test_microwave.py::TestMicrowave::test_failure - AssertionError: True is not false =========================== 1 failed in X.YZs ============================I add AssertionError to the list of Exceptions seen in
test_microwave.py4class TestMicrowave(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(True) 8 9 10# Exceptions seen 11# AssertionErrorthen I change True to False in the assertion
7 self.assertFalse(False)the test passes
test_door_open
RED: make it fail
I change test_failure to test_door_open, then add an assertion for when the door is open and the start button is pushed amount in the account is enough
door |
start button |
output |
|---|---|---|
open |
start |
OFF |
4class TestMicrowave(unittest.TestCase):
5
6 def test_door_open(self):
7 my_expectation = 'OFF'
8
9 reality = src.microwave.microwave(
10 door_is_open=True,
11 start_is_pushed=True,
12 )
13 self.assertEqual(reality, my_expectation)
14
15
16# Exceptions seen
17# AssertionError
the terminal is my friend, and shows NameError
NameError: name 'src' is not defined
GREEN: make it pass
I add NameError to the list of Exceptions seen
15# Exceptions seen 16# AssertionError 17# NameErrorI add an import statement at the top of the file
1import src.microwave 2import unittest 3 4 5class TestMicrowave(unittest.TestCase):the terminal is my friend, and shows AttributeError
AttributeError: module 'src.microwave' has no attribute 'microwave'I add AttributeError to the list of Exceptions seen
16# Exceptions seen 17# AssertionError 18# NameError 19# AttributeErrorI add a function named
microwavetomicrowave.py1def microwave(): 2 return Nonethe terminal is my friend, and shows TypeError
TypeError: microwave() got an unexpected keyword argument 'door_is_open'I add TypeError to the list of Exceptions seen in
test_microwave.py16# Exceptions seen 17# AssertionError 18# NameError 19# AttributeError 20# TypeErrorI add the keyword argument to the function in
microwave.py1def microwave(door_is_open): 2 return Nonethe terminal is my friend, and shows TypeError
TypeError: microwave() got an unexpected keyword argument 'start_is_pushed'I add
start_is_pushedto the function signature1def microwave(door_is_open, start_is_pushed): 2 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: None != 'OFF'I change the return statement to give the test what it wants
1def microwave(door_is_open, start_is_pushed): 2 return 'OFF'the test passes. The
microwavefunction always returns OFF, it does not care about the inputs. Is this Contradiction?
REFACTOR: make it better
I add an assertion for when the door is open and the start button is NOT pushed, in
test_microwave.pydoor
start button
output
open
pushed
OFF
open
NOT pushed
OFF
7 def test_door_open(self): 8 my_expectation = 'OFF' 9 10 reality = src.microwave.microwave( 11 door_is_open=True, 12 start_is_pushed=True, 13 ) 14 self.assertEqual(reality, my_expectation) 15 16 reality = src.microwave.microwave( 17 door_is_open=True, 18 start_is_pushed=False, 19 ) 20 self.assertEqual(reality, my_expectation) 21 22 23# Exceptions seenthe test is still green
test_door_closed
RED: make it fail
I add a test with an assertion for when the door is closed and the start button is pushed
door |
start button |
output |
|---|---|---|
closed |
pushed |
HEATING |
16 reality = src.microwave.microwave(
17 door_is_open=True,
18 start_is_pushed=False,
19 )
20 self.assertEqual(reality, my_expectation)
21
22 def test_door_closed(self):
23 my_expectation = 'HEATING'
24 reality = src.microwave.microwave(
25 door_is_open=False,
26 start_is_pushed=True,
27 )
28 self.assertEqual(reality, my_expectation)
29
30
31 # Exceptions seen
the terminal is my friend, and shows AssertionError
AssertionError: 'OFF' != 'HEATING'
GREEN: make it pass
I add an if statement to microwave.py
1def microwave(door_is_open, start_is_pushed):
2 if not door_is_open and start_is_pushed:
3 return 'HEATING'
4 return 'OFF'
the test passes. Is this Converse NonImplication?
REFACTOR: make it better
I add an assertion for when the door is closed and the start button is NOT pushed to test_door_closed in
test_microwave.pydoor
start button
output
closed
pushed
HEATING
closed
NOT pushed
OFF
22 def test_door_closed(self): 23 my_expectation = 'HEATING' 24 reality = src.microwave.microwave( 25 door_is_open=False, 26 start_is_pushed=True, 27 ) 28 self.assertEqual(reality, my_expectation) 29 30 my_expectation = 'OFF' 31 32 reality = src.microwave.microwave( 33 door_is_open=False, 34 start_is_pushed=False, 35 ) 36 self.assertEqual(reality, my_expectation) 37 38 39# Exceptions seenthe test is still green
test_door_open_and_timer_set
So far, the truth table for the Microwave is
door |
start button |
output |
|---|---|---|
open |
pushed |
OFF |
open |
NOT pushed |
OFF |
closed |
pushed |
HEATING |
closed |
NOT pushed |
OFF |
I want it to only HEATING up food when the timer is set, the inputs for the microwave will then be
is the door open?
is the timer set?
has the start button been pushed?
and the truth table will be
door |
timer |
start button |
output |
|---|---|---|---|
open |
set |
pushed |
OFF |
open |
set |
NOT pushed |
OFF |
open |
NOT set |
pushed |
OFF |
open |
NOT set |
NOT pushed |
OFF |
door |
timer |
start button |
output |
|---|---|---|---|
closed |
set |
pushed |
HEATING |
closed |
set |
NOT pushed |
OFF |
closed |
NOT set |
pushed |
OFF |
closed |
NOT set |
NOT pushed |
OFF |
RED: make it fail
I add a value for timer_is_set to the first assertion in test_door_open, for when the door is open, the timer is set and the start button is pushed
door |
timer |
start button |
output |
|---|---|---|---|
open |
set |
pushed |
OFF |
7 def test_door_open(self):
8 my_expectation = 'OFF'
9
10 reality = src.microwave.microwave(
11 door_is_open=True,
12 timer_is_set=True,
13 start_is_pushed=True,
14 )
15 self.assertEqual(reality, my_expectation)
the terminal is my friend, and shows TypeError
TypeError: microwave() got an unexpected keyword argument 'timer_is_set'
GREEN: make it pass
I add
timer_is_setto the function signature inmicrowave.py1def microwave( 2 door_is_open, start_is_pushed, 3 timer_is_set, 4 ): 5 if not door_is_open and start_is_pushed: 6 return 'HEATING' 7 return 'OFF'FAILED ...test_microwave_w - TypeError: microwave() missing 1 required positional argument:... FAILED ...test_door_open - TypeError: microwave() missing 1 required positional argument:...because the other assertions call the
microwavefunction with 2 arguments and I just changed the function signature to make it take 3 required argumentsI add a default value to make
timer_is_seta choice1def microwave( 2 door_is_open, start_is_pushed, 3 timer_is_set=False, 4 ):the test passes because this
src.microwave.microwave( door_is_open=True, start_is_pushed=False, )is now the same as
src.microwave.microwave( door_is_open=True, start_is_pushed=False, timer_is_set=False, )because
timer_is_sethas a default value
REFACTOR: make it better
I add a value for
timer_is_setto the next assertion for when the door is open, the timer is set and the start button is NOT pusheddoor
timer
start button
output
open
set
pushed
OFF
open
set
NOT pushed
OFF
7 def test_door_open(self): 8 my_expectation = 'OFF' 9 10 reality = src.microwave.microwave( 11 door_is_open=True, 12 timer_is_set=True, 13 start_is_pushed=True, 14 ) 15 self.assertEqual(reality, my_expectation) 16 17 reality = src.microwave.microwave( 18 door_is_open=True, 19 timer_is_set=True, 20 start_is_pushed=False, 21 ) 22 self.assertEqual(reality, my_expectation) 23 24 def test_microwave_w(self):the test is still green
I change the name of the test from test_door_open to test_door_open_and_timer_set
5class TestMicrowave(unittest.TestCase): 6 7 def test_door_open_and_timer_set(self): 8 my_expectation = 'OFF'
test_door_open_and_timer_not_set
I add a new test with an assertion for when the door is open, the timer is NOT set and the start button is pushed
door
timer
start button
output
open
NOT set
pushed
OFF
17 reality = src.microwave.microwave( 18 door_is_open=True, 19 timer_is_set=True, 20 start_is_pushed=False, 21 ) 22 self.assertEqual(reality, my_expectation) 23 24 def test_door_open_and_timer_not_set(self): 25 my_expectation = 'OFF' 26 27 reality = src.microwave.microwave( 28 door_is_open=True, 29 timer_is_set=False, 30 start_is_pushed=True, 31 ) 32 self.assertEqual(reality, my_expectation) 33 34 def test_door_closed(self):still green
I add an assertion for when the door is open, the timer is NOT set and the start button is NOT pushed
door
timer
start button
output
open
NOT set
pushed
OFF
open
NOT set
NOT pushed
OFF
24 def test_door_open_and_timer_not_set(self): 25 my_expectation = 'OFF' 26 27 reality = src.microwave.microwave( 28 door_is_open=True, 29 timer_is_set=False, 30 start_is_pushed=True, 31 ) 32 self.assertEqual(reality, my_expectation) 33 34 reality = src.microwave.microwave( 35 door_is_open=True, 36 timer_is_set=False, 37 start_is_pushed=False, 38 ) 39 self.assertEqual(reality, my_expectation) 40 41 def test_door_closed(self):green
test_door_closed_and_timer_set
I add a value for the
timer_is_setparameter to the first assertion in test_door_closed for the case where the door is closed, the timer is set and the start button is pusheddoor
timer
start button
output
closed
set
pushed
HEATING
41 def test_door_closed(self): 42 my_expectation = 'HEATING' 43 reality = src.microwave.microwave( 44 door_is_open=False, 45 timer_is_set=True, 46 start_is_pushed=True, 47 ) 48 self.assertEqual(reality, my_expectation) 49 50 my_expectation = 'OFF' 51 52 reality = src.microwave.microwave( 53 door_is_open=False, 54 start_is_pushed=False, 55 ) 56 self.assertEqual(reality, my_expectation)the test is still green
I do not need to add a value for
timer_is_setto the next assertion for when the door is closed, the timer is set and the start button is NOT pusheddoor
timer
start button
output
closed
set
pushed
HEATING
closed
set
NOT pushed
OFF
because
src.microwave.microwave( door_is_open=False, start_is_pushed=False, )is the same as
src.microwave.microwave( door_is_open=False, start_is_pushed=False, timer_is_set=False, )the default value for
timer_is_setis FalseI change the name of the test from test_door_closed to test_door_closed_and_timer_set
34 reality = src.microwave.microwave( 35 door_is_open=True, 36 timer_is_set=False, 37 start_is_pushed=False, 38 ) 39 self.assertEqual(reality, my_expectation) 40 41 def test_door_closed_and_timer_set(self): 42 my_expectation = 'HEATING' 43 reality = src.microwave.microwave( 44 door_is_open=False, 45 timer_is_set=True, 46 start_is_pushed=True, 47 ) 48 self.assertEqual(reality, my_expectation)
test_door_closed_and_timer_not_set
RED: make it fail
I add a new test with an assertion for when the door is closed, the timer is NOT set and the start button is pushed
door
timer
start button
output
closed
NOT set
pushed
OFF
52 reality = src.microwave.microwave( 53 door_is_open=False, 54 start_is_pushed=False, 55 ) 56 self.assertEqual(reality, my_expectation) 57 58 def test_door_closed_and_timer_not_set(self): 59 my_expectation = 'OFF' 60 61 reality = src.microwave.microwave( 62 door_is_open=False, 63 timer_is_set=False, 64 start_is_pushed=True, 65 ) 66 self.assertEqual(reality, my_expectation) 67 68 69# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: 'HEATING' != 'OFF'
GREEN: make it pass
I add a if statement to the microwave function in microwave.py
1def microwave(
2 door_is_open, start_is_pushed,
3 timer_is_set=False,
4 ):
5 if not timer_is_set:
6 return 'OFF'
7 if not door_is_open and start_is_pushed:
8 return 'HEATING'
9 return 'OFF'
the test passes. The Microwave returns
OFF if the timer is NOT set
HEATING if the door is closed and the start button is pushed, which only happens if the timer is set
OFF if none of the conditions are met
REFACTOR: make it better
I add an assertion for when the door is closed, the timer is NOT set and the start button is NOT pushed
58 def test_door_closed_and_timer_not_set(self): 59 my_expectation = 'OFF' 60 61 reality = src.microwave.microwave( 62 door_is_open=False, 63 timer_is_set=False, 64 start_is_pushed=True, 65 ) 66 self.assertEqual(reality, my_expectation) 67 68 reality = src.microwave.microwave( 69 door_is_open=False, 70 timer_is_set=False, 71 start_is_pushed=False, 72 ) 73 self.assertEqual(reality, my_expectation) 74 75 76# Exceptions seenthe test is still green
I add another clause to the if statement for when the timer is set, in the
microwavefunction inmicrowave.py1def microwave( 2 door_is_open, start_is_pushed, 3 timer_is_set=False, 4 ): 5 if not timer_is_set: 6 return 'OFF' 7 if not door_is_open and start_is_pushed and timer_is_set: 8 return 'HEATING' 9 return 'OFF'the test is still green
I remove the if statement for when the timer is NOT set because I do not need it anymore
1def microwave( 2 door_is_open, start_is_pushed, 3 timer_is_set=False, 4 ): 5 if not door_is_open and start_is_pushed and timer_is_set: 6 return 'HEATING' 7 return 'OFF'still green. The Microwave returns
'HEATING'if the door is closed, the timer is set and the start button is pushed'OFF'in every other case
test_door_open_timer_set_w_overheating
the truth table for the Microwave is
door |
timer |
start button |
output |
|---|---|---|---|
open |
set |
pushed |
OFF |
open |
set |
NOT pushed |
OFF |
open |
NOT set |
pushed |
OFF |
open |
NOT set |
NOT pushed |
OFF |
door |
timer |
start button |
output |
|---|---|---|---|
closed |
set |
pushed |
HEATING |
closed |
set |
NOT pushed |
OFF |
closed |
NOT set |
pushed |
OFF |
closed |
NOT set |
NOT pushed |
OFF |
I want to add a failsafe to stop the Microwave if it gets too hot. The inputs will then be
is the door open?
is the timer set?
has the start button been pushed?
is the microwave too hot?
and the truth table will be
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
set |
pushed |
too hot |
OFF |
open |
set |
pushed |
NOT too hot |
OFF |
open |
set |
NOT pushed |
too hot |
OFF |
open |
set |
NOT pushed |
NOT too hot |
OFF |
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
NOT set |
pushed |
too hot |
OFF |
open |
NOT set |
pushed |
NOT too hot |
OFF |
open |
NOT set |
NOT pushed |
too hot |
OFF |
open |
NOT set |
NOT pushed |
NOT too hot |
OFF |
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
closed |
set |
pushed |
too hot |
OFF |
closed |
set |
pushed |
NOT too hot |
HEATING |
closed |
set |
NOT pushed |
too hot |
OFF |
closed |
set |
NOT pushed |
NOT too hot |
OFF |
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
closed |
NOT set |
pushed |
too hot |
OFF |
closed |
NOT set |
pushed |
NOT too hot |
OFF |
closed |
NOT set |
NOT pushed |
too hot |
OFF |
closed |
NOT set |
NOT pushed |
NOT too hot |
OFF |
RED: make it fail
test_door_open_timer_set_and_overheating
I add a value for overheating to the assertion for the case where the door is open, the timer is set, the start button is pushed and the microwave temperature is too hot, to test_door_open_and_timer_set in test_microwave.py
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
set |
pushed |
too hot |
OFF |
7 def test_door_open_and_timer_set(self):
8 my_expectation = 'OFF'
9
10 reality = src.microwave.microwave(
11 door_is_open=True,
12 timer_is_set=True,
13 start_is_pushed=True,
14 overheating=True,
15 )
16 self.assertEqual(reality, my_expectation)
the terminal shows TypeError
TypeError: microwave() got an unexpected keyword argument 'overheating'
GREEN: make it pass
I add
overheatingto themicrowavefunction inmicrowave.py1def microwave( 2 door_is_open, start_is_pushed, 3 timer_is_set=False, overheating, 4 ): 5 if not door_is_open and start_is_pushed and timer_is_set: 6 return 'HEATING' 7 return 'OFF'the terminal shows SyntaxError
SyntaxError: parameter without a default follows parameter with a defaultI add a default value for the
overheatingparameter in the function signature to. make it a choice1def microwave( 2 door_is_open, start_is_pushed, 3 timer_is_set=False, overheating=False, 4 ):the test passes
REFACTOR: make it better
I add an assertion for when the door is open, the timer is set, the start button is pushed and the microwave temperature is NOT too hot
door
timer
start button
too hot
output
open
set
pushed
too hot
OFF
open
set
pushed
NOT too hot
OFF
7 def test_door_open_and_timer_set(self): 8 my_expectation = 'OFF' 9 10 reality = src.microwave.microwave( 11 door_is_open=True, 12 timer_is_set=True, 13 start_is_pushed=True, 14 overheating=True, 15 ) 16 self.assertEqual(reality, my_expectation) 17 18 reality = src.microwave.microwave( 19 door_is_open=True, 20 timer_is_set=True, 21 start_is_pushed=True, 22 overheating=False, 23 ) 24 self.assertEqual(reality, my_expectation) 25 26 reality = src.microwave.microwave( 27 door_is_open=True, 28 timer_is_set=True, 29 start_is_pushed=False, 30 ) 31 self.assertEqual(reality, my_expectation)the test is still green
I add a value for the
overheatingparameter in the next assertion for when the door is open, the timer is set, the start button is NOT pushed and the microwave temperature is too hotdoor
timer
start button
too hot
output
open
set
pushed
too hot
OFF
open
set
pushed
NOT too hot
OFF
open
set
NOT pushed
too hot
OFF
7 def test_door_open_and_timer_set(self): 8 my_expectation = 'OFF' 9 10 reality = src.microwave.microwave( 11 door_is_open=True, 12 timer_is_set=True, 13 start_is_pushed=True, 14 overheating=True, 15 ) 16 self.assertEqual(reality, my_expectation) 17 18 reality = src.microwave.microwave( 19 door_is_open=True, 20 timer_is_set=True, 21 start_is_pushed=True, 22 overheating=False, 23 ) 24 self.assertEqual(reality, my_expectation) 25 26 reality = src.microwave.microwave( 27 door_is_open=True, 28 timer_is_set=True, 29 start_is_pushed=False, 30 overheating=True 31 ) 32 self.assertEqual(reality, my_expectation) 33 34 def test_door_open_and_timer_not_set(self):still green
I add an assertion for when the door is open, the timer is set, the start button is NOT pushed and the microwave temperature is NOT too hot
door
timer
start button
too hot
output
open
set
pushed
too hot
OFF
open
set
pushed
NOT too hot
OFF
open
set
NOT pushed
too hot
OFF
open
set
NOT pushed
NOT too hot
OFF
7 def test_door_open_and_timer_set(self): 8 my_expectation = 'OFF' 9 10 reality = src.microwave.microwave( 11 door_is_open=True, 12 timer_is_set=True, 13 start_is_pushed=True, 14 overheating=True, 15 ) 16 self.assertEqual(reality, my_expectation) 17 18 reality = src.microwave.microwave( 19 door_is_open=True, 20 timer_is_set=True, 21 start_is_pushed=True, 22 overheating=False, 23 ) 24 self.assertEqual(reality, my_expectation) 25 26 reality = src.microwave.microwave( 27 door_is_open=True, 28 timer_is_set=True, 29 start_is_pushed=False, 30 overheating=True 31 ) 32 self.assertEqual(reality, my_expectation) 33 34 reality = src.microwave.microwave( 35 door_is_open=True, 36 timer_is_set=True, 37 start_is_pushed=False, 38 overheating=False, 39 ) 40 41 def test_door_open_and_timer_not_set(self):green
I change the name of the test from test_door_open_and_timer_set to test_door_open_timer_set_w_overheating
5class TestMicrowave(unittest.TestCase): 6 7 def test_door_open_timer_set_w_overheating(self): 8 my_expectation = 'OFF'I add a global variable for
'OFF'. I want to use it to remove repetition from the tests1import src.microwave 2import unittest 3 4 5OFF = 'OFF' 6 7 8class TestMicrowave(unittest.TestCase): 9 10 def test_door_open_timer_set_w_overheating(self):I use the global variable for
my_expectationin test_door_open_timer_set_w_overheating10 def test_door_open_timer_set_w_overheating(self): 11 # my_expectation = 'OFF' 12 13 reality = src.microwave.microwave( 14 door_is_open=True, 15 timer_is_set=True, 16 start_is_pushed=True, 17 overheating=True, 18 ) 19 # self.assertEqual(reality, my_expectation) 20 self.assertEqual(reality, OFF) 21 22 reality = src.microwave.microwave( 23 door_is_open=True, 24 timer_is_set=True, 25 start_is_pushed=True, 26 overheating=False, 27 ) 28 # self.assertEqual(reality, my_expectation) 29 self.assertEqual(reality, OFF) 30 31 reality = src.microwave.microwave( 32 door_is_open=True, 33 timer_is_set=True, 34 start_is_pushed=False, 35 overheating=True 36 ) 37 # self.assertEqual(reality, my_expectation) 38 self.assertEqual(reality, OFF) 39 40 reality = src.microwave.microwave( 41 door_is_open=True, 42 timer_is_set=True, 43 start_is_pushed=False, 44 overheating=False, 45 ) 46 # self.assertEqual(reality, my_expectation) 47 self.assertEqual(reality, OFF) 48 49 def test_door_open_and_timer_not_set(self):still green
I remove the commented lines
10 def test_door_open_timer_set_w_overheating(self): 11 reality = src.microwave.microwave( 12 door_is_open=True, 13 timer_is_set=True, 14 start_is_pushed=True, 15 overheating=True, 16 ) 17 self.assertEqual(reality, OFF) 18 19 reality = src.microwave.microwave( 20 door_is_open=True, 21 timer_is_set=True, 22 start_is_pushed=True, 23 overheating=False, 24 ) 25 self.assertEqual(reality, OFF) 26 27 reality = src.microwave.microwave( 28 door_is_open=True, 29 timer_is_set=True, 30 start_is_pushed=False, 31 overheating=True 32 ) 33 self.assertEqual(reality, OFF) 34 35 reality = src.microwave.microwave( 36 door_is_open=True, 37 timer_is_set=True, 38 start_is_pushed=False, 39 overheating=False, 40 ) 41 self.assertEqual(reality, OFF) 42 43 def test_door_open_and_timer_not_set(self):
test_door_open_timer_not_set_w_overheating
I add a value for the
overheatingparameter to the assertion in test_door_open_and_timer_not_set for when the door is open, the timer is NOT set, the start button is pushed and the microwave temperature is too hotdoor
timer
start button
too hot
output
open
NOT set
pushed
too hot
OFF
43 def test_door_open_and_timer_not_set(self): 44 my_expectation = 'OFF' 45 46 reality = src.microwave.microwave( 47 door_is_open=True, 48 timer_is_set=False, 49 start_is_pushed=True, 50 overheating=True, 51 ) 52 self.assertEqual(reality, my_expectation)still green
I add an assertion for when the door is open, the timer is NOT set, the start button is pushed and the microwave temperature is NOT too hot
door
timer
start button
too hot
output
open
NOT set
pushed
too hot
OFF
open
NOT set
pushed
NOT too hot
OFF
43 def test_door_open_and_timer_not_set(self): 44 my_expectation = 'OFF' 45 46 reality = src.microwave.microwave( 47 door_is_open=True, 48 timer_is_set=False, 49 start_is_pushed=True, 50 overheating=True, 51 ) 52 self.assertEqual(reality, my_expectation) 53 54 reality = src.microwave.microwave( 55 door_is_open=True, 56 timer_is_set=False, 57 start_is_pushed=True, 58 overheating=False, 59 ) 60 self.assertEqual(reality, my_expectation) 61 62 reality = src.microwave.microwave( 63 door_is_open=True, 64 timer_is_set=False, 65 start_is_pushed=False, 66 ) 67 self.assertEqual(reality, my_expectation) 68 69 def test_door_closed_and_timer_set(self):the test is still green
I add a value for
overheatingto the next assertion for when the door is open, the timer is NOT set, the start button is NOT pushed and the microwave temperatur is too hotdoor
timer
start button
too hot
output
open
NOT set
pushed
too hot
OFF
open
NOT set
pushed
NOT too hot
OFF
open
NOT set
NOT pushed
too hot
OFF
43 def test_door_open_and_timer_not_set(self): 44 my_expectation = 'OFF' 45 46 reality = src.microwave.microwave( 47 door_is_open=True, 48 timer_is_set=False, 49 start_is_pushed=True, 50 overheating=True, 51 ) 52 self.assertEqual(reality, my_expectation) 53 54 reality = src.microwave.microwave( 55 door_is_open=True, 56 timer_is_set=False, 57 start_is_pushed=True, 58 overheating=False, 59 ) 60 self.assertEqual(reality, my_expectation) 61 62 reality = src.microwave.microwave( 63 door_is_open=True, 64 timer_is_set=False, 65 start_is_pushed=False, 66 overheating=True 67 ) 68 self.assertEqual(reality, my_expectation) 69 70 def test_door_closed_and_timer_set(self):still green
I add an assertion for when the door is open, the timer is NOT set, the start button is NOT pushed and the microwave temperature is NOT too hot
door
timer
start button
too hot
output
open
NOT set
pushed
too hot
OFF
open
NOT set
pushed
NOT too hot
OFF
open
NOT set
NOT pushed
too hot
OFF
open
NOT set
NOT pushed
NOT too hot
OFF
43 def test_door_open_and_timer_not_set(self): 44 my_expectation = 'OFF' 45 46 reality = src.microwave.microwave( 47 door_is_open=True, 48 timer_is_set=False, 49 start_is_pushed=True, 50 overheating=True, 51 ) 52 self.assertEqual(reality, my_expectation) 53 54 reality = src.microwave.microwave( 55 door_is_open=True, 56 timer_is_set=False, 57 start_is_pushed=True, 58 overheating=False, 59 ) 60 self.assertEqual(reality, my_expectation) 61 62 reality = src.microwave.microwave( 63 door_is_open=True, 64 timer_is_set=False, 65 start_is_pushed=False, 66 overheating=True 67 ) 68 self.assertEqual(reality, my_expectation) 69 70 reality = src.microwave.microwave( 71 door_is_open=True, 72 timer_is_set=False, 73 start_is_pushed=False, 74 overheating=False, 75 ) 76 self.assertEqual(reality, my_expectation) 77 78 def test_door_closed_and_timer_set(self):green
I change the name of the test from test_door_open_and_timer_not_set to test_door_open_timer_not_set_w_overheating
35 reality = src.microwave.microwave( 36 door_is_open=True, 37 timer_is_set=True, 38 start_is_pushed=False, 39 overheating=False, 40 ) 41 self.assertEqual(reality, OFF) 42 43 def test_door_open_timer_not_set_w_overheating(self): 44 my_expectation = 'OFF'I use the
OFFglobal variable to remove repetition from43 def test_door_open_timer_not_set_w_overheating(self): 44 # my_expectation = 'OFF' 45 46 reality = src.microwave.microwave( 47 door_is_open=True, 48 timer_is_set=False, 49 start_is_pushed=True, 50 overheating=True, 51 ) 52 # self.assertEqual(reality, my_expectation) 53 self.assertEqual(reality, OFF) 54 55 reality = src.microwave.microwave( 56 door_is_open=True, 57 timer_is_set=False, 58 start_is_pushed=True, 59 overheating=False, 60 ) 61 # self.assertEqual(reality, my_expectation) 62 self.assertEqual(reality, OFF) 63 64 reality = src.microwave.microwave( 65 door_is_open=True, 66 timer_is_set=False, 67 start_is_pushed=False, 68 overheating=True 69 ) 70 # self.assertEqual(reality, my_expectation) 71 self.assertEqual(reality, OFF) 72 73 reality = src.microwave.microwave( 74 door_is_open=True, 75 timer_is_set=False, 76 start_is_pushed=False, 77 overheating=False, 78 ) 79 # self.assertEqual(reality, my_expectation) 80 self.assertEqual(reality, OFF)still green
I remove the commented lines
43 def test_door_open_timer_not_set_w_overheating(self): 44 reality = src.microwave.microwave( 45 door_is_open=True, 46 timer_is_set=False, 47 start_is_pushed=True, 48 overheating=True, 49 ) 50 self.assertEqual(reality, OFF) 51 52 reality = src.microwave.microwave( 53 door_is_open=True, 54 timer_is_set=False, 55 start_is_pushed=True, 56 overheating=False, 57 ) 58 self.assertEqual(reality, OFF) 59 60 reality = src.microwave.microwave( 61 door_is_open=True, 62 timer_is_set=False, 63 start_is_pushed=False, 64 overheating=True 65 ) 66 self.assertEqual(reality, OFF) 67 68 reality = src.microwave.microwave( 69 door_is_open=True, 70 timer_is_set=False, 71 start_is_pushed=False, 72 overheating=False, 73 ) 74 self.assertEqual(reality, OFF) 75 76 def test_door_closed_and_timer_set(self):
test_door_closed_timer_set_w_overheating
I use the
OFFglobal variable formy_expectationwhen the value is'OFF'in test_door_closed_and_timer_set76 def test_door_closed_and_timer_set(self): 77 my_expectation = 'HEATING' 78 reality = src.microwave.microwave( 79 door_is_open=False, 80 timer_is_set=True, 81 start_is_pushed=True, 82 ) 83 self.assertEqual(reality, my_expectation) 84 85 # my_expectation = 'OFF' 86 87 reality = src.microwave.microwave( 88 door_is_open=False, 89 start_is_pushed=False, 90 ) 91 # self.assertEqual(reality, my_expectation) 92 self.assertEqual(reality, OFF) 93 94 def test_door_closed_and_timer_not_set(self):the test is still green
I remove the commented lines
76 def test_door_closed_and_timer_set(self): 77 my_expectation = 'HEATING' 78 reality = src.microwave.microwave( 79 door_is_open=False, 80 timer_is_set=True, 81 start_is_pushed=True, 82 ) 83 self.assertEqual(reality, my_expectation) 84 85 reality = src.microwave.microwave( 86 door_is_open=False, 87 start_is_pushed=False, 88 ) 89 self.assertEqual(reality, OFF) 90 91 def test_door_closed_and_timer_not_set(self):I add a value for the
overheatingandtimer_is_setparameters in the second assertion, for when the door is closed, the timer is set, the start button is pushed, and the microwave temperature is too hotdoor
timer
start button
too hot
output
closed
set
pushed
too hot
OFF
76 def test_door_closed_and_timer_set(self): 77 my_expectation = 'HEATING' 78 reality = src.microwave.microwave( 79 door_is_open=False, 80 timer_is_set=True, 81 start_is_pushed=True, 82 ) 83 self.assertEqual(reality, my_expectation) 84 85 reality = src.microwave.microwave( 86 door_is_open=False, 87 timer_is_set=True, 88 start_is_pushed=False, 89 overheating=True, 90 ) 91 self.assertEqual(reality, OFF) 92 93 def test_door_closed_and_timer_not_set(self):still green
I add a value for the
overheatingparameter to the first assertion, for when the door is closed, the timer is set, the start button is pushed and the microwave temperature is NOT too hotdoor
timer
start button
too hot
output
closed
set
pushed
too hot
OFF
closed
set
pushed
NOT too hot
HEATING
76 def test_door_closed_and_timer_set(self): 77 my_expectation = 'HEATING' 78 reality = src.microwave.microwave( 79 door_is_open=False, 80 timer_is_set=True, 81 start_is_pushed=True, 82 overheating=False, 83 ) 84 self.assertEqual(reality, my_expectation) 85 86 reality = src.microwave.microwave( 87 door_is_open=False, 88 timer_is_set=True, 89 start_is_pushed=False, 90 overheating=True, 91 ) 92 self.assertEqual(reality, OFF) 93 94 def test_door_closed_and_timer_not_set(self):green
I add an assertion for when the door is closed, the timer is set, the start button is NOT pushed, and the microwave temperature is too hot
door
timer
start button
too hot
output
closed
set
pushed
too hot
OFF
closed
set
pushed
NOT too hot
HEATING
closed
set
NOT pushed
too hot
OFF
76 def test_door_closed_and_timer_set(self): 77 my_expectation = 'HEATING' 78 reality = src.microwave.microwave( 79 door_is_open=False, 80 timer_is_set=True, 81 start_is_pushed=True, 82 overheating=False, 83 ) 84 self.assertEqual(reality, my_expectation) 85 86 reality = src.microwave.microwave( 87 door_is_open=False, 88 timer_is_set=True, 89 start_is_pushed=False, 90 overheating=True, 91 ) 92 self.assertEqual(reality, OFF) 93 94 reality = src.microwave.microwave( 95 door_is_open=False, 96 timer_is_set=True, 97 start_is_pushed=False, 98 overheating=True, 99 ) 100 self.assertEqual(reality, OFF) 101 102 def test_door_closed_and_timer_not_set(self):still green
I add an assertion for when the door is closed, the timer is set, the start button is NOT pushed, and the microwave temperature is NOT too hot
door
timer
start button
too hot
output
closed
set
pushed
too hot
OFF
closed
set
pushed
NOT too hot
HEATING
closed
set
NOT pushed
too hot
OFF
closed
set
NOT pushed
NOT too hot
OFF
76 def test_door_closed_and_timer_set(self): 77 my_expectation = 'HEATING' 78 reality = src.microwave.microwave( 79 door_is_open=False, 80 timer_is_set=True, 81 start_is_pushed=True, 82 overheating=False, 83 ) 84 self.assertEqual(reality, my_expectation) 85 86 reality = src.microwave.microwave( 87 door_is_open=False, 88 timer_is_set=True, 89 start_is_pushed=False, 90 overheating=True, 91 ) 92 self.assertEqual(reality, OFF) 93 94 reality = src.microwave.microwave( 95 door_is_open=False, 96 timer_is_set=True, 97 start_is_pushed=False, 98 overheating=True, 99 ) 100 self.assertEqual(reality, OFF) 101 102 reality = src.microwave.microwave( 103 door_is_open=False, 104 timer_is_set=True, 105 start_is_pushed=False, 106 overheating=False 107 ) 108 self.assertEqual(reality, OFF) 109 110 def test_door_closed_and_timer_not_set(self):the test is still green
I change the name of the test from test_door_closed_and_timer_set to test_door_closed_timer_set_w_overheating
68 reality = src.microwave.microwave( 69 door_is_open=True, 70 timer_is_set=False, 71 start_is_pushed=False, 72 overheating=False, 73 ) 74 self.assertEqual(reality, OFF) 75 76 def test_door_closed_timer_set_w_overheating(self): 77 my_expectation = 'HEATING'
test_door_closed_timer_not_set_w_overheating
I use the
OFFglobal variable to remove repetition from test_door_closed_and_timer_not_set110 def test_door_closed_and_timer_not_set(self): 111 # my_expectation = 'OFF' 112 113 reality = src.microwave.microwave( 114 door_is_open=False, 115 timer_is_set=False, 116 start_is_pushed=True, 117 ) 118 # self.assertEqual(reality, my_expectation) 119 self.assertEqual(reality, OFF) 120 121 reality = src.microwave.microwave( 122 door_is_open=False, 123 timer_is_set=False, 124 start_is_pushed=False, 125 ) 126 # self.assertEqual(reality, my_expectation) 127 self.assertEqual(reality, OFF) 128 129 130# Exceptions seenstill green
I remove the commented lines
110 def test_door_closed_and_timer_not_set(self): 111 reality = src.microwave.microwave( 112 door_is_open=False, 113 timer_is_set=False, 114 start_is_pushed=True, 115 ) 116 self.assertEqual(reality, OFF) 117 118 reality = src.microwave.microwave( 119 door_is_open=False, 120 timer_is_set=False, 121 start_is_pushed=False, 122 ) 123 self.assertEqual(reality, OFF) 124 125 126# Exceptions seenI change the name of the test from test_door_closed_and_timer_not_set to test_door_closed_timer_not_set_w_overheating
102 reality = src.microwave.microwave( 103 door_is_open=False, 104 timer_is_set=True, 105 start_is_pushed=False, 106 overheating=False 107 ) 108 self.assertEqual(reality, OFF) 109 110 def test_door_closed_timer_not_set_w_overheating(self): 111 reality = src.microwave.microwave( 112 door_is_open=False, 113 timer_is_set=False, 114 start_is_pushed=True, 115 ) 116 self.assertEqual(reality, OFF)I add a value for the
overheatingparameter to the first assertion in test_door_closed_timer_not_set_w_overheating, for when the door is closed, the timer is NOT set, the start button is pushed, and the microwave temperature is too hotdoor
timer
start button
too hot
output
closed
NOT set
pushed
too hot
OFF
110 def test_door_closed_timer_not_set_w_overheating(self): 111 reality = src.microwave.microwave( 112 door_is_open=False, 113 timer_is_set=False, 114 start_is_pushed=True, 115 overheating=True, 116 ) 117 self.assertEqual(reality, OFF) 118 119 reality = src.microwave.microwave( 120 door_is_open=False, 121 timer_is_set=False, 122 start_is_pushed=False, 123 ) 124 self.assertEqual(reality, OFF)green
I add an assertion for when the door is closed, the timer is NOT set, the start button is pushed, and the microwave temperature is NOT too hot
door
timer
start button
too hot
output
closed
NOT set
pushed
too hot
OFF
closed
NOT set
pushed
NOT too hot
OFF
110 def test_door_closed_timer_not_set_w_overheating(self): 111 reality = src.microwave.microwave( 112 door_is_open=False, 113 timer_is_set=False, 114 start_is_pushed=True, 115 overheating=True, 116 ) 117 self.assertEqual(reality, OFF) 118 119 reality = src.microwave.microwave( 120 door_is_open=False, 121 timer_is_set=False, 122 start_is_pushed=True, 123 overheating=False, 124 ) 125 self.assertEqual(reality, OFF) 126 127 reality = src.microwave.microwave( 128 door_is_open=False, 129 timer_is_set=False, 130 start_is_pushed=False, 131 ) 132 self.assertEqual(reality, OFF) 133 134 135# Exceptions seenstill green
I add a value for the
overheatingparameter to the next assertion, for when the door is closed, the timer is NOT set, the start button is NOT pushed, and the microwave temperature is too hotdoor
timer
start button
too hot
output
closed
NOT set
pushed
too hot
OFF
closed
NOT set
pushed
NOT too hot
OFF
closed
NOT set
NOT pushed
too hot
OFF
110 def test_door_closed_timer_not_set_w_overheating(self): 111 reality = src.microwave.microwave( 112 door_is_open=False, 113 timer_is_set=False, 114 start_is_pushed=True, 115 overheating=True, 116 ) 117 self.assertEqual(reality, OFF) 118 119 reality = src.microwave.microwave( 120 door_is_open=False, 121 timer_is_set=False, 122 start_is_pushed=True, 123 overheating=False, 124 ) 125 self.assertEqual(reality, OFF) 126 127 reality = src.microwave.microwave( 128 door_is_open=False, 129 timer_is_set=False, 130 start_is_pushed=False, 131 overheating=True, 132 ) 133 self.assertEqual(reality, OFF) 134 135 136# Exceptions seenthe test is still green
I add an assertion for when the door is closed, the timer is NOT set, the start button is NOT pushed, and the microwave temperature is NOT too hot
door
timer
start button
too hot
output
closed
NOT set
pushed
too hot
OFF
closed
NOT set
pushed
NOT too hot
OFF
closed
NOT set
NOT pushed
too hot
OFF
closed
NOT set
NOT pushed
NOT too hot
OFF
110 def test_door_closed_timer_not_set_w_overheating(self): 111 reality = src.microwave.microwave( 112 door_is_open=False, 113 timer_is_set=False, 114 start_is_pushed=True, 115 overheating=True, 116 ) 117 self.assertEqual(reality, OFF) 118 119 reality = src.microwave.microwave( 120 door_is_open=False, 121 timer_is_set=False, 122 start_is_pushed=True, 123 overheating=False, 124 ) 125 self.assertEqual(reality, OFF) 126 127 reality = src.microwave.microwave( 128 door_is_open=False, 129 timer_is_set=False, 130 start_is_pushed=False, 131 overheating=True, 132 ) 133 self.assertEqual(reality, OFF) 134 135 reality = src.microwave.microwave( 136 door_is_open=False, 137 timer_is_set=False, 138 start_is_pushed=False, 139 overheating=False, 140 ) 141 self.assertEqual(reality, OFF) 142 143 144# Exceptions seenstill green. All the tests are still passing
I use the call to the
microwavefunction directly in the assertion I do not therealityvariable because it is only used once in each one
close the project
I close
test_microwave.pyandmicrowave.pyin the editorI click in the terminal, then use q on the keyboard to leave the tests. The terminal goes back to the command line
I change directory to the parent of
microwavecd ..the terminal is my friend, and shows
.../pumping_pythonI am back in the
pumping_pythondirectory
review
I ran tests for an Microwave with the following inputs:
has the card expired?
is the PIN correct?
is the amount I want to take, smaller or bigger than what is in the account?
will this put the account above or below the daily limit for microwaveals?
the inputs gave me this truth table
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
set |
pushed |
too hot |
OFF |
open |
set |
pushed |
NOT too hot |
OFF |
open |
set |
NOT pushed |
too hot |
OFF |
open |
set |
NOT pushed |
NOT too hot |
OFF |
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
NOT set |
pushed |
too hot |
OFF |
open |
NOT set |
pushed |
NOT too hot |
OFF |
open |
NOT set |
NOT pushed |
too hot |
OFF |
open |
NOT set |
NOT pushed |
NOT too hot |
OFF |
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
closed |
set |
pushed |
too hot |
OFF |
closed |
set |
pushed |
NOT too hot |
HEATING |
closed |
set |
NOT pushed |
too hot |
OFF |
closed |
set |
NOT pushed |
NOT too hot |
OFF |
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
closed |
NOT set |
pushed |
too hot |
OFF |
closed |
NOT set |
pushed |
NOT too hot |
OFF |
closed |
NOT set |
NOT pushed |
too hot |
OFF |
closed |
NOT set |
NOT pushed |
NOT too hot |
OFF |
What if I want the microwave to give a different message with each denial, so that the user knows why the microwaveal failed? The truth table could then be
card expired |
PIN |
balance |
daily limit |
microwaveal |
|---|---|---|---|---|
expired |
right PIN |
enough |
above limit |
DENIED: Card Expired |
expired |
right PIN |
enough |
below limit |
DENIED: Card Expired |
expired |
right PIN |
NOT enough |
above limit |
DENIED: Card Expired |
expired |
right PIN |
NOT enough |
below limit |
DENIED: Card Expired |
card expired |
PIN |
balance |
daily limit |
microwaveal |
|---|---|---|---|---|
expired |
wrong PIN |
enough |
above limit |
DENIED: Card Expired |
expired |
wrong PIN |
enough |
below limit |
DENIED: Card Expired |
expired |
wrong PIN |
NOT enough |
above limit |
DENIED: Card Expired |
expired |
wrong PIN |
NOT enough |
below limit |
DENIED: Card Expired |
card expired |
PIN |
balance |
daily limit |
microwaveal |
|---|---|---|---|---|
NOT expired |
right PIN |
enough |
above limit |
DENIED: You have exceeded the daily microwaveal Limit |
NOT expired |
right PIN |
enough |
below limit |
CASH |
NOT expired |
right PIN |
NOT enough |
above limit |
DENIED: There is not enough money in the account to complete the microwaveal |
NOT expired |
right PIN |
NOT enough |
below limit |
DENIED: There is not enough money in the account to complete the microwaveal |
card expired |
PIN |
balance |
daily limit |
microwaveal |
|---|---|---|---|---|
NOT expired |
wrong PIN |
enough |
above limit |
Error: You entered the wrong PIN. Try again |
NOT expired |
wrong PIN |
enough |
below limit |
Error: You entered the wrong PIN. Try again |
NOT expired |
wrong PIN |
NOT enough |
above limit |
Error: You entered the wrong PIN. Try again |
NOT expired |
wrong PIN |
NOT enough |
below limit |
Error: You entered the wrong PIN. Try again |
code from the chapter
what is next?
you now know
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too