Microwave
I want to make a Microwave that heats up food when I push a button.
preview
These are the tests I have at the end of the chapter
1import src.microwave
2import unittest
3
4
5class TestMicrowave(unittest.TestCase):
6
7 def test_too_hot_w_set_timer_closed_door_pressed_start(self):
8 self.assertFalse(
9 src.microwave.microwave(
10 closed_door=True,
11 set_timer=True,
12 pressed_start=True,
13 too_hot=True,
14 )
15 )
16 self.assertTrue(
17 src.microwave.microwave(
18 closed_door=True,
19 set_timer=True,
20 pressed_start=True,
21 too_hot=False,
22 )
23 )
25 def test_too_hot_w_set_timer_closed_door_not_pressed_start(self):
26 self.assertFalse(
27 src.microwave.microwave(
28 closed_door=True,
29 set_timer=True,
30 pressed_start=False,
31 too_hot=True,
32 )
33 )
34 self.assertFalse(
35 src.microwave.microwave(
36 closed_door=True,
37 set_timer=True,
38 pressed_start=False,
39 too_hot=False,
40 )
41 )
43 def test_too_hot_w_not_set_timer_closed_door_pressed_start(self):
44 self.assertFalse(
45 src.microwave.microwave(
46 closed_door=True,
47 set_timer=False,
48 pressed_start=True,
49 too_hot=True,
50 )
51 )
52 self.assertFalse(
53 src.microwave.microwave(
54 closed_door=True,
55 set_timer=False,
56 pressed_start=True,
57 too_hot=False,
58 )
59 )
61 def test_too_hot_w_not_set_timer_closed_door_not_pressed_start(self):
62 self.assertFalse(
63 src.microwave.microwave(
64 closed_door=True,
65 set_timer=False,
66 pressed_start=False,
67 too_hot=True,
68 )
69 )
70 self.assertFalse(
71 src.microwave.microwave(
72 closed_door=True,
73 set_timer=False,
74 pressed_start=False,
75 too_hot=False,
76 )
77 )
79 def test_too_hot_w_set_timer_open_door_pressed_start(self):
80 self.assertFalse(
81 src.microwave.microwave(
82 closed_door=False,
83 set_timer=True,
84 pressed_start=True,
85 too_hot=True,
86 )
87 )
88 self.assertFalse(
89 src.microwave.microwave(
90 closed_door=False,
91 set_timer=True,
92 pressed_start=True,
93 too_hot=False,
94 )
95 )
97 def test_too_hot_w_set_timer_open_door_not_pressed_start(self):
98 self.assertFalse(
99 src.microwave.microwave(
100 closed_door=False,
101 set_timer=True,
102 pressed_start=False,
103 too_hot=True,
104 )
105 )
106 self.assertFalse(
107 src.microwave.microwave(
108 closed_door=False,
109 set_timer=True,
110 pressed_start=False,
111 too_hot=False,
112 )
113 )
115 def test_too_hot_w_not_set_timer_open_door_pressed_start(self):
116 self.assertFalse(
117 src.microwave.microwave(
118 closed_door=False,
119 set_timer=False,
120 pressed_start=True,
121 too_hot=True,
122 )
123 )
124 self.assertFalse(
125 src.microwave.microwave(
126 closed_door=False,
127 set_timer=False,
128 pressed_start=True,
129 too_hot=False,
130 )
131 )
133 def test_too_hot_w_not_set_timer_open_door_not_pressed_start(self):
134 self.assertFalse(
135 src.microwave.microwave(
136 closed_door=False,
137 set_timer=False,
138 pressed_start=False,
139 too_hot=True,
140 )
141 )
142 self.assertFalse(
143 src.microwave.microwave(
144 closed_door=False,
145 set_timer=False,
146 pressed_start=False,
147 too_hot=False,
148 )
149 )
150
151
152# Exceptions seen
153# AssertionError
154# NameError
155# AttributeError
156# TypeError
157# SyntaxError
start the project
I open a terminal
I open
makePythonTdd.shI change the name of the project to
microwaveinmakePythonTdd.sh1#!/bin/bash 2uv init microwave 3cd microwave 4mkdir tests 5touch tests/__init__.py 6 7echo "import unittest 8 9 10class TestMicrowave(unittest.TestCase): 11 12 def test_failure(self): 13 self.assertFalse(True) 14 15 16# Exceptions seen 17# AssertionError 18" > tests/test_microwave.py 19 20echo "pytest" > requirements.txt 21echo "pytest-watcher" >> requirements.txt 22uv add --requirement requirements.txt 23uv run pytest-watcher . --nowI run
makePythonTdd.shin the terminal to make themicrowaveproject./makePythonTdd.sh
I open
makePythonTdd.ps1I change the name of the project to
microwaveinmakePythonTdd.ps11uv init microwave 2cd microwave 3New-Item "src/microwave.py" 4mkdir tests 5New-Item tests/__init__.py 6 7"import unittest 8 9 10class TestMicrowave(unittest.TestCase): 11 12 def test_failure(self): 13 self.assertFalse(True) 14 15 16# Exceptions seen 17# AssertionError 18" | Out-File "tests/test_microwave.py" -Encoding UTF8 19 20"pytest" | Out-File requirements.txt -Encoding UTF8 21"pytest-watcher" >> requirements.txt 22uv add --requirement requirements.txt 23uv run pytest-watcher . --nowI run
makePythonTdd.ps1in the terminal to make themicrowaveproject.\makePythonTdd.ps1
the terminal is my friend, and shows AssertionError
======================== 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 hold ctrl (Windows/Linux) or option/command (MacOS) on the keyboard and use the mouse to click on
tests/test_microwave.py:7to open itI change True to False in
test_microwave.py4class TestMicrowave(unittest.TestCase): 5 6 def test_failure(self): 7 # self.assertFalse(True) 8 self.assertFalse(False) 9 10 11# Exceptions seenthe test passes.
I open a new terminal then change directory to
microwavecd microwaveI add the new files and folder to git for tracking
git add .I add a git commit message
git commit -am 'setup project'
I want the Microwave to HEAT UP only when the start button is pressed. I get this truth table
start button |
output |
|---|---|
pressed |
True |
NOT pressed |
False |
Where True means the Microwave will HEAT UP, and False means it stays OFF.
test_pressed_start
RED: make it fail
I go back to the terminal where the tests are running
I change test_failure to test_pressed_start with an assertion for if the start button is pressed
start button
output
pressed
True
4class TestMicrowave(unittest.TestCase): 5 6 def test_pressed_start(self): 7 self.assertTrue( 8 src.microwave.microwave( 9 pressed_start=True, 10 ) 11 ) 12 13 14# Exceptions seen 15# AssertionErrorthe terminal is my friend, and shows NameError
NameError: name 'src' is not definedbecause I do not have a definition for
srcin this file.I add NameError to the list of Exceptions seen
14# Exceptions seen 15# AssertionError 16# NameError
GREEN: make it pass
I 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'because
microwave/__init__.pyin thesrcfolder does not have anything namedmicrowavein it.I add AttributeError to the list of Exceptions seen
15# Exceptions seen 16# AssertionError 17# NameError 18# AttributeErrorI open
microwave/__init__.pyfrom thesrcfolderI delete all the text in the file then add a function named
microwavetomicrowave.py1def microwave(): 2 return Nonethe terminal is my friend, and shows TypeError
TypeError: microwave() got an unexpected keyword argument 'pressed_start'because the test called the
microwavefunction with a name (pressed_start) that is not in the parentheses of its definition.I add TypeError to the list of Exceptions seen, in
test_microwave.py15# Exceptions seen 16# AssertionError 17# NameError 18# AttributeError 19# TypeErrorI add
pressed_startto the function definition1def microwave(pressed_start): 2 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: None is not truethe
microwavefunction returned None and the assertion expects TrueI change the return statement to give the test what it wants
1def microwave(pressed_start): 2 return Truethe test passes. The
microwavefunction always returns True.microwave(pressed_start=True ) -> TrueI add a git commit message in the other terminal
git commit -am 'add test_pressed_start'
test_not_pressed_start
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the start button is NOT pressed
start button
output
NOT pressed
False
7 def test_pressed_start(self): 8 self.assertTrue( 9 src.microwave.microwave( 10 pressed_start=True, 11 ) 12 ) 13 14 def test_not_pressed_start(self): 15 self.assertFalse( 16 src.microwave.microwave( 17 pressed_start=False, 18 ) 19 ) 20 21 22# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: True is not falsebecause the
microwavefunction always returns True and this assertion expects False.
GREEN: make it pass
I make the function return its input
1def microwave(pressed_start): 2 return pressed_startthe test passes.
microwave(pressed_start=True ) -> True microwave(pressed_start=False) -> FalseI add a git commit message in the other terminal
git commit -am 'add test_not_pressed_start'
I want the Microwave to HEAT UP only when the Microwave door is closed AND the start button is pressed. The inputs to the Microwave will then be
is the Microwave door closed?
was the start button pressed?
and I get this truth table
door |
start button |
output |
|---|---|---|
closed |
pressed |
True |
closed |
NOT pressed |
False |
open |
pressed |
False |
open |
NOT pressed |
False |
Where True means the Microwave will HEAT UP, and False means it stays OFF.
test_open_door_not_pressed_start
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the Microwave door is open AND the start button is NOT pressed, in
test_microwave.pydoor
start button
output
open
NOT pressed
False
14 def test_not_pressed_start(self): 15 self.assertFalse( 16 src.microwave.microwave( 17 pressed_start=False, 18 ) 19 ) 20 21 def test_open_door_not_pressed_start(self): 22 self.assertFalse( 23 src.microwave.microwave( 24 closed_door=False, 25 pressed_start=False, 26 ) 27 ) 28 29 30# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: microwave() got an unexpected keyword argument 'closed_doorbecause the test called the
microwavefunction with a name (closed_door) that is not in the parentheses of its definition.
GREEN: make it pass
I add
closed_doorto the function signature inmicrowave.py1def microwave(closed_door, pressed_start): 2 return pressed_startthe terminal is my friend, and shows TypeError
FAILED ...test_not_pressed_start - TypeError: microwave() missing 1 required positional argument: 'closed_door' FAILED ...test_pressed_start - TypeError: microwave() missing 1 required positional argument: 'closed_door'I add a default value for
closed_door1def microwave(closed_door=False, pressed_start): 2 return pressed_startthe terminal is my friend, and shows SyntaxError
SyntaxError: parameter without a default follows parameter with a defaultI add SyntaxError to the list of Exceptions seen, in
test_microwave.py30# Exceptions seen 31# AssertionError 32# NameError 33# AttributeError 34# TypeError 35# SyntaxErrorI add a default value for
pressed_start, inmicrowave.py1def microwave(closed_door=False, pressed_start=False): 2 return pressed_startthe test passes.
microwave(closed_door=False, pressed_start=False) -> FalseI add a git commit message in the other terminal
git commit -am 'add test_open_door_not_pressed_start'
test_open_door_pressed_start
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the Microwave door is open AND the start button is pressed, in
test_microwave.pydoor
start button
output
open
pressed
False
14 def test_not_pressed_start(self): 15 self.assertFalse( 16 src.microwave.microwave( 17 pressed_start=False, 18 ) 19 ) 20 21 def test_open_door_pressed_start(self): 22 self.assertFalse( 23 src.microwave.microwave( 24 closed_door=False, 25 pressed_start=True, 26 ) 27 ) 28 29 def test_open_door_not_pressed_start(self):the terminal is my friend, and shows AssertionError
AssertionError: True is not false
GREEN: make it pass
I add an if statement for
closed_doorto themicrowavefunction inmicrowave.py1def microwave(closed_door=False, pressed_start=False): 2 if not closed_door: 3 return False 4 return pressed_startthe test passes.
microwave(closed_door=False, pressed_start=True ) -> False microwave(closed_door=False, pressed_start=False) -> FalseI add a git commit message in the other terminal
git commit -am 'add test_open_door_pressed_start'
test_closed_door_not_pressed_start
I go back to the terminal where the tests are running
I add a value for the
closed_doorparameter to the assertion in test_not_pressed_start for if the Microwave door is closed AND the start button is NOT presseddoor
start button
output
closed
NOT pressed
False
14 def test_not_pressed_start(self): 15 self.assertFalse( 16 src.microwave.microwave( 17 closed_door=True, 18 pressed_start=False, 19 ) 20 ) 21 22 def test_closed_door_pressed_start(self):the test is still green.
microwave(closed_door=True , pressed_start=False) -> False microwave(closed_door=False, pressed_start=True ) -> False microwave(closed_door=False, pressed_start=False) -> FalseI change the name of the test from test_not_pressed_start to test_closed_door_not_pressed_start
7 def test_pressed_start(self): 8 self.assertTrue( 9 src.microwave.microwave( 10 pressed_start=True, 11 ) 12 ) 13 14 def test_closed_door_not_pressed_start(self): 15 self.assertFalse( 16 src.microwave.microwave( 17 closed_door=True, 18 pressed_start=False, 19 ) 20 )I add a git commit message in the other terminal
git commit -am 'add test_closed_door_not_pressed_start'
test_closed_door_pressed_start
I go back to the terminal where the tests are running
I add a value for the
closed_doorparameter to the assertion in test_pressed_start for if the Microwave door is closed AND the start button is presseddoor
start button
output
closed
pressed
True
7 def test_pressed_start(self): 8 self.assertTrue( 9 src.microwave.microwave( 10 closed_door=True, 11 pressed_start=True, 12 ) 13 ) 14 15 def test_closed_door_not_pressed_start(self):the test is still green.
microwave(closed_door=True , pressed_start=True ) -> True microwave(closed_door=True , pressed_start=False) -> False microwave(closed_door=False, pressed_start=True ) -> False microwave(closed_door=False, pressed_start=False) -> FalseI add a git commit message in the other terminal
git commit -am 'add test_closed_door_pressed_start'
When the microwave function is called, it checks if the door is closed
if the door is open it returns False
microwave(closed_door=False, pressed_start=True ) -> False └── def microwave(closed_door=False, pressed_start=False): └── if not closed_door: └── return False return pressed_startmicrowave(closed_door=False, pressed_start=False) -> False └── def microwave(closed_door=False, pressed_start=False): └── if not closed_door: └── return False return pressed_startif the door is closed it returns the value of
pressed_startif the door is closed AND the start button is NOT pressed it returns False
microwave(closed_door=True , pressed_start=False) -> False └── def microwave(closed_door=False, pressed_start=False): ├── if not closed_door: │ return False └── return pressed_start return Falseif the door is closed AND the start button is pressed it returns True
microwave(closed_door=True , pressed_start=True ) -> True └── def microwave(closed_door=False, pressed_start=False): ├── if not closed_door: │ return False └── return pressed_start return True
So far, the truth table for the Microwave is
door |
start button |
output |
|---|---|---|
closed |
pressed |
True |
closed |
NOT pressed |
False |
open |
pressed |
False |
open |
NOT pressed |
False |
I want the Microwave to only heat up food when the door is closed AND the timer is set AND the start button is pressed. The inputs for the Microwave will then be
is the Microwave door closed?
is the Microwave timer set?
was the start button pressed?
test_set_timer_closed_door_pressed_start
RED: make it fail
I go back to the terminal where the tests are running
I add
set_timerto the assertion in test_closed_door_pressed_start, for if the Microwave door is closed AND the timer is set AND the start button is presseddoor
timer
start button
output
closed
set
pressed
True
7 def test_closed_door_pressed_start(self): 8 self.assertTrue( 9 src.microwave.microwave( 10 closed_door=True, 11 set_timer=True, 12 pressed_start=True, 13 ) 14 ) 15 16 def test_closed_door_not_pressed_start(self):the terminal is my friend, and shows TypeError
TypeError: microwave() got an unexpected keyword argument 'set_timer'because the test called the
microwavefunction with a name (set_timer) that is not in the parentheses of its definition.
GREEN: make it pass
I add set_timer to the function signature in microwave.py
1def microwave(
2 closed_door=False, pressed_start=False,
3 set_timer=False,
4 ):
5 if not closed_door:
6 return False
7 return pressed_start
the test passes.
microwave(
closed_door=True, set_timer=True,
pressed_start=True
) -> True
REFACTOR: make it better
I change the name of the test from test_closed_door_pressed_start to test_set_timer_closed_door_pressed_start
5class TestMicrowave(unittest.TestCase): 6 7 def test_set_timer_closed_door_pressed_start(self): 8 self.assertTrue( 9 src.microwave.microwave( 10 closed_door=True, 11 set_timer=True, 12 pressed_start=True, 13 ) 14 )I add a git commit message in the other terminal
git commit -am \ 'add test_set_timer_closed_door_pressed_start'
test_set_timer_closed_door_not_pressed_start
I go back to the terminal where the tests are running
I add a value for
set_timerto the assertion in test_closed_door_not_pressed_start for if the Microwave door is closed AND the timer is set AND the start button is NOT presseddoor
timer
start button
output
closed
set
NOT pressed
False
16 def test_closed_door_not_pressed_start(self): 17 self.assertFalse( 18 src.microwave.microwave( 19 closed_door=True, 20 set_timer=True, 21 pressed_start=False, 22 ) 23 ) 24 25 def test_open_door_pressed_start(self):the test is still green.
microwave( closed_door=True, set_timer=True, pressed_start=True ) -> True microwave( closed_door=True, set_timer=True, pressed_start=False ) -> FalseI change the name of the test from test_closed_door_not_pressed_start to test_set_timer_closed_door_not_pressed_start
7 def test_set_timer_closed_door_pressed_start(self): 8 self.assertTrue( 9 src.microwave.microwave( 10 closed_door=True, 11 set_timer=True, 12 pressed_start=True, 13 ) 14 ) 15 16 def test_set_timer_closed_door_not_pressed_start(self): 17 self.assertFalse( 18 src.microwave.microwave( 19 closed_door=True, 20 set_timer=True, 21 pressed_start=False, 22 ) 23 )I add a git commit message in the other terminal
git commit -am \ 'add test_set_timer_closed_door_not_pressed_start'
test_not_set_timer_closed_door_pressed_start
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the Microwave door is closed AND the timer is NOT set AND the start button is pressed
door
timer
start button
output
closed
NOT set
pressed
False
16 def test_set_timer_closed_door_not_pressed_start(self): 17 self.assertFalse( 18 src.microwave.microwave( 19 closed_door=True, 20 set_timer=True, 21 pressed_start=False, 22 ) 23 ) 24 25 def test_not_set_timer_closed_door_pressed_start(self): 26 self.assertFalse( 27 src.microwave.microwave( 28 closed_door=True, 29 set_timer=False, 30 pressed_start=True, 31 ) 32 ) 33 34 def test_open_door_pressed_start(self):the terminal is my friend, and shows AssertionError
AssertionError: True is not false
GREEN: make it pass
I add an if statement for
set_timerto the function inmicrowave.py1 def microwave( 2 closed_door=False, pressed_start=False, 3 set_timer=False, 4 ): 5 if not closed_door: 6 return False 7 if not set_timer: 8 return False 9 return pressed_startthe test passes.
microwave( closed_door=True, set_timer=True, pressed_start=True ) -> True microwave( closed_door=True, set_timer=True, pressed_start=False ) -> False microwave( closed_door=True, set_timer=False, pressed_start=True ) -> FalseI add a git commit message in the other terminal
git commit -am \ 'add test_not_set_timer_closed_door_pressed_start'
test_not_set_timer_closed_door_not_pressed_start
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the Microwave door is closed AND the timer is NOT set AND the start button is NOT pressed
door
timer
start button
output
closed
NOT set
NOT pressed
False
25 def test_not_set_timer_closed_door_pressed_start(self): 26 self.assertFalse( 27 src.microwave.microwave( 28 closed_door=True, 29 set_timer=False, 30 pressed_start=True, 31 ) 32 ) 33 34 def test_not_set_timer_closed_door_not_pressed_start(self): 35 self.assertTrue( 36 src.microwave.microwave( 37 closed_door=True, 38 set_timer=False, 39 pressed_start=False, 40 ) 41 ) 42 43 def test_open_door_pressed_start(self):the terminal is my friend, and shows AssertionError
AssertionError: False is not true
GREEN: make it pass
I change assertTrue to assertFalse in test_not_set_timer_closed_door_not_pressed_start
34 def test_not_set_timer_closed_door_not_pressed_start(self): 35 self.assertFalse( 36 src.microwave.microwave( 37 closed_door=True, 38 set_timer=False, 39 pressed_start=False, 40 ) 41 ) 42 43 def test_open_door_pressed_start(self):the test passes.
microwave( closed_door=True, set_timer=True, pressed_start=True ) -> True microwave( closed_door=True, set_timer=True, pressed_start=False ) -> False microwave( closed_door=True, set_timer=False, pressed_start=True ) -> False microwave( closed_door=True, set_timer=False, pressed_start=False ) -> FalseI add a git commit message in the other terminal
git commit -am \ 'add test_not_set_timer_closed_door_not_pressed_start'
test_set_timer_open_door_pressed_start
RED: make it fail
I go back to the terminal where the tests are running
I add
set_timerto the assertion in test_open_door_pressed_start, for if the Microwave door is open AND the timer is set AND the start button is presseddoor
timer
start button
output
open
set
pressed
False
43 def test_open_door_pressed_start(self): 44 self.assertFalse( 45 src.microwave.microwave( 46 closed_door=False, 47 set_timer=True, 48 pressed_start=True, 49 ) 50 ) 51 52 def test_open_door_not_pressed_start(self):the test is still green.
microwave( closed_door=False, set_timer=True, pressed_start=True ) -> FalseI change the name of the test from test_open_door_pressed_start to test_set_timer_open_door_pressed_start
34 def test_not_set_timer_closed_door_not_pressed_start(self): 35 self.assertFalse( 36 src.microwave.microwave( 37 closed_door=True, 38 set_timer=False, 39 pressed_start=False, 40 ) 41 ) 42 43 def test_set_timer_open_door_pressed_start(self): 44 self.assertFalse( 45 src.microwave.microwave( 46 closed_door=False, 47 set_timer=True, 48 pressed_start=True, 49 ) 50 ) 51 52 def test_open_door_not_pressed_start(self):I add a git commit message in the other terminal
git commit -am \ 'add test_set_timer_open_door_pressed_start'
test_set_timer_open_door_not_pressed_start
I go back to the terminal where the tests are running
I add a value for
set_timerto the assertion in test_open_door_not_pressed_start for if the Microwave door is open AND the timer is set AND the start button is NOT presseddoor
timer
start button
output
open
set
NOT pressed
False
52 def test_open_door_not_pressed_start(self): 53 self.assertFalse( 54 src.microwave.microwave( 55 closed_door=False, 56 set_timer=True, 57 pressed_start=False, 58 ) 59 ) 60 61 62# Exceptions seenthe test is still green.
microwave( closed_door=False, set_timer=True, pressed_start=True ) -> False microwave( closed_door=False, set_timer=False, pressed_start=True ) -> FalseI change the name of the test from test_open_door_not_pressed_start to test_set_timer_open_door_not_pressed_start
43 def test_set_timer_open_door_pressed_start(self): 44 self.assertFalse( 45 src.microwave.microwave( 46 closed_door=False, 47 set_timer=True, 48 pressed_start=True, 49 ) 50 ) 51 52 def test_set_timer_open_door_not_pressed_start(self): 53 self.assertFalse( 54 src.microwave.microwave( 55 closed_door=False, 56 set_timer=True, 57 pressed_start=False, 58 ) 59 )I add a git commit message in the other terminal
git commit -am \ 'add test_set_timer_open_door_not_pressed_start'
test_not_set_timer_open_door_pressed_start
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the Microwave door is open AND the timer is NOT set AND the start button is pressed
door
timer
start button
output
open
NOT set
pressed
False
52 def test_set_timer_open_door_not_pressed_start(self): 53 self.assertFalse( 54 src.microwave.microwave( 55 closed_door=False, 56 set_timer=True, 57 pressed_start=False, 58 ) 59 ) 60 61 def test_not_set_timer_open_door_pressed_start(self): 62 self.assertTrue( 63 src.microwave.microwave( 64 closed_door=False, 65 set_timer=False, 66 pressed_start=True, 67 ) 68 ) 69 70 71# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: False is not true
GREEN: make it pass
I change assertTrue to assertFalse in test_not_set_timer_open_door_pressed_start
61 def test_not_set_timer_open_door_pressed_start(self): 62 self.assertFalse( 63 src.microwave.microwave( 64 closed_door=False, 65 set_timer=False, 66 pressed_start=True, 67 ) 68 ) 69 70 71# Exceptions seenthe test passes.
microwave( closed_door=False, set_timer=True, pressed_start=True ) -> False microwave( closed_door=False, set_timer=False, pressed_start=True ) -> False microwave( closed_door=False, set_timer=False, pressed_start=True ) -> FalseI add a git commit message in the other terminal
git commit -am \ 'add test_not_set_timer_open_door_pressed_start'
test_not_set_timer_open_door_not_pressed_start
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the Microwave door is open AND the timer is NOT set AND the start button is NOT pressed
door
timer
start button
output
open
NOT set
NOT pressed
False
61 def test_not_set_timer_open_door_pressed_start(self): 62 self.assertFalse( 63 src.microwave.microwave( 64 closed_door=False, 65 set_timer=False, 66 pressed_start=True, 67 ) 68 ) 69 70 def test_not_set_timer_open_door_not_pressed_start(self): 71 self.assertTrue( 72 src.microwave.microwave( 73 closed_door=False, 74 set_timer=False, 75 pressed_start=False, 76 ) 77 ) 78 79 80# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: False is not true
GREEN: make it pass
I change assertTrue to assertFalse in test_not_set_timer_open_door_not_pressed_start
70 def test_not_set_timer_open_door_not_pressed_start(self): 71 self.assertFalse( 72 src.microwave.microwave( 73 closed_door=False, 74 set_timer=False, 75 pressed_start=False, 76 ) 77 ) 78 79 80# Exceptions seenthe test passes.
microwave( closed_door=False, set_timer=True, pressed_start=True ) -> False microwave( closed_door=False, set_timer=False, pressed_start=True ) -> False microwave( closed_door=False, set_timer=False, pressed_start=True ) -> False microwave( closed_door=False, set_timer=False, pressed_start=False ) -> FalseI add a git commit message in the other terminal
git commit -am \ 'add test_not_set_timer_open_door_not_pressed_start'
When the microwave function is called, it checks if the Microwave door is closed
If the Microwave door is open, it returns False
microwave( closed_door=False, set_timer=True, pressed_start=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, ): └── if not closed_door: └── return False if not set_timer: return False return pressed_startmicrowave( closed_door=False, set_timer=False, pressed_start=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, ): └── if not closed_door: └── return False if not set_timer: return False return pressed_startmicrowave( closed_door=False, set_timer=False, pressed_start=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, ): └── if not closed_door: └── return False if not set_timer: return False return pressed_startmicrowave( closed_door=False, set_timer=False, pressed_start=False ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, ): └── if not closed_door: └── return False if not set_timer: return False return pressed_startIf the Microwave door is closed, it checks if the timer is set
If the timer is NOT set, it returns False
microwave( closed_door=True, set_timer=False, pressed_start=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, ): ├── if not closed_door: │ return False └── if not set_timer: └── return False return pressed_startmicrowave( closed_door=True, set_timer=False, pressed_start=False ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, ): ├── if not closed_door: │ return False └── if not set_timer: └── return False return pressed_startIf the timer is set, it returns the value of
pressed_startIf the start button is NOT pressed, it returns False
microwave( closed_door=True, set_timer=True, pressed_start=False ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, ): ├── if not closed_door: │ return False ├── if not set_timer: │ return False └── return pressed_start return FalseIf the Microwave door is closed AND the timer is set AND the start button is pressed, it returns True
microwave( closed_door=True, set_timer=True, pressed_start=True ) -> True └── def microwave( closed_door=False, pressed_start=False, set_timer=False, ): ├── if not closed_door: │ return False ├── if not set_timer: │ return False └── return pressed_start return True
The truth table for the Microwave is
door |
timer |
start button |
output |
|---|---|---|---|
closed |
set |
pressed |
True |
closed |
set |
NOT pressed |
False |
closed |
NOT set |
pressed |
False |
closed |
NOT set |
NOT pressed |
False |
door |
timer |
start button |
output |
|---|---|---|---|
open |
set |
pressed |
False |
open |
set |
NOT pressed |
False |
open |
NOT set |
pressed |
False |
open |
NOT set |
NOT pressed |
False |
I want to add a failsafe to stop the Microwave if it gets too hot. It will only heat up food when the door is closed AND the timer is set AND the start button is pressed and it is NOT too hot. The inputs for the Microwave will then be
is the Microwave door closed?
is the timer set?
was the start button pressed?
is the microwave too hot?
test_too_hot_w_not_set_timer_open_door_not_pressed_start
The truth table when the Microwave door is open AND the timer is NOT set AND the start button is NOT pressed is
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
NOT set |
NOT pressed |
too hot |
False |
open |
NOT set |
NOT pressed |
NOT too hot |
False |
RED: make it fail
I go back to the terminal where the tests are running
I add a value for
too_hotto the assertion in test_not_set_timer_open_door_not_pressed_start for if the Microwave door is open AND the timer is NOT set AND the start button is NOT pressed AND the Microwave temperature is NOT too hotdoor
timer
start button
too hot
output
open
NOT set
NOT pressed
NOT too hot
False
70 def test_not_set_timer_open_door_not_pressed_start(self): 71 self.assertFalse( 72 src.microwave.microwave( 73 closed_door=False, 74 set_timer=False, 75 pressed_start=False, 76 too_hot=False, 77 ) 78 ) 79 80 81# Exceptions seenthe terminal shows TypeError
TypeError: microwave() got an unexpected keyword argument 'too_hot'because the test called the
microwavefunction with a name (too_hot) that is not in the parentheses of its definition.
GREEN: make it pass
I add too_hot to the microwave function definition in microwave.py
1def microwave(
2 closed_door=False, pressed_start=False,
3 set_timer=False, too_hot=False,
4 ):
the test passes.
microwave(
closed_door=False, set_timer=False,
pressed_start=False, too_hot=False
) -> False
REFACTOR: make it better
I add an assertion for if the Microwave door is open AND the timer is NOT set AND the start button is NOT pressed AND the Microwave temperature is too hot, in
test_microwave.pydoor
timer
start button
too hot
output
open
NOT set
NOT pressed
too hot
False
70 def test_not_set_timer_open_door_not_pressed_start(self): 71 self.assertTrue( 72 src.microwave.microwave( 73 closed_door=False, 74 set_timer=False, 75 pressed_start=False, 76 too_hot=True, 77 ) 78 ) 79 self.assertFalse( 80 src.microwave.microwave( 81 closed_door=False, 82 set_timer=False, 83 pressed_start=False, 84 too_hot=False, 85 ) 86 ) 87 88 89# Exceptions seen 90# AssertionError 91# NameError 92# AttributeError 93# TypeError 94# SyntaxErrorthe terminal is my friend, and shows AssertionError
AssertionError: False is not trueI change assertTrue to assertFalse in test_not_set_timer_open_door_not_pressed_start
70 def test_not_set_timer_open_door_not_pressed_start(self): 71 self.assertFalse( 72 src.microwave.microwave( 73 closed_door=False, 74 set_timer=False, 75 pressed_start=False, 76 too_hot=True, 77 ) 78 )the test passes.
microwave( closed_door=False, set_timer=False, pressed_start=False, too_hot=True ) -> False microwave( closed_door=False, set_timer=False, pressed_start=False, too_hot=False ) -> FalseI change the name of the test from test_not_set_timer_open_door_not_pressed_start to test_too_hot_w_not_set_timer_open_door_not_pressed_start
61 def test_not_set_timer_open_door_pressed_start(self): 62 self.assertFalse( 63 src.microwave.microwave( 64 closed_door=False, 65 set_timer=False, 66 pressed_start=True, 67 ) 68 ) 69 70 def test_too_hot_w_not_set_timer_open_door_not_pressed_start(self): 71 self.assertFalse( 72 src.microwave.microwave( 73 closed_door=False, 74 set_timer=False, 75 pressed_start=False, 76 too_hot=True, 77 ) 78 )I add a git commit message in the other terminal
git commit -am \ 'add test_too_hot_w_not_set_timer_open_door_not_pressed_start'
test_too_hot_w_not_set_timer_open_door_pressed_start
The truth table when the Microwave door is open AND the timer is NOT set AND the start button is pressed is
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
NOT set |
pressed |
too hot |
False |
open |
NOT set |
pressed |
NOT too hot |
False |
RED: make it fail
I go back to the terminal where the tests are running
I add a value for
too_hotto the assertion in test_not_set_timer_open_door_pressed_start for if the Microwave door is open AND the timer is NOT set AND the start button is pressed AND the Microwave temperature is NOT too hotdoor
timer
start button
too hot
output
open
NOT set
pressed
NOT too hot
False
61 def test_not_set_timer_open_door_pressed_start(self): 62 self.assertFalse( 63 src.microwave.microwave( 64 closed_door=False, 65 set_timer=False, 66 pressed_start=True, 67 too_hot=False, 68 ) 69 ) 70 71 def test_too_hot_w_not_set_timer_open_door_not_pressed_start(self):the test is still green.
microwave( closed_door=False, set_timer=False, pressed_start=True, too_hot=False ) -> False microwave( closed_door=False, set_timer=False, pressed_start=False, too_hot=True ) -> False microwave( closed_door=False, set_timer=False, pressed_start=False, too_hot=False ) -> FalseI add an assertion for if the Microwave door is open AND the timer is NOT set AND the start button is pressed AND the Microwave temperature is too hot, in
test_microwave.pydoor
timer
start button
too hot
output
open
NOT set
pressed
too hot
False
61 def test_not_set_timer_open_door_pressed_start(self): 62 self.assertTrue( 63 src.microwave.microwave( 64 closed_door=False, 65 set_timer=False, 66 pressed_start=True, 67 too_hot=True, 68 ) 69 ) 70 self.assertFalse( 71 src.microwave.microwave( 72 closed_door=False, 73 set_timer=False, 74 pressed_start=True, 75 too_hot=False, 76 ) 77 ) 78 79 def test_too_hot_w_not_set_timer_open_door_not_pressed_start(self):the terminal is my friend, and shows AssertionError
AssertionError: False is not true
GREEN: make it pass
I change assertTrue to assertFalse in test_not_set_timer_open_door_pressed_start
70 def test_not_set_timer_open_door_pressed_start(self):
71 self.assertFalse(
72 src.microwave.microwave(
73 closed_door=False,
74 set_timer=False,
75 pressed_start=True,
76 too_hot=True,
77 )
78 )
the test passes.
microwave(
closed_door=False, set_timer=False,
pressed_start=True, too_hot=True
) -> False
microwave(
closed_door=False, set_timer=False,
pressed_start=True, too_hot=False
) -> False
microwave(
closed_door=False, set_timer=False,
pressed_start=False, too_hot=True
) -> False
microwave(
closed_door=False, set_timer=False,
pressed_start=False, too_hot=False
) -> False
REFACTOR: make it better
I change the name of the test from test_not_set_timer_open_door_pressed_start to test_too_hot_w_not_set_timer_open_door_pressed_start
52 def test_set_timer_open_door_not_pressed_start(self): 53 self.assertFalse( 54 src.microwave.microwave( 55 closed_door=False, 56 set_timer=True, 57 pressed_start=False, 58 ) 59 ) 60 61 def test_too_hot_w_not_set_timer_open_door_pressed_start(self): 62 self.assertFalse( 63 src.microwave.microwave( 64 closed_door=False, 65 set_timer=False, 66 pressed_start=True, 67 too_hot=True, 68 ) 69 )I add a git commit message in the other terminal
git commit -am \ 'add test_too_hot_w_not_set_timer_open_door_pressed_start'
test_too_hot_w_set_timer_open_door_not_pressed_start
The truth table when the Microwave door is open AND the timer is set AND the start button is NOT pressed is
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
set |
NOT pressed |
too hot |
False |
open |
set |
NOT pressed |
NOT too hot |
False |
RED: make it fail
I go back to the terminal where the tests are running
I add a value for
too_hotto the assertion in test_set_timer_open_door_not_pressed_start for if the Microwave door is open AND the timer is set AND the start button is NOT pressed AND the Microwave temperature is NOT too hotdoor
timer
start button
too hot
output
open
set
NOT pressed
NOT too hot
False
52 def test_set_timer_open_door_not_pressed_start(self): 53 self.assertFalse( 54 src.microwave.microwave( 55 closed_door=False, 56 set_timer=True, 57 pressed_start=False, 58 too_hot=False, 59 ) 60 ) 61 62 def test_too_hot_w_not_set_timer_open_door_pressed_start(self):the test is still green.
microwave( closed_door=False, set_timer=True, pressed_start=False, too_hot=False ) -> FalseI add an assertion for if the Microwave door is open AND the timer is set AND the start button is NOT pressed AND the Microwave temperature is too hot, in
test_microwave.pydoor
timer
start button
too hot
output
open
set
NOT pressed
too hot
False
52 def test_set_timer_open_door_not_pressed_start(self): 53 self.assertTrue( 54 src.microwave.microwave( 55 closed_door=False, 56 set_timer=True, 57 pressed_start=False, 58 too_hot=True, 59 ) 60 ) 61 self.assertFalse( 62 src.microwave.microwave( 63 closed_door=False, 64 set_timer=True, 65 pressed_start=False, 66 too_hot=False, 67 ) 68 ) 69 70 def test_too_hot_w_not_set_timer_open_door_pressed_start(self):the terminal is my friend, and shows AssertionError
AssertionError: False is not true
GREEN: make it pass
I change assertTrue to assertFalse in test_set_timer_open_door_not_pressed_start
52 def test_set_timer_open_door_not_pressed_start(self):
53 self.assertFalse(
54 src.microwave.microwave(
55 closed_door=False,
56 set_timer=True,
57 pressed_start=False,
58 too_hot=True,
59 )
60 )
the test passes.
microwave(
closed_door=False, set_timer=True,
pressed_start=False, too_hot=True
) -> False
microwave(
closed_door=False, set_timer=True,
pressed_start=False, too_hot=False
) -> False
REFACTOR: make it better
I change the name of the test from test_set_timer_open_door_not_pressed_start to test_too_hot_w_set_timer_open_door_not_pressed_start
43 def test_set_timer_open_door_pressed_start(self): 44 self.assertFalse( 45 src.microwave.microwave( 46 closed_door=False, 47 set_timer=True, 48 pressed_start=True, 49 ) 50 ) 51 52 def test_too_hot_w_set_timer_open_door_not_pressed_start(self): 53 self.assertFalse( 54 src.microwave.microwave( 55 closed_door=False, 56 set_timer=True, 57 pressed_start=False, 58 too_hot=True, 59 ) 60 )I add a git commit message in the other terminal
git commit -am \ 'add test_too_hot_w_set_timer_open_door_not_pressed_start'
test_too_hot_w_set_timer_open_door_pressed_start
The truth table when the Microwave door is open AND the timer is set AND the start button is pressed is
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
set |
pressed |
too hot |
False |
open |
set |
pressed |
NOT too hot |
False |
RED: make it fail
I go back to the terminal where the tests are running
I add a value for
too_hotto the assertion in test_set_timer_open_door_pressed_start for if the Microwave door is open AND the timer is set AND the start button is pressed AND the Microwave temperature is NOT too hotdoor
timer
start button
too hot
output
open
set
pressed
NOT too hot
False
43 def test_set_timer_open_door_pressed_start(self): 44 self.assertFalse( 45 src.microwave.microwave( 46 closed_door=False, 47 set_timer=True, 48 pressed_start=True, 49 too_hot=False, 50 ) 51 ) 52 53 def test_too_hot_w_set_timer_open_door_not_pressed_startthe test is still green.
microwave( closed_door=False, set_timer=True, pressed_start=True, too_hot=False ) -> False microwave( closed_door=False, set_timer=True, pressed_start=False, too_hot=True ) -> False microwave( closed_door=False, set_timer=True, pressed_start=False, too_hot=False ) -> FalseI add an assertion for if the Microwave door is open AND the timer is set AND the start button is pressed AND the Microwave temperature is too hot, in
test_microwave.pydoor
timer
start button
too hot
output
open
set
pressed
too hot
False
43 def test_set_timer_open_door_pressed_start(self): 44 self.assertTrue( 45 src.microwave.microwave( 46 closed_door=False, 47 set_timer=True, 48 pressed_start=True, 49 too_hot=True, 50 ) 51 ) 52 self.assertFalse( 53 src.microwave.microwave( 54 closed_door=False, 55 set_timer=True, 56 pressed_start=True, 57 too_hot=False, 58 ) 59 ) 60 61 def test_too_hot_w_set_timer_open_door_not_pressed_start(self):the terminal is my friend, and shows AssertionError
AssertionError: False is not true
GREEN: make it pass
I change assertTrue to assertFalse in test_set_timer_open_door_pressed_start
43 def test_set_timer_open_door_pressed_start(self):
44 self.assertFalse(
45 src.microwave.microwave(
46 closed_door=False,
47 set_timer=True,
48 pressed_start=True,
49 too_hot=True,
50 )
51 )
the test passes.
microwave(
closed_door=False, set_timer=True,
pressed_start=True, too_hot=True
) -> False
microwave(
closed_door=False, set_timer=True,
pressed_start=True, too_hot=False
) -> False
microwave(
closed_door=False, set_timer=True,
pressed_start=False, too_hot=True
) -> False
microwave(
closed_door=False, set_timer=True,
pressed_start=False, too_hot=False
) -> False
REFACTOR: make it better
I change the name of the test from test_set_timer_open_door_pressed_start to test_too_hot_w_set_timer_open_door_pressed_start
34 def test_set_timer_closed_door_not_pressed_start(self): 35 self.assertFalse( 36 src.microwave.microwave( 37 closed_door=True, 38 set_timer=False, 39 pressed_start=False, 40 ) 41 ) 42 43 def test_too_hot_w_set_timer_open_door_pressed_start(self): 44 self.assertFalse( 45 src.microwave.microwave( 46 closed_door=False, 47 set_timer=True, 48 pressed_start=True, 49 too_hot=True, 50 ) 51 )I add a git commit message in the other terminal
git commit -am \ 'add test_too_hot_w_set_timer_open_door_pressed_start'
test_too_hot_w_not_set_timer_closed_door_not_pressed_start
The truth table when the Microwave door is closed AND the timer is NOT set AND the start button is NOT pressed is
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
closed |
NOT set |
NOT pressed |
too hot |
False |
closed |
NOT set |
NOT pressed |
NOT too hot |
False |
RED: make it fail
I go back to the terminal where the tests are running
I add a value for
too_hotto the assertion in test_not_set_timer_closed_door_not_pressed_start for if the Microwave door is closed AND the timer is NOT set AND the start button is NOT pressed AND the Microwave temperature is NOT too hotdoor
timer
start button
too hot
output
closed
NOT set
NOT pressed
NOT too hot
False
34 def test_not_set_timer_closed_door_not_pressed_start(self): 35 self.assertFalse( 36 src.microwave.microwave( 37 closed_door=True, 38 set_timer=False, 39 pressed_start=False, 40 too_hot=False, 41 ) 42 ) 43 44 def test_set_timer_open_door_pressed_start(self):the test is still green.
microwave( closed_door=True, set_timer=False, pressed_start=False, too_hot=False ) -> FalseI add an assertion for if the Microwave door is closed AND the timer is NOT set AND the start button is NOT pressed AND the Microwave temperature is too hot, in
test_microwave.pydoor
timer
start button
too hot
output
closed
NOT set
NOT pressed
too hot
False
34 def test_not_set_timer_closed_door_not_pressed_start(self): 35 self.assertTrue( 36 src.microwave.microwave( 37 closed_door=True, 38 set_timer=False, 39 pressed_start=False, 40 too_hot=True, 41 ) 42 ) 43 self.assertFalse( 44 src.microwave.microwave( 45 closed_door=True, 46 set_timer=False, 47 pressed_start=False, 48 too_hot=False, 49 ) 50 ) 51 52 def test_set_timer_open_door_pressed_start(self):the terminal is my friend, and shows AssertionError
AssertionError: False is not true
GREEN: make it pass
I change assertTrue to assertFalse in test_not_set_timer_closed_door_not_pressed_start
34 def test_not_set_timer_closed_door_not_pressed_start(self):
35 self.assertFalse(
36 src.microwave.microwave(
37 closed_door=True,
38 set_timer=False,
39 pressed_start=False,
40 too_hot=True,
41 )
42 )
the test passes.
microwave(
closed_door=True, set_timer=False,
pressed_start=False, too_hot=True
) -> False
microwave(
closed_door=True, set_timer=False,
pressed_start=False, too_hot=False
) -> False
REFACTOR: make it better
I change the name of the test from test_not_set_timer_closed_door_not_pressed_start to test_too_hot_w_not_set_timer_closed_door_not_pressed_start
25 def test_not_set_timer_closed_door_pressed_start(self): 26 self.assertFalse( 27 src.microwave.microwave( 28 closed_door=True, 29 set_timer=False, 30 pressed_start=True, 31 ) 32 ) 33 34 def test_too_hot_w_not_set_timer_closed_door_not_pressed_start(self): 35 self.assertFalse( 36 src.microwave.microwave( 37 closed_door=True, 38 set_timer=False, 39 pressed_start=False, 40 too_hot=True, 41 ) 42 )I add a git commit message in the other terminal
git commit -am \ 'add test_too_hot_w_not_set_timer_closed_door_not_pressed_start'
test_too_hot_w_not_set_timer_closed_door_pressed_start
The truth table when the Microwave door is closed AND the timer is NOT set AND the start button is pressed is
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
closed |
NOT set |
pressed |
too hot |
False |
closed |
NOT set |
pressed |
NOT too hot |
False |
RED: make it fail
I go back to the terminal where the tests are running
I add a value for
too_hotto the assertion in test_not_set_timer_closed_door_pressed_start for if the Microwave door is closed AND the timer is NOT set AND the start button is pressed AND the Microwave temperature is NOT too hotdoor
timer
start button
too hot
output
closed
NOT set
pressed
NOT too hot
False
25 def test_not_set_timer_closed_door_pressed_start(self): 26 self.assertFalse( 27 src.microwave.microwave( 28 closed_door=True, 29 set_timer=False, 30 pressed_start=True, 31 too_hot=False, 32 ) 33 ) 34 35 def test_too_hot_w_not_set_timer_closed_door_not_pressed_start(self):the test is still green.
microwave( closed_door=True, set_timer=False, pressed_start=True, too_hot=False ) -> False microwave( closed_door=True, set_timer=False, pressed_start=False, too_hot=True ) -> False microwave( closed_door=True, set_timer=False, pressed_start=False, too_hot=False ) -> FalseI add an assertion for if the Microwave door is closed AND the timer is NOT set AND the start button is pressed AND the Microwave temperature is too hot, in
test_microwave.pydoor
timer
start button
too hot
output
closed
NOT set
pressed
too hot
False
25 def test_not_set_timer_closed_door_pressed_start(self): 26 self.assertTrue( 27 src.microwave.microwave( 28 closed_door=True, 29 set_timer=False, 30 pressed_start=True, 31 too_hot=True, 32 ) 33 ) 34 self.assertFalse( 35 src.microwave.microwave( 36 closed_door=True, 37 set_timer=False, 38 pressed_start=True, 39 too_hot=False, 40 ) 41 ) 42 43 def test_too_hot_w_not_set_timer_closed_door_not_pressed_start(self):the terminal is my friend, and shows AssertionError
AssertionError: False is not true
GREEN: make it pass
I change assertTrue to assertFalse in test_not_set_timer_closed_door_pressed_start
25 def test_not_set_timer_closed_door_pressed_start(self):
26 self.assertFalse(
27 src.microwave.microwave(
28 closed_door=True,
29 set_timer=False,
30 pressed_start=True,
31 too_hot=True,
32 )
33 )
the test passes.
microwave(
closed_door=True, set_timer=False,
pressed_start=True, too_hot=True
) -> False
microwave(
closed_door=True, set_timer=False,
pressed_start=True, too_hot=False
) -> False
microwave(
closed_door=True, set_timer=False,
pressed_start=False, too_hot=True
) -> False
microwave(
closed_door=True, set_timer=False,
pressed_start=False, too_hot=False
) -> False
REFACTOR: make it better
I change the name of the test from test_not_set_timer_closed_door_pressed_start to test_too_hot_w_not_set_timer_closed_door_pressed_start
16 def test_set_timer_closed_door_not_pressed_start(self): 17 self.assertFalse( 18 src.microwave.microwave( 19 closed_door=True, 20 set_timer=True, 21 pressed_start=False, 22 ) 23 ) 24 25 def test_too_hot_w_not_set_timer_closed_door_pressed_start(self): 26 self.assertFalse( 27 src.microwave.microwave( 28 closed_door=True, 29 set_timer=False, 30 pressed_start=True, 31 too_hot=True, 32 ) 33 )I add a git commit message in the other terminal
git commit -am \ 'add test_too_hot_w_not_set_timer_closed_door_pressed_start'
test_too_hot_w_set_timer_closed_door_not_pressed_start
The truth table when the Microwave door is closed AND the timer is set AND the start button is NOT pressed is
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
closed |
set |
NOT pressed |
too hot |
False |
closed |
set |
NOT pressed |
NOT too hot |
False |
RED: make it fail
I go back to the terminal where the tests are running
I add a value for
too_hotto the assertion in test_set_timer_closed_door_not_pressed_start for if the Microwave door is closed AND the timer is set AND the start button is NOT pressed AND the Microwave temperature is NOT too hotdoor
timer
start button
too hot
output
closed
set
NOT pressed
NOT too hot
False
16 def test_set_timer_closed_door_not_pressed_start(self): 17 self.assertFalse( 18 src.microwave.microwave( 19 closed_door=True, 20 set_timer=True, 21 pressed_start=False, 22 too_hot=False, 23 ) 24 ) 25 26 def test_too_hot_w_not_set_timer_closed_door_pressed_start(self):the test is still green.
microwave( closed_door=True, set_timer=True, pressed_start=False, too_hot=False ) -> FalseI add an assertion for if the Microwave door is closed AND the timer is set AND the start button is NOT pressed AND the Microwave temperature is too hot, in
test_microwave.pydoor
timer
start button
too hot
output
closed
set
NOT pressed
too hot
False
16 def test_set_timer_closed_door_not_pressed_start(self): 17 self.assertTrue( 18 src.microwave.microwave( 19 closed_door=True, 20 set_timer=True, 21 pressed_start=False, 22 too_hot=True, 23 ) 24 ) 25 self.assertFalse( 26 src.microwave.microwave( 27 closed_door=True, 28 set_timer=True, 29 pressed_start=False, 30 too_hot=False, 31 ) 32 ) 33 34 def test_too_hot_w_not_set_timer_closed_door_pressed_start(self):the terminal is my friend, and shows AssertionError
AssertionError: False is not true
GREEN: make it pass
I change assertTrue to assertFalse in test_set_timer_closed_door_not_pressed_start
16 def test_set_timer_closed_door_not_pressed_start(self):
17 self.assertFalse(
18 src.microwave.microwave(
19 closed_door=True,
20 set_timer=True,
21 pressed_start=False,
22 too_hot=True,
23 )
24 )
the test passes.
microwave(
closed_door=True, set_timer=True,
pressed_start=False, too_hot=True
) -> False
microwave(
closed_door=True, set_timer=True,
pressed_start=False, too_hot=False
) -> False
REFACTOR: make it better
I change the name of the test from test_set_timer_closed_door_not_pressed_start to test_too_hot_w_set_timer_closed_door_not_pressed_start
7 def test_set_timer_closed_door_pressed_start(self): 8 self.assertTrue( 9 src.microwave.microwave( 10 closed_door=True, 11 set_timer=True, 12 pressed_start=True, 13 ) 14 ) 15 16 def test_too_hot_w_set_timer_closed_door_not_pressed_start(self): 17 self.assertFalse( 18 src.microwave.microwave( 19 closed_door=True, 20 set_timer=True, 21 pressed_start=False, 22 too_hot=True, 23 ) 24 )I add a git commit message in the other terminal
git commit -am \ 'add test_too_hot_w_set_timer_closed_door_not_pressed_start'
test_too_hot_w_set_timer_closed_door_pressed_start
The truth table when the Microwave door is closed AND the timer is set AND the start button is pressed is
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
closed |
set |
pressed |
too hot |
False |
closed |
set |
pressed |
NOT too hot |
True |
RED: make it fail
I go back to the terminal where the tests are running
I add a value for
too_hotto the assertion in test_set_timer_closed_door_pressed_start for if the Microwave door is closed AND the timer is set AND the start button is pressed AND the Microwave temperature is NOT too hotdoor
timer
start button
too hot
output
closed
set
pressed
NOT too hot
True
7 def test_set_timer_closed_door_pressed_start(self): 8 self.assertTrue( 9 src.microwave.microwave( 10 closed_door=True, 11 set_timer=True, 12 pressed_start=True, 13 too_hot=False, 14 ) 15 ) 16 17 def test_too_hot_w_set_timer_closed_door_not_pressed_start(self):the test is still green.
microwave( closed_door=True, set_timer=True, pressed_start=True, too_hot=False ) -> True microwave( closed_door=True, set_timer=True, pressed_start=False, too_hot=True ) -> False microwave( closed_door=True, set_timer=True, pressed_start=False, too_hot=False ) -> FalseI add an assertion for if the Microwave door is closed AND the timer is set AND the start button is pressed AND the Microwave temperature is too hot, in
test_microwave.pydoor
timer
start button
too hot
output
closed
set
pressed
too hot
False
7 def test_set_timer_closed_door_pressed_start(self): 8 self.assertFalse( 9 src.microwave.microwave( 10 closed_door=True, 11 set_timer=True, 12 pressed_start=True, 13 too_hot=True, 14 ) 15 ) 16 self.assertTrue( 17 src.microwave.microwave( 18 closed_door=True, 19 set_timer=True, 20 pressed_start=True, 21 too_hot=False, 22 ) 23 ) 24 25 def test_too_hot_w_set_timer_closed_door_not_pressed_start(self):the terminal is my friend, and shows AssertionError
AssertionError: True is not false
GREEN: make it pass
I add an if statement for the too_hot parameter in microwave.py
1def microwave(
2 closed_door=False, pressed_start=False,
3 set_timer=False, too_hot=False,
4 ):
5 if not closed_door:
6 return False
7 if not set_timer:
8 return False
9 if too_hot:
10 return False
11 return pressed_start
the test passes.
microwave(
closed_door=True, set_timer=True,
pressed_start=True, too_hot=True
) -> False
microwave(
closed_door=True, set_timer=True,
pressed_start=True, too_hot=False
) -> True
microwave(
closed_door=True, set_timer=True,
pressed_start=False, too_hot=True
) -> False
microwave(
closed_door=True, set_timer=True,
pressed_start=False, too_hot=False
) -> False
REFACTOR: make it better
I change the name of the test from test_set_timer_closed_door_pressed_start to test_too_hot_w_set_timer_closed_door_pressed_start, in
test_microwave.py5class TestMicrowave(unittest.TestCase): 6 7 def test_too_hot_w_set_timer_closed_door_pressed_start(self): 8 self.assertFalse( 9 src.microwave.microwave( 10 closed_door=True, 11 set_timer=True, 12 pressed_start=True, 13 too_hot=True, 14 ) 15 )I add a git commit message in the other terminal
git commit -am \ 'add test_too_hot_w_set_timer_closed_door_pressed_start'
When the microwave function is called, it checks if the Microwave door is closed
If the Microwave door is open, it returns False
microwave( closed_door=False, set_timer=False, pressed_start=True, too_hot=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): └── if not closed_door: └── return False if not set_timer: return False if too_hot: return False return pressed_startmicrowave( closed_door=False, set_timer=False, pressed_start=True, too_hot=False ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): └── if not closed_door: └── return False if not set_timer: return False if too_hot: return False return pressed_startmicrowave( closed_door=False, set_timer=False, pressed_start=False, too_hot=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): └── if not closed_door: └── return False if not set_timer: return False if too_hot: return False return pressed_startmicrowave( closed_door=False, set_timer=False, pressed_start=False, too_hot=False ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): └── if not closed_door: └── return False if not set_timer: return False if too_hot: return False return pressed_startmicrowave( closed_door=False, set_timer=True, pressed_start=True, too_hot=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): └── if not closed_door: └── return False if not set_timer: return False if too_hot: return False return pressed_startmicrowave( closed_door=False, set_timer=True, pressed_start=True, too_hot=False ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): └── if not closed_door: └── return False if not set_timer: return False if too_hot: return False return pressed_startmicrowave( closed_door=False, set_timer=True, pressed_start=False, too_hot=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): └── if not closed_door: └── return False if not set_timer: return False if too_hot: return False return pressed_startmicrowave( closed_door=False, set_timer=True, pressed_start=False, too_hot=False ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): └── if not closed_door: └── return False if not set_timer: return False if too_hot: return False return pressed_startIf the Microwave door is closed, it checks if the timer is set
If the timer is NOT set, it returns False
microwave( closed_door=True, set_timer=False, pressed_start=True, too_hot=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): ├── if not closed_door: │ return False └── if not set_timer: └── return False if too_hot: return False return pressed_startmicrowave( closed_door=True, set_timer=False, pressed_start=True, too_hot=False ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): ├── if not closed_door: │ return False └── if not set_timer: └── return False if too_hot: return False return pressed_startmicrowave( closed_door=True, set_timer=False, pressed_start=False, too_hot=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): ├── if not closed_door: │ return False └── if not set_timer: └── return False if too_hot: return False return pressed_startmicrowave( closed_door=True, set_timer=False, pressed_start=False, too_hot=False ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): ├── if not closed_door: │ return False └── if not set_timer: └── return False if too_hot: return False return pressed_startIf the timer is set, it checks if the Microwave temperature is too hot
If the Microwave temperature is too hot, it returns False
microwave( closed_door=True, set_timer=True, pressed_start=True, too_hot=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): ├── if not closed_door: │ return False ├── if not set_timer: │ return False └── if too_hot: └── return False return pressed_startmicrowave( closed_door=True, set_timer=True, pressed_start=False, too_hot=True ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): ├── if not closed_door: │ return False ├── if not set_timer: │ return False └── if too_hot: └── return False return pressed_startIf the Microwave temperature is NOT too hot, it returns the value of
pressed_startIf the start button is NOT pressed, it returns False
microwave( closed_door=True, set_timer=True, pressed_start=False, too_hot=False ) -> False └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): ├── if not closed_door: │ return False ├── if not set_timer: │ return False ├── if too_hot: │ return False └── return pressed_start return FalseIf the Microwave door is closed AND the timer is set AND the Microwave temperature is NOT too hot AND the start button is pressed, it returns True
microwave( closed_door=True, set_timer=True, pressed_start=True, too_hot=False ) -> True └── def microwave( closed_door=False, pressed_start=False, set_timer=False, too_hot=False, ): ├── if not closed_door: │ return False ├── if not set_timer: │ return False ├── if too_hot: │ return False └── return pressed_start return True
refactor microwave function
I go back to the terminal where the tests are running
I add an if statement for the
pressed_startparameter to make it clearer, inmicrowave.py5 if not closed_door: 6 return False 7 if not set_timer: 8 return False 9 if too_hot: 10 return False 11 if not pressed_start: 12 return False 13 return Truethe tests are still green.
I use Logical Disjunction (OR) to put the if statements together since they all return False
5 # if not closed_door: 6 # return False 7 # if not set_timer: 8 # return False 9 # if too_hot: 10 # return False 11 # if not pressed_start: 12 # return False 13 if ( 14 not closed_door 15 or not set_timer 16 or too_hot 17 or not pressed_start 18 ): 19 return False 20 return Truestill green.
I write the new statement in terms of not since it happens three times
13 # if ( 14 # not closed_door 15 # or not set_timer 16 # or too_hot 17 # or not pressed_start 18 # ): 19 if ( 20 (not closed_door) 21 (not and) (not set_timer) 22 (not and) (not not too_hot) 23 (not and) (not pressed_start) 24 ): 25 return False 26 return Truethe terminal is my friend, and shows SyntaxError
SyntaxError: invalid syntaxI “factor” out the nots
19 # if ( 20 # (not closed_door) 21 # (not and) (not set_timer) 22 # (not and) (not not too_hot) 23 # (not and) (not pressed_start) 24 # ): 25 if not ( 26 closed_door 27 and set_timer 28 and not too_hot 29 and pressed_start 30 ): 31 return False 32 return Truethe tests are green again.
I add an else clause to make it clearer
25 if not ( 26 closed_door 27 and set_timer 28 and not too_hot 29 and pressed_start 30 ): 31 return False 32 else: 33 return Truethe tests are still green.
I use Logical Negation (NOT) to write the else clause in relation to the if statement
32 # else: 33 if not not ( 34 closed_door 35 and set_timer 36 and not too_hot 37 and pressed_start 38 ): 39 return Truestill green.
I cancel out the nots
32 # else: 33 # if not not ( 34 if ( 35 closed_door 36 and set_timer 37 and not too_hot 38 and pressed_start 39 ): 40 return Truegreen.
I add a conditional expression
25 # if not ( 26 # closed_door 27 # and set_timer 28 # and not too_hot 29 # and pressed_start 30 # ): 31 # return False 32 # else: 33 # if not not ( 34 # if ( 35 # closed_door 36 # and set_timer 37 # and not too_hot 38 # and pressed_start 39 # ): 40 # return True 41 return ( 42 closed_door 43 and set_timer 44 and not too_hot 45 and pressed_start 46 )the tests are still green.
I remove the commented lines
1def microwave( 2 closed_door=False, pressed_start=False, 3 set_timer=False, too_hot=False, 4 ): 5 return ( 6 closed_door 7 and set_timer 8 and not too_hot 9 and pressed_start 10 )I add a git commit message in the other terminal
git commit -am 'refactor microwave function'
close the project
review
I ran tests for a Microwave with these inputs:
is the Microwave door closed?
is the timer set?
is the Microwave too hot?
was the start button pressed?
and got this truth table
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
closed |
set |
pressed |
too hot |
False |
closed |
set |
pressed |
NOT too hot |
True |
closed |
set |
NOT pressed |
too hot |
False |
closed |
set |
NOT pressed |
NOT too hot |
False |
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
closed |
NOT set |
pressed |
too hot |
False |
closed |
NOT set |
pressed |
NOT too hot |
False |
closed |
NOT set |
NOT pressed |
too hot |
False |
closed |
NOT set |
NOT pressed |
NOT too hot |
False |
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
set |
pressed |
too hot |
False |
open |
set |
pressed |
NOT too hot |
False |
open |
set |
NOT pressed |
too hot |
False |
open |
set |
NOT pressed |
NOT too hot |
False |
door |
timer |
start button |
too hot |
output |
|---|---|---|---|---|
open |
NOT set |
pressed |
too hot |
False |
open |
NOT set |
pressed |
NOT too hot |
False |
open |
NOT set |
NOT pressed |
too hot |
False |
open |
NOT set |
NOT pressed |
NOT too hot |
False |
The only time this Microwave heats up food is when the Microwave door is closed AND the timer is set AND the Microwave temperature is NOT too hot AND the start button is pressed.
code from the chapter
what is next?
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.