test microwave with loops

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


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

    ======================== 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

     4class TestMicrowave(unittest.TestCase):
     5
     6    def test_failure(self):
     7        # self.assertFalse(True)
     8        self.assertFalse(False)
     9
    10
    11# Exceptions seen
    

    the test passes.

  • I open a new terminal_ then `change directory`_ to microwave

    cd microwave
    
  • I 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# AssertionError
    

    the terminal_ is my friend, and shows NameError

    NameError: name 'src' is not defined
    

    because I do not have a definition for src in 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 the microwave module does not have anything named microwave in it.

  • I add AttributeError to the list of Exceptions seen

    15# Exceptions seen
    16# AssertionError
    17# NameError
    18# AttributeError
    
  • 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

    15# Exceptions seen
    16# AssertionError
    17# NameError
    18# AttributeError
    19# TypeError
    
  • 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_

    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 seen
    

    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_

    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

    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 seen
    

    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

    30# Exceptions seen
    31# AssertionError
    32# NameError
    33# AttributeError
    34# TypeError
    35# SyntaxError
    
  • 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_

    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

    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_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_

    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

    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) -> False
    
  • I 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_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

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

     5class TestMicrowave(unittest.TestCase):
     6
     7    def test_closed_door_pressed_start(self):
     8        self.assertTrue(
     9            src.microwave.microwave(
    10                closed_door=True,
    11                pressed_start=True,
    12            )
    13        )
    
  • I 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

    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

     7    def test_closed_door_pressed_start(self):
     8        self.assertTrue(
     9            src.microwave.microwave(
    10                closed_door=True,
    11                timer_set=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 '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


  • I change the name of the test from test_closed_door_pressed_start to test_timer_set_closed_door_pressed_start

     5class TestMicrowave(unittest.TestCase):
     6
     7    def test_timer_set_closed_door_pressed_start(self):
     8        self.assertTrue(
     9            src.microwave.microwave(
    10                closed_door=True,
    11                timer_set=True,
    12                pressed_start=True,
    13            )
    14        )
    
  • I add a git_ commit message in the other terminal_

    git commit -am \
    'add test_timer_set_closed_door_pressed_start'
    

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

    16    def test_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            )
    23        )
    24
    25    def test_open_door_pressed_start(self):
    

    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

     7    def test_timer_set_closed_door_pressed_start(self):
     8        self.assertTrue(
     9            src.microwave.microwave(
    10                closed_door=True,
    11                timer_set=True,
    12                pressed_start=True,
    13            )
    14        )
    15
    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            )
    23        )
    
  • I add a git_ commit message in the other 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

    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            )
    23        )
    24
    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            )
    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 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_

    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

    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            )
    32        )
    33
    34    def test_not_timer_set_closed_door_not_pressed_start(self):
    35        self.assertTrue(
    36            src.microwave.microwave(
    37                closed_door=True,
    38                timer_set=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_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            )
    41        )
    42
    43    def test_open_door_pressed_start(self):
    

    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_

    git commit -am \
    'add test_not_timer_set_closed_door_not_pressed_start'
    

