Microwave Controller

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

microwave/tests/test_microwave.py
 1import src.microwave
 2import unittest
 3
 4
 5class TestMicrowave(unittest.TestCase):
 6
 7    def test_too_hot_w_timer_set_closed_door_pressed_start(self):
 8        self.assertFalse(
 9            src.microwave.microwave(
10                closed_door=True,
11                timer_set=True,
12                pressed_start=True,
13                too_hot=True,
14            )
15        )
16        self.assertTrue(
17            src.microwave.microwave(
18                closed_door=True,
19                timer_set=True,
20                pressed_start=True,
21                too_hot=False,
22            )
23        )
microwave/tests/test_microwave.py
25    def test_too_hot_w_timer_set_closed_door_not_pressed_start(self):
26        self.assertFalse(
27            src.microwave.microwave(
28                closed_door=True,
29                timer_set=True,
30                pressed_start=False,
31                too_hot=True,
32            )
33        )
34        self.assertFalse(
35            src.microwave.microwave(
36                closed_door=True,
37                timer_set=True,
38                pressed_start=False,
39                too_hot=False,
40            )
41        )
microwave/tests/test_microwave.py
43    def test_too_hot_w_not_timer_set_closed_door_pressed_start(self):
44        self.assertFalse(
45            src.microwave.microwave(
46                closed_door=True,
47                timer_set=False,
48                pressed_start=True,
49                too_hot=True,
50            )
51        )
52        self.assertFalse(
53            src.microwave.microwave(
54                closed_door=True,
55                timer_set=False,
56                pressed_start=True,
57                too_hot=False,
58            )
59        )
microwave/tests/test_microwave.py
61    def test_too_hot_w_not_timer_set_closed_door_not_pressed_start(self):
62        self.assertFalse(
63            src.microwave.microwave(
64                closed_door=True,
65                timer_set=False,
66                pressed_start=False,
67                too_hot=True,
68            )
69        )
70        self.assertFalse(
71            src.microwave.microwave(
72                closed_door=True,
73                timer_set=False,
74                pressed_start=False,
75                too_hot=False,
76            )
77        )
microwave/tests/test_microwave.py
79    def test_too_hot_w_timer_set_open_door_pressed_start(self):
80        self.assertFalse(
81            src.microwave.microwave(
82                closed_door=False,
83                timer_set=True,
84                pressed_start=True,
85                too_hot=True,
86            )
87        )
88        self.assertFalse(
89            src.microwave.microwave(
90                closed_door=False,
91                timer_set=True,
92                pressed_start=True,
93                too_hot=False,
94            )
95        )
microwave/tests/test_microwave.py
 97    def test_too_hot_w_timer_set_open_door_not_pressed_start(self):
 98        self.assertFalse(
 99            src.microwave.microwave(
100                closed_door=False,
101                timer_set=True,
102                pressed_start=False,
103                too_hot=True,
104            )
105        )
106        self.assertFalse(
107            src.microwave.microwave(
108                closed_door=False,
109                timer_set=True,
110                pressed_start=False,
111                too_hot=False,
112            )
113        )
microwave/tests/test_microwave.py
115    def test_too_hot_w_not_timer_set_open_door_pressed_start(self):
116        self.assertFalse(
117            src.microwave.microwave(
118                closed_door=False,
119                timer_set=False,
120                pressed_start=True,
121                too_hot=True,
122            )
123        )
124        self.assertFalse(
125            src.microwave.microwave(
126                closed_door=False,
127                timer_set=False,
128                pressed_start=True,
129                too_hot=False,
130            )
131        )
microwave/tests/test_microwave.py
133    def test_too_hot_w_not_timer_set_open_door_not_pressed_start(self):
134        self.assertFalse(
135            src.microwave.microwave(
136                closed_door=False,
137                timer_set=False,
138                pressed_start=False,
139                too_hot=True,
140            )
141        )
142        self.assertFalse(
143            src.microwave.microwave(
144                closed_door=False,
145                timer_set=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.sh

    • I change the name of the project to microwave in makePythonTdd.sh

    • I run makePythonTdd.sh in the terminal to make the microwave project

      ./makePythonTdd.sh
      
    • I open makePythonTdd.ps1

    • I change the name of the project to microwave in makePythonTdd.ps1

    • I run makePythonTdd.ps1 in the terminal to make the microwave project

      .\makePythonTdd.ps1
      

    the terminal is my friend, and shows AssertionError

    terminal
    ======================== 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 open test_microwave.py from the tests folder

  • I change True to False in tests/test_microwave.py

    the test passes.

  • I open a new terminal then change directory to microwave

    terminal
    cd microwave
    
  • I add the new files and folder to git for tracking

    terminal
    git add .
    
  • I add a git commit message

    terminal
    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



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 the microwave module does not have anything named microwave in it.

  • I add AttributeError to the list of Exceptions seen

  • I open __init__.py from the microwave folder in the src folder

  • I delete all the text in src/microwave/__init__.py

  • I add a function named microwave to src/microwave/__init__.py

    1def microwave():
    2    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: microwave() got
               an unexpected keyword argument 'pressed_start'
    

    because the test called the microwave function with a name (pressed_start) that is not in the parentheses of its definition.

  • I add TypeError to the list of Exceptions seen, in tests/test_microwave.py

  • I add pressed_start to the function definition

    1def microwave(pressed_start):
    2    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    

    the microwave function returned None and the assertion expects True

  • I change the return statement to give the test what it wants

    1def microwave(pressed_start):
    2    return True
    

    the test passes. The microwave function always returns True.

    microwave(pressed_start=True ) -> True
    
  • I add a git commit message in the other terminal

    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

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the microwave function 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_start
    

    the test passes.

    microwave(pressed_start=True ) -> True
    microwave(pressed_start=False) -> False
    
  • I add a git commit message in the other terminal

    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 tests/test_microwave.py

    door

    start button

    output

    open

    NOT pressed

    False

    the terminal is my friend, and shows TypeError

    TypeError: microwave() got
               an unexpected keyword argument 'closed_door'
    

    because the test called the microwave function with a name (closed_door) that is not in the parentheses of its definition.


GREEN: make it pass


  • I add closed_door to the function signature in src/microwave/__init__.py

    1def microwave(closed_door, pressed_start):
    2    return pressed_start
    

    the 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_door

    1def microwave(closed_door=False, pressed_start):
    2    return pressed_start
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: parameter without a default follows
                 parameter with a default
    
  • I add SyntaxError to the list of Exceptions seen, in tests/test_microwave.py

  • I add a default value for pressed_start, in src/microwave/__init__.py

    1def microwave(closed_door=False, pressed_start=False):
    2    return pressed_start
    

    the test passes.

    microwave(closed_door=False, pressed_start=False) -> False
    
  • I add a git commit message in the other terminal

    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 tests/test_microwave.py

    door

    start button

    output

    open

    pressed

    False

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

GREEN: make it pass


  • I add an if statement for closed_door to the microwave function in src/microwave/__init__.py

    1def microwave(closed_door=False, pressed_start=False):
    2    if not closed_door:
    3        return False
    4    return pressed_start
    

    the test passes.

    microwave(closed_door=False, pressed_start=True ) -> False
    microwave(closed_door=False, pressed_start=False) -> False
    
  • I add a git commit message in the other terminal

    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_door parameter to the assertion in test_not_pressed_start for if the Microwave door is closed AND the start button is NOT pressed

    door

    start button

    output

    closed

    NOT pressed

    False

    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) -> False
    
  • I change the name of the test from test_not_pressed_start to test_closed_door_not_pressed_start

  • I add a git commit message in the other terminal

    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_door parameter to the assertion in test_pressed_start for if the Microwave door is closed AND the start button is pressed

    door

    start button

    output

    closed

    pressed

    True

    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) -> False
    
  • I change the name of the test from test_pressed_start to test_closed_door_pressed_start

  • I add a git commit message in the other terminal

    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

    src.microwave.microwave(
        closed_door=False, pressed_start=True
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(closed_door=False, pressed_start=False):
            └── if not closed_door:
                └── return False
                return pressed_start
    
    src.microwave.microwave(
        closed_door=False, pressed_start=False
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(closed_door=False, pressed_start=False):
            └── if not closed_door:
                └── return False
                return pressed_start
    
  • if the door is closed it returns the value of pressed_start

    • if the door is closed AND the start button is NOT pressed it returns False

      src.microwave.microwave(
          closed_door=True , pressed_start=False
      ) -> False
      └── src/microwave/__init__.py
          └── def microwave(closed_door=False, pressed_start=False):
              ├── if not closed_door:
              │       return False
              └── return pressed_start
                  return False
      
    • if the door is closed AND the start button is pressed it returns True

      src.microwave.microwave(
          closed_door=True , pressed_start=True
      ) -> True
      └── src/microwave/__init__.py
          └── 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 (to stop it from burning the food) 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_timer_set_closed_door_pressed_start

RED: make it fail


  • I go back to the terminal where the tests are running

  • I add timer_set to 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 pressed

    door

    timer

    start button

    output

    closed

    set

    pressed

    True

    the terminal is my friend, and shows TypeError

    TypeError: microwave() got
               an unexpected keyword argument 'timer_set'
    

    because the test called the microwave function with a name (timer_set) that is not in the parentheses of its definition.


GREEN: make it pass


I add timer_set to the function signature in src/microwave/__init__.py

1def microwave(
2        closed_door=True, pressed_start=False,
3        timer_set=False,
4    ):
5    if not closed_door:
6        return False
7    return pressed_start

the test passes.

microwave(
    closed_door=True, timer_set=True,
    pressed_start=True
) -> True

REFACTOR: make it better



test_timer_set_closed_door_not_pressed_start

  • I go back to the terminal where the tests are running

  • I add a value for timer_set to 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 pressed

    door

    timer

    start button

    output

    closed

    set

    NOT pressed

    False

    the test is still green.

    microwave(
        closed_door=True, timer_set=True,
        pressed_start=True
    ) -> True
    microwave(
        closed_door=True, timer_set=True,
        pressed_start=False
    ) -> False
    
  • I change the name of the test from test_closed_door_not_pressed_start to test_timer_set_closed_door_not_pressed_start

  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'add test_timer_set_closed_door_not_pressed_start'
    

test_not_timer_set_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

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

GREEN: make it pass


  • I add an if statement for timer_set to the function in src/microwave/__init__.py

    1  def microwave(
    2          closed_door=True, pressed_start=False,
    3          timer_set=False,
    4      ):
    5      if not closed_door:
    6          return False
    7      if not timer_set:
    8          return False
    9      return pressed_start
    

    the test passes.

    microwave(
        closed_door=True, timer_set=True,
        pressed_start=True
    ) -> True
    microwave(
        closed_door=True, timer_set=True,
        pressed_start=False
    ) -> False
    microwave(
        closed_door=True, timer_set=False,
        pressed_start=True
    ) -> False
    
  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'add test_not_timer_set_closed_door_pressed_start'
    

test_not_timer_set_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

    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_timer_set_closed_door_not_pressed_start

    the test passes.

    microwave(
        closed_door=True, timer_set=True,
        pressed_start=True
    ) -> True
    microwave(
        closed_door=True, timer_set=True,
        pressed_start=False
    ) -> False
    microwave(
        closed_door=True, timer_set=False,
        pressed_start=True
    ) -> False
    microwave(
        closed_door=True, timer_set=False,
        pressed_start=False
    ) -> False
    
  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'add test_not_timer_set_closed_door_not_pressed_start'
    

test_timer_set_open_door_pressed_start

RED: make it fail



test_timer_set_open_door_not_pressed_start

  • I go back to the terminal where the tests are running

  • I add a value for timer_set to 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 pressed

    door

    timer

    start button

    output

    open

    set

    NOT pressed

    False

    the test is still green.

    microwave(
        closed_door=False, timer_set=True,
        pressed_start=True
    ) -> False
    microwave(
        closed_door=False, timer_set=False,
        pressed_start=True
    ) -> False
    
  • I change the name of the test from test_open_door_not_pressed_start to test_timer_set_open_door_not_pressed_start

  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'add test_timer_set_open_door_not_pressed_start'
    

test_not_timer_set_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

    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_timer_set_open_door_pressed_start

    the test passes.

    microwave(
        closed_door=False, timer_set=True,
        pressed_start=True
    ) -> False
    microwave(
        closed_door=False, timer_set=False,
        pressed_start=True
    ) -> False
    microwave(
        closed_door=False, timer_set=False,
        pressed_start=True
    ) -> False
    
  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'add test_not_timer_set_open_door_pressed_start'
    

test_not_timer_set_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

    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_timer_set_open_door_not_pressed_start

    the test passes.

    microwave(
        closed_door=False, timer_set=True,
        pressed_start=True
    ) -> False
    microwave(
        closed_door=False, timer_set=False,
        pressed_start=True
    ) -> False
    microwave(
        closed_door=False, timer_set=False,
        pressed_start=True
    ) -> False
    microwave(
        closed_door=False, timer_set=False,
        pressed_start=False
    ) -> False
    
  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'add test_not_timer_set_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

    src.microwave.microwave(
        closed_door=False, timer_set=True,
        pressed_start=True
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    return pressed_start
    
    src.microwave.microwave(
        closed_door=False, timer_set=False,
        pressed_start=True
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    return pressed_start
    
    src.microwave.microwave(
        closed_door=False, timer_set=False,
        pressed_start=True
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    return pressed_start
    
    src.microwave.microwave(
        closed_door=False, timer_set=False,
        pressed_start=False
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    return pressed_start
    
  • If the Microwave door is closed, it checks if the timer is set

    • If the timer is NOT set, it returns False

      src.microwave.microwave(
          closed_door=True, timer_set=False,
          pressed_start=True
      ) -> False
      └── src/microwave/__init__.py
          └── def microwave(
                      closed_door=True, pressed_start=False,
                      timer_set=False,
                  ):
                  ├── if not closed_door:
                  │       return False
                  └── if not timer_set:
                      └── return False
                      return pressed_start
      
      src.microwave.microwave(
          closed_door=True, timer_set=False,
          pressed_start=False
      ) -> False
      └── src/microwave/__init__.py
          └── def microwave(
                      closed_door=True, pressed_start=False,
                      timer_set=False,
                  ):
                  ├── if not closed_door:
                  │       return False
                  └── if not timer_set:
                      └── return False
                      return pressed_start
      
    • If the timer is set, it returns the value of pressed_start

      • If the start button is NOT pressed, it returns False

        src.microwave.microwave(
            closed_door=True, timer_set=True,
            pressed_start=False
        ) -> False
        └── src/microwave/__init__.py
            └── def microwave(
                        closed_door=True, pressed_start=False,
                        timer_set=False,
                    ):
                    ├── if not closed_door:
                    │       return False
                    ├── if not timer_set:
                    │       return False
                    └── return pressed_start
                        return False
        
      • If the Microwave door is closed AND the timer is set AND the start button is pressed, it returns True

        src.microwave.microwave(
            closed_door=True, timer_set=True,
            pressed_start=True
        ) -> True
        └── src/microwave/__init__.py
            └── def microwave(
                        closed_door=True, pressed_start=False,
                        timer_set=False,
                    ):
                    ├── if not closed_door:
                    │       return False
                    ├── if not timer_set:
                    │       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 Microwave timer set?

  • was the start button pressed?

  • is the Microwave temperature too hot?


test_too_hot_w_not_timer_set_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_hot to the assertion in test_not_timer_set_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 hot

    door

    timer

    start button

    too hot

    output

    open

    NOT set

    NOT pressed

    NOT too hot

    False

    the terminal is my friend, and shows TypeError

    TypeError: microwave() got
               an unexpected keyword argument 'too_hot'
    

    because the test called the microwave function 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 src/microwave/__init__.py

    1def microwave(
    2        closed_door=True, pressed_start=False,
    3        timer_set=False, too_hot=False,
    4    ):
    

    the test passes.

    microwave(
        closed_door=False, timer_set=False,
        pressed_start=False, too_hot=False
    ) -> False
    

REFACTOR: make it better



test_too_hot_w_not_timer_set_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_hot to the assertion in test_not_timer_set_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 hot

    door

    timer

    start button

    too hot

    output

    open

    NOT set

    pressed

    NOT too hot

    False

    the test is still green.

    microwave(
        closed_door=False, timer_set=False,
        pressed_start=True, too_hot=False
    ) -> False
    microwave(
        closed_door=False, timer_set=False,
        pressed_start=False, too_hot=True
    ) -> False
    microwave(
        closed_door=False, timer_set=False,
        pressed_start=False, too_hot=False
    ) -> False
    
  • I 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 tests/test_microwave.py

    door

    timer

    start button

    too hot

    output

    open

    NOT set

    pressed

    too hot

    False

    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_timer_set_open_door_pressed_start

70    def test_not_timer_set_open_door_pressed_start(self):
71        self.assertFalse(
72            src.microwave.microwave(
73                closed_door=False,
74                timer_set=False,
75                pressed_start=True,
76                too_hot=True,
77            )
78        )

the test passes.

microwave(
    closed_door=False, timer_set=False,
    pressed_start=True, too_hot=True
) -> False
microwave(
    closed_door=False, timer_set=False,
    pressed_start=True, too_hot=False
) -> False
microwave(
    closed_door=False, timer_set=False,
    pressed_start=False, too_hot=True
) -> False
microwave(
    closed_door=False, timer_set=False,
    pressed_start=False, too_hot=False
) -> False

REFACTOR: make it better



test_too_hot_w_timer_set_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_hot to the assertion in test_timer_set_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 hot

    door

    timer

    start button

    too hot

    output

    open

    set

    NOT pressed

    NOT too hot

    False

    the test is still green.

    microwave(
        closed_door=False, timer_set=True,
        pressed_start=False, too_hot=False
    ) -> False
    
  • I 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 tests/test_microwave.py

    door

    timer

    start button

    too hot

    output

    open

    set

    NOT pressed

    too hot

    False

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

GREEN: make it pass


I change assertTrue to assertFalse in test_timer_set_open_door_not_pressed_start

52    def test_timer_set_open_door_not_pressed_start(self):
53        self.assertFalse(
54            src.microwave.microwave(
55                closed_door=False,
56                timer_set=True,
57                pressed_start=False,
58                too_hot=True,
59            )
60        )

the test passes.

microwave(
    closed_door=False, timer_set=True,
    pressed_start=False, too_hot=True
) -> False
microwave(
    closed_door=False, timer_set=True,
    pressed_start=False, too_hot=False
) -> False

REFACTOR: make it better



test_too_hot_w_timer_set_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_hot to the assertion in test_timer_set_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 hot

    door

    timer

    start button

    too hot

    output

    open

    set

    pressed

    NOT too hot

    False

    the test is still green.

    microwave(
        closed_door=False, timer_set=True,
        pressed_start=True, too_hot=False
    ) -> False
    microwave(
        closed_door=False, timer_set=True,
        pressed_start=False, too_hot=True
    ) -> False
    microwave(
        closed_door=False, timer_set=True,
        pressed_start=False, too_hot=False
    ) -> False
    
  • I 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 tests/test_microwave.py

    door

    timer

    start button

    too hot

    output

    open

    set

    pressed

    too hot

    False

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

GREEN: make it pass


I change assertTrue to assertFalse in test_timer_set_open_door_pressed_start

43    def test_timer_set_open_door_pressed_start(self):
44        self.assertFalse(
45            src.microwave.microwave(
46                closed_door=False,
47                timer_set=True,
48                pressed_start=True,
49                too_hot=True,
50            )
51        )

the test passes.

microwave(
    closed_door=False, timer_set=True,
    pressed_start=True, too_hot=True
) -> False
microwave(
    closed_door=False, timer_set=True,
    pressed_start=True, too_hot=False
) -> False
microwave(
    closed_door=False, timer_set=True,
    pressed_start=False, too_hot=True
) -> False
microwave(
    closed_door=False, timer_set=True,
    pressed_start=False, too_hot=False
) -> False

REFACTOR: make it better



test_too_hot_w_not_timer_set_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_hot to the assertion in test_not_timer_set_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 hot

    door

    timer

    start button

    too hot

    output

    closed

    NOT set

    NOT pressed

    NOT too hot

    False

    the test is still green.

    microwave(
        closed_door=True, timer_set=False,
        pressed_start=False, too_hot=False
    ) -> False
    
  • I 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 tests/test_microwave.py

    door

    timer

    start button

    too hot

    output

    closed

    NOT set

    NOT pressed

    too hot

    False

    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_timer_set_closed_door_not_pressed_start

34    def test_not_timer_set_closed_door_not_pressed_start(self):
35        self.assertFalse(
36            src.microwave.microwave(
37                closed_door=True,
38                timer_set=False,
39                pressed_start=False,
40                too_hot=True,
41            )
42        )

the test passes.

microwave(
    closed_door=True, timer_set=False,
    pressed_start=False, too_hot=True
) -> False
microwave(
    closed_door=True, timer_set=False,
    pressed_start=False, too_hot=False
) -> False

REFACTOR: make it better



test_too_hot_w_not_timer_set_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_hot to the assertion in test_not_timer_set_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 hot

    door

    timer

    start button

    too hot

    output

    closed

    NOT set

    pressed

    NOT too hot

    False

    the test is still green.

    microwave(
        closed_door=True, timer_set=False,
        pressed_start=True, too_hot=False
    ) -> False
    microwave(
        closed_door=True, timer_set=False,
        pressed_start=False, too_hot=True
    ) -> False
    microwave(
        closed_door=True, timer_set=False,
        pressed_start=False, too_hot=False
    ) -> False
    
  • I 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 tests/test_microwave.py

    door

    timer

    start button

    too hot

    output

    closed

    NOT set

    pressed

    too hot

    False

    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_timer_set_closed_door_pressed_start

25    def test_not_timer_set_closed_door_pressed_start(self):
26        self.assertFalse(
27            src.microwave.microwave(
28                closed_door=True,
29                timer_set=False,
30                pressed_start=True,
31                too_hot=True,
32            )
33        )

the test passes.

microwave(
    closed_door=True, timer_set=False,
    pressed_start=True, too_hot=True
) -> False
microwave(
    closed_door=True, timer_set=False,
    pressed_start=True, too_hot=False
) -> False
microwave(
    closed_door=True, timer_set=False,
    pressed_start=False, too_hot=True
) -> False
microwave(
    closed_door=True, timer_set=False,
    pressed_start=False, too_hot=False
) -> False

REFACTOR: make it better



test_too_hot_w_timer_set_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_hot to the assertion in test_timer_set_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 hot

    door

    timer

    start button

    too hot

    output

    closed

    set

    NOT pressed

    NOT too hot

    False

    the test is still green.

    microwave(
        closed_door=True, timer_set=True,
        pressed_start=False, too_hot=False
    ) -> False
    
  • I 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 tests/test_microwave.py

    door

    timer

    start button

    too hot

    output

    closed

    set

    NOT pressed

    too hot

    False

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

GREEN: make it pass


I change assertTrue to assertFalse in test_timer_set_closed_door_not_pressed_start

16    def test_timer_set_closed_door_not_pressed_start(self):
17        self.assertFalse(
18            src.microwave.microwave(
19                closed_door=True,
20                timer_set=True,
21                pressed_start=False,
22                too_hot=True,
23            )
24        )

the test passes.

microwave(
    closed_door=True, timer_set=True,
    pressed_start=False, too_hot=True
) -> False
microwave(
    closed_door=True, timer_set=True,
    pressed_start=False, too_hot=False
) -> False

REFACTOR: make it better



test_too_hot_w_timer_set_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_hot to the assertion in test_timer_set_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 hot

    door

    timer

    start button

    too hot

    output

    closed

    set

    pressed

    NOT too hot

    True

    the test is still green.

    microwave(
        closed_door=True, timer_set=True,
        pressed_start=True, too_hot=False
    ) -> True
    microwave(
        closed_door=True, timer_set=True,
        pressed_start=False, too_hot=True
    ) -> False
    microwave(
        closed_door=True, timer_set=True,
        pressed_start=False, too_hot=False
    ) -> False
    
  • I 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 tests/test_microwave.py

    door

    timer

    start button

    too hot

    output

    closed

    set

    pressed

    too hot

    False

    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 src/microwave/__init__.py

     1def microwave(
     2        closed_door=True, pressed_start=False,
     3        timer_set=False, too_hot=False,
     4    ):
     5    if not closed_door:
     6        return False
     7    if not timer_set:
     8        return False
     9    if too_hot:
    10        return False
    11    return pressed_start
    

    the test passes.

    microwave(
        closed_door=True, timer_set=True,
        pressed_start=True, too_hot=True
    ) -> False
    microwave(
        closed_door=True, timer_set=True,
        pressed_start=True, too_hot=False
    ) -> True
    microwave(
        closed_door=True, timer_set=True,
        pressed_start=False, too_hot=True
    ) -> False
    microwave(
        closed_door=True, timer_set=True,
        pressed_start=False, too_hot=False
    ) -> False
    

REFACTOR: make it better



When the microwave function is called, it checks if the Microwave door is closed

  • If the Microwave door is open, it returns False

    src.microwave.microwave(
        closed_door=False, timer_set=False,
        pressed_start=True, too_hot=True
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False, too_hot=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    if too_hot:
                        return False
                    return pressed_start
    
    src.microwave.microwave(
        closed_door=False, timer_set=False,
        pressed_start=True, too_hot=False
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False, too_hot=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    if too_hot:
                        return False
                    return pressed_start
    
    src.microwave.microwave(
        closed_door=False, timer_set=False,
        pressed_start=False, too_hot=True
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False, too_hot=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    if too_hot:
                        return False
                    return pressed_start
    
    src.microwave.microwave(
        closed_door=False, timer_set=False,
        pressed_start=False, too_hot=False
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False, too_hot=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    if too_hot:
                        return False
                    return pressed_start
    
    src.microwave.microwave(
        closed_door=False, timer_set=True,
        pressed_start=True, too_hot=True
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False, too_hot=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    if too_hot:
                        return False
                    return pressed_start
    
    src.microwave.microwave(
        closed_door=False, timer_set=True,
        pressed_start=True, too_hot=False
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False, too_hot=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    if too_hot:
                        return False
                    return pressed_start
    
    src.microwave.microwave(
        closed_door=False, timer_set=True,
        pressed_start=False, too_hot=True
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False, too_hot=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    if too_hot:
                        return False
                    return pressed_start
    
    src.microwave.microwave(
        closed_door=False, timer_set=True,
        pressed_start=False, too_hot=False
    ) -> False
    └── src/microwave/__init__.py
        └── def microwave(
                    closed_door=True, pressed_start=False,
                    timer_set=False, too_hot=False,
                ):
                └── if not closed_door:
                    └── return False
                    if not timer_set:
                        return False
                    if too_hot:
                        return False
                    return pressed_start
    
  • If the Microwave door is closed, it checks if the timer is set

    • If the timer is NOT set, it returns False

      src.microwave.microwave(
          closed_door=True, timer_set=False,
          pressed_start=True, too_hot=True
      ) -> False
      └── src/microwave/__init__.py
          └── def microwave(
                      closed_door=True, pressed_start=False,
                      timer_set=False, too_hot=False,
                  ):
                  ├── if not closed_door:
                  │       return False
                  └── if not timer_set:
                      └── return False
                      if too_hot:
                          return False
                      return pressed_start
      
      src.microwave.microwave(
          closed_door=True, timer_set=False,
          pressed_start=True, too_hot=False
      ) -> False
      └── src/microwave/__init__.py
          └── def microwave(
                      closed_door=True, pressed_start=False,
                      timer_set=False, too_hot=False,
                  ):
                  ├── if not closed_door:
                  │       return False
                  └── if not timer_set:
                      └── return False
                      if too_hot:
                          return False
                      return pressed_start
      
      src.microwave.microwave(
          closed_door=True, timer_set=False,
          pressed_start=False, too_hot=True
      ) -> False
      └── src/microwave/__init__.py
          └── def microwave(
                      closed_door=True, pressed_start=False,
                      timer_set=False, too_hot=False,
                  ):
                  ├── if not closed_door:
                  │       return False
                  └── if not timer_set:
                      └── return False
                      if too_hot:
                          return False
                      return pressed_start
      
      src.microwave.microwave(
          closed_door=True, timer_set=False,
          pressed_start=False, too_hot=False
      ) -> False
      └── src/microwave/__init__.py
          └── def microwave(
                      closed_door=True, pressed_start=False,
                      timer_set=False, too_hot=False,
                  ):
                  ├── if not closed_door:
                  │       return False
                  └── if not timer_set:
                      └── return False
                      if too_hot:
                          return False
                      return pressed_start
      
    • If the timer is set, it checks if the Microwave temperature is too hot

      • If the Microwave temperature is too hot, it returns False

        src.microwave.microwave(
            closed_door=True, timer_set=True,
            pressed_start=True, too_hot=True
        ) -> False
        └── src/microwave/__init__.py
            └── def microwave(
                        closed_door=True, pressed_start=False,
                        timer_set=False, too_hot=False,
                    ):
                    ├── if not closed_door:
                    │       return False
                    ├── if not timer_set:
                    │       return False
                    └── if too_hot:
                        └── return False
                        return pressed_start
        
        src.microwave.microwave(
            closed_door=True, timer_set=True,
            pressed_start=False, too_hot=True
        ) -> False
        └── src/microwave/__init__.py
            └── def microwave(
                        closed_door=True, pressed_start=False,
                        timer_set=False, too_hot=False,
                    ):
                    ├── if not closed_door:
                    │       return False
                    ├── if not timer_set:
                    │       return False
                    └── if too_hot:
                        └── return False
                        return pressed_start
        
      • If the Microwave temperature is NOT too hot, it returns the value of pressed_start

        • If the start button is NOT pressed, it returns False

          src.microwave.microwave(
              closed_door=True, timer_set=True,
              pressed_start=False, too_hot=False
          ) -> False
          └── src/microwave/__init__.py
              └── def microwave(
                          closed_door=True, pressed_start=False,
                          timer_set=False, too_hot=False,
                      ):
                      ├── if not closed_door:
                      │       return False
                      ├── if not timer_set:
                      │       return False
                      ├── if too_hot:
                      │       return False
                      └── return pressed_start
                          return False
          
        • If 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

          src.microwave.microwave(
              closed_door=True, timer_set=True,
              pressed_start=True, too_hot=False
          ) -> True
          └── src/microwave/__init__.py
              └── def microwave(
                          closed_door=True, pressed_start=False,
                          timer_set=False, too_hot=False,
                      ):
                      ├── if not closed_door:
                      │       return False
                      ├── if not timer_set:
                      │       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_start parameter to make it clearer, in src/microwave/__init__.py

     1def microwave(
     2        closed_door=True, pressed_start=False,
     3        timer_set=False, too_hot=False,
     4    ):
     5    if not closed_door:
     6        return False
     7    if not timer_set:
     8        return False
     9    if too_hot:
    10        return False
    11    if not pressed_start:
    12        return False
    13    return True
    

    the tests are still green.

  • I use Logical Disjunction (OR) to put the if statements together since they all return False

    still green.

  • I write the new statement in terms of not since it happens three times

    the terminal is my friend, and shows SyntaxError

    SyntaxError: invalid syntax
    
  • I “factor” out the nots

    the tests are green again.

  • I add an else clause to make it clearer

    the tests are still green.

  • I use Logical Negation (NOT) to write the else clause in relation to the if statement

    still green.

  • I cancel out the nots

    green.

  • I add a conditional expression

     1def microwave(
     2        closed_door=True, pressed_start=False,
     3        timer_set=False, too_hot=False,
     4    ):
     5    return (
     6        closed_door
     7        and timer_set
     8        and not too_hot
     9        and pressed_start
    10    )
    11    # if not closed_door:
    

    the tests are still green.

  • I remove the commented lines from the microwave function

     1def microwave(
     2        closed_door=True, pressed_start=False,
     3        timer_set=False, too_hot=False,
     4    ):
     5    return (
     6        closed_door
     7        and timer_set
     8        and not too_hot
     9        and pressed_start
    10    )
    
  • I add a git commit message in the other terminal

    terminal
    git commit -am 'refactor microwave function'
    

close the project

  • I close tests/test_microwave.py and src/microwave/__init__.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests. The terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


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

Do you want to see all the CODE I typed in this chapter?


what is next?

Would you like to test making a Car starter?


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.