test_timer_set_open_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_open_door_pressed_start, for if the Microwave door is open AND the timer is set AND the start button is pressed

    door

    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                timer_set=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, timer_set=True,
        pressed_start=True
    ) -> False
    
  • I change the name of the test from test_open_door_pressed_start to test_timer_set_open_door_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            )
    41        )
    42
    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            )
    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_timer_set_open_door_pressed_start'
    

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

    52    def test_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            )
    59        )
    60
    61
    62# Exceptions seen
    

    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

    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            )
    50        )
    51
    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            )
    59        )
    
  • I add a git_ commit message in the other 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

    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            )
    59        )
    60
    61    def test_not_timer_set_open_door_pressed_start(self):
    62        self.assertTrue(
    63            src.microwave.microwave(
    64                closed_door=False,
    65                timer_set=False,
    66                pressed_start=True,
    67            )
    68        )
    69
    70
    71# Exceptions seen
    

    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

    61    def test_not_timer_set_open_door_pressed_start(self):
    62        self.assertFalse(
    63            src.microwave.microwave(
    64                closed_door=False,
    65                timer_set=False,
    66                pressed_start=True,
    67            )
    68        )
    69
    70
    71# Exceptions seen
    

    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_

    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

    61    def test_not_timer_set_open_door_pressed_start(self):
    62        self.assertFalse(
    63            src.microwave.microwave(
    64                closed_door=False,
    65                timer_set=False,
    66                pressed_start=True,
    67            )
    68        )
    69
    70    def test_not_timer_set_open_door_not_pressed_start(self):
    71        self.assertTrue(
    72            src.microwave.microwave(
    73                closed_door=False,
    74                timer_set=False,
    75                pressed_start=False,
    76            )
    77        )
    78
    79
    80# Exceptions seen
    

    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

    70    def test_not_timer_set_open_door_not_pressed_start(self):
    71        self.assertFalse(
    72            src.microwave.microwave(
    73                closed_door=False,
    74                timer_set=False,
    75                pressed_start=False,
    76            )
    77        )
    78
    79
    80# Exceptions seen
    

    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_

    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

    70    def test_not_timer_set_open_door_not_pressed_start(self):
    71        self.assertFalse(
    72            src.microwave.microwave(
    73                closed_door=False,
    74                timer_set=False,
    75                pressed_start=False,
    76                too_hot=False,
    77            )
    78        )
    79
    80
    81# Exceptions seen
    

    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


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

    door

    timer

    start button

    too hot

    output

    open

    NOT set

    NOT pressed

    too hot

    False

    70    def test_not_timer_set_open_door_not_pressed_start(self):
    71        self.assertTrue(
    72            src.microwave.microwave(
    73                closed_door=False,
    74                timer_set=False,
    75                pressed_start=False,
    76                too_hot=True,
    77            )
    78        )
    79        self.assertFalse(
    80            src.microwave.microwave(
    81                closed_door=False,
    82                timer_set=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# SyntaxError
    

    the terminal_ is my friend, and shows AssertionError

    AssertionError: False is not true
    
  • I change assertTrue to assertFalse in test_not_timer_set_open_door_not_pressed_start

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

    the test passes.

    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 change the name of the test from test_not_timer_set_open_door_not_pressed_start to test_too_hot_w_not_timer_set_open_door_not_pressed_start

    61    def test_not_timer_set_open_door_pressed_start(self):
    62        self.assertFalse(
    63            src.microwave.microwave(
    64                closed_door=False,
    65                timer_set=False,
    66                pressed_start=True,
    67            )
    68        )
    69
    70    def test_too_hot_w_not_timer_set_open_door_not_pressed_start(self):
    71        self.assertFalse(
    72            src.microwave.microwave(
    73                closed_door=False,
    74                timer_set=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_timer_set_open_door_not_pressed_start'
    

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

    61    def test_not_timer_set_open_door_pressed_start(self):
    62        self.assertFalse(
    63            src.microwave.microwave(
    64                closed_door=False,
    65                timer_set=False,
    66                pressed_start=True,
    67                too_hot=False,
    68            )
    69        )
    70
    71    def test_too_hot_w_not_timer_set_open_door_not_pressed_start(self):
    

    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

    61    def test_not_timer_set_open_door_pressed_start(self):
    62        self.assertTrue(
    63            src.microwave.microwave(
    64                closed_door=False,
    65                timer_set=False,
    66                pressed_start=True,
    67                too_hot=True,
    68            )
    69        )
    70        self.assertFalse(
    71            src.microwave.microwave(
    72                closed_door=False,
    73                timer_set=False,
    74                pressed_start=True,
    75                too_hot=False,
    76            )
    77        )
    78
    79    def test_too_hot_w_not_timer_set_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_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


  • I change the name of the test from test_not_timer_set_open_door_pressed_start to test_too_hot_w_not_timer_set_open_door_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            )
    59        )
    60
    61    def test_too_hot_w_not_timer_set_open_door_pressed_start(self):
    62        self.assertFalse(
    63            src.microwave.microwave(
    64                closed_door=False,
    65                timer_set=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_timer_set_open_door_pressed_start'
    

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

    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=False,
    59            )
    60        )
    61
    62    def test_too_hot_w_not_timer_set_open_door_pressed_start(self):
    

    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

    52    def test_timer_set_open_door_not_pressed_start(self):
    53        self.assertTrue(
    54            src.microwave.microwave(
    55                closed_door=False,
    56                timer_set=True,
    57                pressed_start=False,
    58                too_hot=True,
    59            )
    60        )
    61        self.assertFalse(
    62            src.microwave.microwave(
    63                closed_door=False,
    64                timer_set=True,
    65                pressed_start=False,
    66                too_hot=False,
    67            )
    68        )
    69
    70    def test_too_hot_w_not_timer_set_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_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


  • I change the name of the test from test_timer_set_open_door_not_pressed_start to test_too_hot_w_timer_set_open_door_not_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            )
    50        )
    51
    52    def test_too_hot_w_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        )
    
  • I add a git_ commit message in the other terminal_

    git commit -am \
    'add test_too_hot_w_timer_set_open_door_not_pressed_start'
    

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

    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=False,
    50            )
    51        )
    52
    53    def test_too_hot_w_timer_set_open_door_not_pressed_start
    

    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

    43    def test_timer_set_open_door_pressed_start(self):
    44        self.assertTrue(
    45            src.microwave.microwave(
    46                closed_door=False,
    47                timer_set=True,
    48                pressed_start=True,
    49                too_hot=True,
    50            )
    51        )
    52        self.assertFalse(
    53            src.microwave.microwave(
    54                closed_door=False,
    55                timer_set=True,
    56                pressed_start=True,
    57                too_hot=False,
    58            )
    59        )
    60
    61    def test_too_hot_w_timer_set_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_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


  • I change the name of the test from test_timer_set_open_door_pressed_start to test_too_hot_w_timer_set_open_door_pressed_start

    34    def test_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            )
    41        )
    42
    43    def test_too_hot_w_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        )
    
  • I add a git_ commit message in the other terminal_

    git commit -am \
    'add test_too_hot_w_timer_set_open_door_pressed_start'
    

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

    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=False,
    41            )
    42        )
    43
    44    def test_timer_set_open_door_pressed_start(self):
    

    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

    34    def test_not_timer_set_closed_door_not_pressed_start(self):
    35        self.assertTrue(
    36            src.microwave.microwave(
    37                closed_door=True,
    38                timer_set=False,
    39                pressed_start=False,
    40                too_hot=True,
    41            )
    42        )
    43        self.assertFalse(
    44            src.microwave.microwave(
    45                closed_door=True,
    46                timer_set=False,
    47                pressed_start=False,
    48                too_hot=False,
    49            )
    50        )
    51
    52    def test_timer_set_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_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


  • I change the name of the test from test_not_timer_set_closed_door_not_pressed_start to test_too_hot_w_not_timer_set_closed_door_not_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            )
    32        )
    33
    34    def test_too_hot_w_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        )
    
  • I add a git_ commit message in the other terminal_

    git commit -am \
    'add test_too_hot_w_not_timer_set_closed_door_not_pressed_start'
    

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

    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=False,
    32            )
    33        )
    34
    35    def test_too_hot_w_not_timer_set_closed_door_not_pressed_start(self):
    

    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

    25    def test_not_timer_set_closed_door_pressed_start(self):
    26        self.assertTrue(
    27            src.microwave.microwave(
    28                closed_door=True,
    29                timer_set=False,
    30                pressed_start=True,
    31                too_hot=True,
    32            )
    33        )
    34        self.assertFalse(
    35            src.microwave.microwave(
    36                closed_door=True,
    37                timer_set=False,
    38                pressed_start=True,
    39                too_hot=False,
    40            )
    41        )
    42
    43    def test_too_hot_w_not_timer_set_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_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


  • I change the name of the test from test_not_timer_set_closed_door_pressed_start to test_too_hot_w_not_timer_set_closed_door_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            )
    23        )
    24
    25    def test_too_hot_w_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        )
    
  • I add a git_ commit message in the other terminal_

    git commit -am \
    'add test_too_hot_w_not_timer_set_closed_door_pressed_start'
    

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

    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=False,
    23            )
    24        )
    25
    26    def test_too_hot_w_not_timer_set_closed_door_pressed_start(self):
    

    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

    16    def test_timer_set_closed_door_not_pressed_start(self):
    17        self.assertTrue(
    18            src.microwave.microwave(
    19                closed_door=True,
    20                timer_set=True,
    21                pressed_start=False,
    22                too_hot=True,
    23            )
    24        )
    25        self.assertFalse(
    26            src.microwave.microwave(
    27                closed_door=True,
    28                timer_set=True,
    29                pressed_start=False,
    30                too_hot=False,
    31            )
    32        )
    33
    34    def test_too_hot_w_not_timer_set_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_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


  • I change the name of the test from test_timer_set_closed_door_not_pressed_start to test_too_hot_w_timer_set_closed_door_not_pressed_start

     7    def test_timer_set_closed_door_pressed_start(self):
     8        self.assertTrue(
     9            src.microwave.microwave(
    10                closed_door=True,
    11                timer_set=True,
    12                pressed_start=True,
    13            )
    14        )
    15
    16    def test_too_hot_w_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        )
    
  • I add a git_ commit message in the other terminal_

    git commit -am \
    'add test_too_hot_w_timer_set_closed_door_not_pressed_start'
    

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

     7    def test_timer_set_closed_door_pressed_start(self):
     8        self.assertTrue(
     9            src.microwave.microwave(
    10                closed_door=True,
    11                timer_set=True,
    12                pressed_start=True,
    13                too_hot=False,
    14            )
    15        )
    16
    17    def test_too_hot_w_timer_set_closed_door_not_pressed_start(self):
    

    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

     7    def test_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        )
    24
    25    def test_too_hot_w_timer_set_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 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


  • I change the name of the test from test_timer_set_closed_door_pressed_start to test_too_hot_w_timer_set_closed_door_pressed_start, in tests/test_microwave.py

     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        )
    
  • I add a git_ commit message in the other terminal_

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

    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

     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    if (
    14        not closed_door
    15        or not timer_set
    16        or too_hot
    17        or not pressed_start
    18    ):
    19        return False
    20    return True
    

    still green.

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

    13    # if (
    14    #     not closed_door
    15    #     or not timer_set
    16    #     or too_hot
    17    #     or not pressed_start
    18    # ):
    19    if (
    20        (not closed_door)
    21        (not and) (not timer_set)
    22        (not and) (not not too_hot)
    23        (not and) (not pressed_start)
    24    ):
    25        return False
    26    return True
    

    the terminal_ is my friend, and shows SyntaxError_

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

    19    # if (
    20    #     (not closed_door)
    21    #     (not and) (not timer_set)
    22    #     (not and) (not not too_hot)
    23    #     (not and) (not pressed_start)
    24    # ):
    25    if not (
    26        closed_door
    27        and timer_set
    28        and not too_hot
    29        and pressed_start
    30    ):
    31        return False
    32    return True
    

    the tests are green again.

  • I add an else clause to make it clearer

    25    if not (
    26        closed_door
    27        and timer_set
    28        and not too_hot
    29        and pressed_start
    30    ):
    31        return False
    32    else:
    33        return True
    

    the 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 timer_set
    36        and not too_hot
    37        and pressed_start
    38    ):
    39        return True
    

    still green.

  • I cancel out the nots

    32    # else:
    33    # if not not (
    34    if (
    35        closed_door
    36        and timer_set
    37        and not too_hot
    38        and pressed_start
    39    ):
    40        return True
    

    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_

    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.