Car

I want to make a Car that comes ON or stays OFF when I push the start button.


preview

These are the tests I have at the end of the chapter

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

       1#!/bin/bash
       2uv init car
       3cd car
       4mkdir src
       5mv main.py src/car.py
       6mkdir tests
       7touch tests/__init__.py
       8
       9echo "import unittest
      10
      11
      12class TestCar(unittest.TestCase):
      13
      14    def test_failure(self):
      15        self.assertFalse(True)
      16
      17
      18# Exceptions seen
      19# AssertionError
      20" > tests/test_car.py
      21
      22echo "pytest" > requirements.txt
      23echo "pytest-watcher" >> requirements.txt
      24uv add --requirement requirements.txt
      25uv run pytest-watcher . --now
      
    • I run makePythonTdd.sh in the terminal to make the car project

      ./makePythonTdd.sh
      
    • I open makePythonTdd.ps1

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

       1uv init car
       2cd car
       3mkdir src
       4Move-Item "main.py" "src/car.py"
       5mkdir tests
       6New-Item tests/__init__.py
       7
       8"import unittest
       9
      10
      11class TestCar(unittest.TestCase):
      12
      13    def test_failure(self):
      14        self.assertFalse(True)
      15
      16
      17# Exceptions seen
      18# AssertionError
      19" | Out-File "tests/test_car.py" -Encoding UTF8
      20
      21"pytest" | Out-File requirements.txt -Encoding UTF8
      22"pytest-watcher" >> requirements.txt
      23uv add --requirement requirements.txt
      24uv run pytest-watcher . --now
      
    • I run makePythonTdd.ps1 in the terminal to make the car project

      .\makePythonTdd.ps1
      

    the terminal is my friend, and shows AssertionError

    ======================== FAILURES =========================
    _________________ TestCar.test_failure ____________________
    
    self = <tests.test_car.TestCar testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_car.py:7: AssertionError
    ================ short test summary info ==================
    FAILED tests/test_car.py::TestCar::test_failure - AssertionError: True is not false
    ==================== 1 failed in X.YZs ====================
    
  • I hold ctrl (Windows/Linux) or option/command (MacOS) on the keyboard and use the mouse to click on tests/test_car.py:7 to open it

  • I change True to False in test_car.py

     4class TestCar(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 car

    cd car
    
  • 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 car to start only when the start button is pressed. I get this truth table

start button

output

pressed

True

NOT pressed

False

where True is the Car comes ON and False is it stays OFF.


test_start_pressed

RED: make it fail


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

  • I change test_failure to test_start_pressed, then add an assertion for if the start button is pressed

    start button

    output

    pressed

    True

     4class TestCar(unittest.TestCase):
     5
     6    def test_start_pressed(self):
     7        reality = src.car.ignition(
     8            start_is_pressed=True,
     9        )
    10        self.assertTrue(reality)
    11
    12
    13# Exceptions seen
    14# 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 test_car.py.


GREEN: make it pass


  • I add NameError to the list of Exceptions seen

    13# Exceptions seen
    14# AssertionError
    15# NameError
    
  • I add an import statement at the top of the file

    1import src.car
    2import unittest
    3
    4
    5class TestCar(unittest.TestCase):
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.car' has no attribute 'ignition'
    

    because car.py in the src folder does not have anything named ignition in it.

  • I add AttributeError to the list of Exceptions seen

    14# Exceptions seen
    15# AssertionError
    16# NameError
    17# AttributeError
    
  • I open car.py from the src folder

  • I delete all the text in the file then add a function named ignition to car.py

    1def ignition():
    2    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: ignition() got
               an unexpected keyword argument 'start_is_pressed'
    

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

  • I add TypeError to the list of Exceptions seen, in test_car.py

    14# Exceptions seen
    15# AssertionError
    16# NameError
    17# AttributeError
    18# TypeError
    
  • I add start_is_pressed to the function signature

    1def ignition(start_is_pressed):
    2    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    

    because the ignition function returns None and the assertion expects True.

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

    1def ignition(start_is_pressed):
    2    return True
    

    the test passes.

  • I add a git commit message in the other terminal

    git commit -am 'add test_start_pressed'
    

The ignition function always returns True.

ignition(start_is_pressed=True ) -> True

test_start_not_pressed

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

    11        self.assertTrue(reality)
    12
    13    def test_start_not_pressed(self):
    14        reality = src.car.ignition(
    15            start_is_pressed=False,
    16        )
    17        self.assertFalse(reality)
    18
    19
    20# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the function always returns True and the assertion expects False.


GREEN: make it pass


  • I change the return statement

    1def ignition(start_is_pressed):
    2    return start_is_pressed
    

    the test passes.

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

    git commit -am 'add test_start_not_pressed'
    

The ignition function always returns the value of start_is_pressed. Is this the Identity Function?

I want the car to start only when the start button is pressed AND the key is close to the ignition. The inputs to the ignition will then be

  • was the start button pressed?

  • is the key close to the ignition?

Which gives me this truth table

key

start button

output

close

pressed

True

NOT close

pressed

False

close

NOT pressed

False

NOT close

NOT pressed

False


test_key_close_start_pressed

RED: make it fail


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

  • I add key_is_close with a value to the call to the ignition function from test_start_pressed for if the the start button is pressed AND the key is close to the ignition

    key

    start button

    output

    close

    pressed

    True

     7    def test_start_pressed(self):
     8        reality = src.car.ignition(
     9            start_is_pressed=True,
    10            key_is_close=True,
    11        )
    12        self.assertTrue(reality)
    13
    14    def test_start_not_pressed(self):
    

    the terminal is my friend, and shows TypeError

    TypeError: ignition() got
               an unexpected keyword argument 'key_is_close
    

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


GREEN: make it pass


  • I add key_is_close to the function definition in car.py

    1def ignition(start_is_pressed, key_is_close):
    2    return start_is_pressed
    

    the terminal is my friend, and shows TypeError

    TypeError: ignition() missing
               1 required positional argument: 'key_is_close'
    

    because the assertion in test_start_not_pressed calls the ignition function with one argument (start_is_pressed) and I just changed the function to make it take two required arguments (key_is_close and start_is_pressed). I have to make key_is_close a choice.

  • I add a default value for key_is_close to make it a choice

    1def ignition(start_is_pressed, key_is_close=False):
    2    return start_is_pressed
    

    the test passes.

    ignition(key_is_close=True , start_is_pressed=True ) -> True
    

REFACTOR: make it better


  • I change the name of test_start_pressed to test_key_close_start_pressed

     5class TestCar(unittest.TestCase):
     6
     7    def test_key_close_start_pressed(self):
     8        reality = src.car.ignition(
     9            key_is_close=True,
    10            start_is_pressed=True,
    11        )
    12        self.assertTrue(reality)
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_key_close_start_pressed'
    

test_key_not_close_start_pressed

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 pressed AND the key is NOT close to the ignition, in test_car.py

    key

    start button

    output

    NOT close

    pressed

    False

    12      self.assertTrue(reality)
    13
    14  def test_key_not_close_start_pressed(self):
    15      reality = src.car.ignition(
    16          start_is_pressed=True,
    17      )
    18      self.assertFalse(reality)
    19
    20  def test_start_not_pressed(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the ignition function returns True and the assertion expects False.

    I do not need to add a value for the key_is_close parameter since

    src.car.ignition(
        start_is_pressed=True
    )
    

    is the same as

    src.car.ignition(
        start_is_pressed=True,
        key_is_close=False,
    )
    

    because a function uses the default value for a parameter when it is called without the parameter.


GREEN: make it pass


I add an if statement to car.py

1def ignition(start_is_pressed, key_is_close=False):
2    if key_is_close == False:
3        return False
4    return start_is_pressed

the test passes.


REFACTOR: make it better


  • I want the ignition function to check if the start button is pressed before it checks if the key is close to the ignition since there is no need to check for the key if the button is NOT pressed. I change the if statement

    1def ignition(start_is_pressed, key_is_close=False):
    2    # if key_is_close == False:
    3    #     return False
    4    # return start_is_pressed
    5    if start_is_pressed:
    6        return key_is_close
    

    the test is still green.

  • I remove the commented lines from the ignition function

    1def ignition(start_is_pressed, key_is_close=False):
    2    if start_is_pressed:
    3        return key_is_close
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_key_not_close_start_pressed'
    

When the ignition function is called it checks if the start button is pressed. If the start button is pressed it returns the value of key_is_close (True if it is close, False if it is NOT close.

ignition(key_is_close=False, start_is_pressed=True ) -> False
ignition(key_is_close=True , start_is_pressed=True ) -> True

test_key_close_start_not_pressed

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

  • I add a value for the key_is_close parameter to the assertion in test_start_not_pressed for if the start button is NOT pressed AND the key is close to the ignition, in test_car.py

    key

    start button

    output

    close

    NOT pressed

    False

    18        self.assertFalse(reality)
    19
    20    def test_start_not_pressed(self):
    21        reality = src.car.ignition(
    22            start_is_pressed=False,
    23            key_is_close=True,
    24        )
    25        self.assertFalse(reality)
    

    the test is still green.

  • I change the name from test_start_not_pressed to test_key_close_start_not_pressed

    18        self.assertFalse(reality)
    19
    20    def test_key_close_start_not_pressed(self):
    21        reality = src.car.ignition(
    22            start_is_pressed=False,
    23            key_is_close=True,
    24        )
    25        self.assertFalse(reality)
    26
    27
    28# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_key_close_start_not_pressed'
    

When the ignition function is called it checks if the start button is pressed. If the start button is pressed it returns the value of key_is_close (True if it is close, False if it is NOT close.

ignition(key_is_close=True , start_is_pressed=False) -> False
ignition(key_is_close=False, start_is_pressed=True ) -> False
ignition(key_is_close=True , start_is_pressed=True ) -> True

test_key_not_close_start_not_pressed

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 AND the key is NOT close to the ignition, in test_car.py

    key

    start button

    output

    NOT close

    NOT pressed

    False

    25        self.assertFalse(reality)
    26
    27    def test_key_not_close_start_not_pressed(self):
    28        reality = src.car.ignition(
    29            start_is_pressed=False,
    30        )
    31        self.assertTrue(reality)
    32
    33
    34# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    

    because all functions return None by default.

    I do not give a value for the key_is_close parameter since

    src.car.ignition(
        start_is_pressed=False,
    )
    

    is the same as

    src.car.ignition(
        start_is_pressed=False,
        key_is_close=False,
    )
    

    because a function uses the default value for a parameter when it is called without the parameter.


GREEN: make it pass


  • I add a return statement to the ignition function to make it clearer, in car.py

    1def ignition(start_is_pressed, key_is_close=False):
    2    if start_is_pressed:
    3        return key_is_close
    4    return None
    

    the terminal still shows AssertionError

  • I change None to False in the return statement

    1def ignition(start_is_pressed, key_is_close=False):
    2    if start_is_pressed:
    3        return key_is_close
    4    return False
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    
  • I change assertTrue to assertFalse to match reality in test_key_not_close_start_not_pressed in test_car.py

    27    def test_key_not_close_start_not_pressed(self):
    28        reality = src.car.ignition(
    29            start_is_pressed=False,
    30        )
    31        self.assertFalse(reality)
    32
    33
    34# Exceptions seen
    

    the test passes.

    ignition(key_is_close=False, start_is_pressed=False) -> False
    ignition(key_is_close=True , start_is_pressed=False) -> False
    ignition(key_is_close=False, start_is_pressed=True ) -> False
    ignition(key_is_close=True , start_is_pressed=True ) -> True
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_key_not_close_start_not_pressed'
    

When the ignition function is called. It checks if the start button is pressed.

  • If the start button is NOT pressed it returns False

    ignition(key_is_close=False, start_is_pressed=False) -> False
    └── def ignition(start_is_pressed, key_is_close=False):
        ├── if start_is_pressed:
               return key_is_close
        └── return False
    
    ignition(key_is_close=True , start_is_pressed=False) -> False
    └── def ignition(start_is_pressed, key_is_close=False):
        ├── if start_is_pressed:
               return key_is_close
        └── return False
    
  • If the start button is pressed it returns the value of key_is_close

    • if the key is NOT close to the ignition it returns False

      ignition(key_is_close=False, start_is_pressed=True ) -> False
      └── def ignition(start_is_pressed, key_is_close=False):
          └── if start_is_pressed:
              └── return key_is_close
                  return False
              return False
      
    • if the key is close to the ignition it returns True

    ignition(key_is_close=True , start_is_pressed=True ) -> True
    └── def ignition(start_is_pressed, key_is_close=False):
        └── if start_is_pressed:
            └── return key_is_close
                return True
            return False
    

remove reality variable

I can call the ignition function directly in all the assertions.

  • I call src.car.ignition directly in the assertion of test_key_not_close_start_not_pressed

    27    def test_key_not_close_start_not_pressed(self):
    28        # reality = src.car.ignition(
    29        #     start_is_pressed=False,
    30        # )
    31        # self.assertFalse(reality)
    32        self.assertFalse(
    33            src.car.ignition(
    34                start_is_pressed=False,
    35            )
    36        )
    37
    38
    39# Exceptions seen
    

    the test is still green.

  • I remove the commented lines from test_key_not_close_start_not_pressed

    27    def test_key_not_close_start_not_pressed(self):
    28        self.assertFalse(
    29            src.car.ignition(
    30                start_is_pressed=False,
    31            )
    32        )
    33
    34
    35# Exceptions seen
    
  • I call src.car.ignition directly in the assertion of test_key_close_start_not_pressed

    20    def test_key_close_start_not_pressed(self):
    21        # reality = src.car.ignition(
    22        #     key_is_close=True,
    23        #     start_is_pressed=False,
    24        # )
    25        # self.assertFalse(reality)
    26        self.assertFalse(
    27              src.car.ignition(
    28                key_is_close=True,
    29                start_is_pressed=False,
    30            )
    31        )
    32
    33    def test_key_not_close_start_not_pressed(self):
    

    still green.

  • I remove the commented lines from test_key_close_start_not_pressed

    20    def test_key_close_start_not_pressed(self):
    21        self.assertFalse(
    22              src.car.ignition(
    23                key_is_close=True,
    24                start_is_pressed=False,
    25            )
    26        )
    27
    28    def test_key_not_close_start_not_pressed(self):
    
  • I call src.car.ignition directly in the assertion of test_key_not_close_start_pressed

    14    def test_key_not_close_start_pressed(self):
    15        # reality = src.car.ignition(
    16        #     start_is_pressed=True
    17        # )
    18        # self.assertFalse(reality)
    19        self.assertFalse(
    20            src.car.ignition(
    21                start_is_pressed=True
    22            )
    23        )
    24
    25    def test_key_close_start_not_pressed(self):
    

    green.

  • I remove the commented lines from test_key_not_close_start_pressed

    14    def test_key_not_close_start_pressed(self):
    15        self.assertFalse(
    16            src.car.ignition(
    17                start_is_pressed=True
    18            )
    19        )
    20
    21    def test_key_close_start_not_pressed(self):
    
  • I call src.car.ignition directly in the assertion of test_key_close_start_pressed

     7    def test_key_close_start_pressed(self):
     8        # reality = src.car.ignition(
     9        #     key_is_close=True,
    10        #     start_is_pressed=True,
    11        # )
    12        # self.assertTrue(reality)
    13        self.assertTrue(
    14            src.car.ignition(
    15                key_is_close=True,
    16                start_is_pressed=True,
    17            )
    18        )
    19
    20    def test_key_not_close_start_pressed(self):
    

    the test is still green.

  • I remove the commented lines from test_key_close_start_pressed

     7    def test_key_close_start_pressed(self):
     8        self.assertTrue(
     9            src.car.ignition(
    10                key_is_close=True,
    11                start_is_pressed=True,
    12            )
    13        )
    14
    15    def test_key_not_close_start_pressed(self):
    
  • I add a git commit message in the other terminal

    git commit -am 'remove reality variable'
    

So far, the truth table for the ignition is

key

start button

output

close

pressed

True

NOT close

pressed

False

close

NOT pressed

False

NOT close

NOT pressed

False

I want the car to start only when the start button is pressed AND the key is close to the ignition AND the brake pedal is pressed. The inputs to the ignition will then be

  • was the start button pressed?

  • is the key close to the ignition?

  • is the brake being pressed?


test_brake_pressed_key_close_start_pressed

RED: make it fail


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

  • I add a value for brake_is_pressed to the assertion in test_key_close_start_pressed, for if the start button is pressed AND the key is close to the ignition AND the brake is being pressed

    key

    brake

    start button

    output

    close

    pressed

    pressed

    True

     7    def test_key_close_start_pressed(self):
     8        self.assertTrue(
     9            src.car.ignition(
    10                start_is_pressed=True,
    11                key_is_close=True,
    12                brake_is_pressed=True,
    13            )
    14        )
    15
    16    def test_key_not_close_start_pressed(self):
    

    the terminal is my friend, and shows TypeError

    TypeError: ignition() got
               an unexpected keyword argument 'brake_is_pressed'.
               Did you mean 'start_is_pressed'?
    

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


GREEN: make it pass


  • I add brake_is_pressed to the function signature in car.py

    1def ignition(
    2        start_is_pressed, key_is_close=False,
    3        brake_is_pressed,
    4    ):
    5    if start_is_pressed:
    6        return key_is_close
    7    return False
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: parameter without a default follows
                 parameter with a default
    

    because parameters without default values must come before parameters with default values.

  • I add SyntaxError to the list of Exceptions seen, in test_car.py

    39# Exceptions seen
    40# AssertionError
    41# NameError
    42# AttributeError
    43# TypeError
    44# SyntaxError
    
  • I add a default value for the brake_is_pressed parameter, in car.py

    1def ignition(
    2        start_is_pressed, key_is_close=False,
    3        brake_is_pressed=False,
    4    ):
    5    if not key_is_close:
    6        return False
    7    return start_is_pressed
    

    the test passes.

    ignition(
        start_is_pressed=True, key_is_close=True,
        brake_is_pressed=True
    ) -> True
    

REFACTOR: make it better


  • I change the name from test_key_close_start_pressed to test_brake_pressed_key_close_start_pressed

     5class TestCar(unittest.TestCase):
     6
     7    def test_brake_pressed_key_close_start_pressed(self):
     8        self.assertTrue(
     9            src.car.ignition(
    10                start_is_pressed=True,
    11                key_is_close=True,
    12                brake_is_pressed=True,
    13            )
    14        )
    
  • I add a git commit message in the other terminal

    git commit -am
    'add test_brake_pressed_key_close_start_pressed'
    

test_brake_not_pressed_key_close_start_pressed

RED: make it fail


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

  • I add a new test with an assertion for if the start button is pressed AND the key is close to the ignition AND the brake is NOT pressed

    key

    brake

    start button

    output

    close

    NOT pressed

    pressed

    False

     7    def test_brake_pressed_key_close_start_pressed(self):
     8        self.assertTrue(
     9            src.car.ignition(
    10                start_is_pressed=True,
    11                key_is_close=True,
    12                brake_is_pressed=True,
    13            )
    14        )
    15
    16    def test_brake_not_pressed_key_close_start_pressed(self):
    17        self.assertFalse(
    18            src.car.ignition(
    19                start_is_pressed=True,
    20                key_is_close=True,
    21                brake_is_pressed=False,
    22            )
    23        )
    24
    25    def test_key_not_close_start_pressed(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the ignition function returned True and the assertion expects False. I do not need to give a value for the brake_is_pressed parameter since it is the same as the default value, I do it to make things clearer.


GREEN: make it pass


  • I add an if statement to the ignition function in car.py

    1def ignition(
    2        start_is_pressed, key_is_close=False,
    3        brake_is_pressed=False,
    4    ):
    5    if brake_is_pressed == False:
    6        return False
    7    if start_is_pressed:
    8        return key_is_close
    9    return False
    

    the test passes.

  • I write a simpler version of the new if statement

    5    # if brake_is_pressed == False:
    6    if not brake_is_pressed:
    7        return False
    

    still green, because if something == False is the same as if not something == True is the same as if not something.

  • I want the ignition function to only check if the brake is pressed if the start button is pressed AND the key is close to the ignition. I change the if statements

     5    # if brake_is_pressed == False:
     6    # if not brake_is_pressed:
     7    #     return False
     8    if start_is_pressed:
     9        # return key_is_close
    10        if key_is_close:
    11            return brake_is_pressed
    12    return False
    13
    14the test is still green.
    
  • I remove the commented lines from the ignition function

    1def ignition(
    2        start_is_pressed, key_is_close=False,
    3        brake_is_pressed=False,
    4    ):
    5    if start_is_pressed:
    6        if key_is_close:
    7            return brake_is_pressed
    8    return False
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_brake_not_pressed_key_close_start_pressed'
    

When the ignition function is called. It checks if the start button is pressed.

  • If the start button is NOT pressed it returns False

  • If the start button is pressed it checks if the key is close to the ignition

    • if the key is NOT close to the ignition it leaves the if statements then returns False

    • if the key is close to the ignition it returns the value of brake_is_pressed

      • if the brake is NOT pressed it returns False

      • if the brake is pressed it returns True

ignition(
    start_is_pressed=True, key_is_close=True,
    brake_is_pressed=False
) -> False
ignition(
    start_is_pressed=True, key_is_close=True,
    brake_is_pressed=True
) -> True

test_brake_pressed_key_not_close_start_pressed

  • I add values for the key_is_close and brake_is_pressed parameters to the assertion in test_key_not_close_start_pressed for if the start button is pressed AND the key is NOT close to the ignition AND the brake is being pressed

    key

    brake

    start button

    output

    NOT close

    pressed

    pressed

    False

    25    def test_key_not_close_start_pressed(self):
    26        self.assertFalse(
    27            src.car.ignition(
    28                start_is_pressed=True,
    29                key_is_close=False,
    30                brake_is_pressed=True,
    31            )
    32        )
    33
    34    def test_key_close_start_not_pressed(self):
    

    the test is still green.

    ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=True
    ) -> False
    ignition(
        start_is_pressed=True, key_is_close=True,
        brake_is_pressed=False
    ) -> False
    ignition(
        start_is_pressed=True, key_is_close=True,
        brake_is_pressed=True
    ) -> True
    
  • I change the name of the test from test_key_not_close_start_pressed to test_brake_pressed_key_not_close_start_pressed

     5    def test_brake_not_pressed_key_close_start_pressed(self):
     6        self.assertFalse(
     7            src.car.ignition(
     8                start_is_pressed=True,
     9                key_is_close=True,
    10                brake_is_pressed=False,
    11            )
    12        )
    13
    14    def test_brake_pressed_key_not_close_start_pressed(self):
    15        self.assertFalse(
    16            src.car.ignition(
    17                start_is_pressed=True,
    18                key_is_close=False,
    19                brake_is_pressed=True,
    20            )
    21        )
    22
    23    def test_key_close_start_not_pressed(self):
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_brake_pressed_key_not_close_start_pressed'
    

test_brake_not_pressed_key_not_close_start_pressed

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 pressed AND the key is NOT close to the ignition AND the brake is NOT pressed

    key

    brake

    start button

    output

    NOT close

    NOT pressed

    pressed

    False

    25    def test_brake_pressed_key_not_close_start_pressed(self):
    26        self.assertFalse(
    27            src.car.ignition(
    28                start_is_pressed=True,
    29                key_is_close=False,
    30                brake_is_pressed=True,
    31            )
    32        )
    33
    34    def test_brake_not_pressed_key_not_close_start_pressed(self):
    35        self.assertTrue(
    36            src.car.ignition(
    37                start_is_pressed=True,
    38                key_is_close=False,
    39                brake_is_pressed=False,
    40            )
    41        )
    42
    43    def test_key_close_start_not_pressed(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

GREEN: make it pass


  • I change assertTrue to assertFalse to match the result of the call to the ignition function

    34    def test_brake_not_pressed_key_not_close_start_pressed(self):
    35        self.assertFalse(
    36            src.car.ignition(
    37                start_is_pressed=True,
    38                key_is_close=False,
    39                brake_is_pressed=False,
    40            )
    41        )
    42
    43    def test_key_close_start_not_pressed(self):
    

    the test passes.

  • I add a git commit message in the other terminal

    git commit -am \
    'add test_brake_not_pressed_key_not_close_start_pressed'
    

When the ignition function is called. It checks if the start button is pressed.

  • If the start button is NOT pressed it returns False

  • If the start button is pressed it checks if the key is close to the ignition

    • if the key is NOT close to the ignition it leaves the if statements then returns False

      ignition(
          start_is_pressed=True, key_is_close=False,
          brake_is_pressed=False
      ) -> False
      └── def ignition(
                  start_is_pressed, key_is_close=False,
                  brake_is_pressed=False,
              ):
              └── if start_is_pressed:
              ┌───┴── if key_is_close:
                         return brake_is_pressed
              └── return False
      
      ignition(
          start_is_pressed=True, key_is_close=False,
          brake_is_pressed=True
      ) -> False
      └── def ignition(
                  start_is_pressed, key_is_close=False,
                  brake_is_pressed=False,
              ):
              └── if start_is_pressed:
              ┌───┴── if key_is_close:
                         return brake_is_pressed
              └── return False
      
    • if the key is close to the ignition it returns the value of brake_is_pressed

      • if the brake is NOT pressed it returns False

        ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=False
        ) -> False
        └── def ignition(
                    start_is_pressed, key_is_close=False,
                    brake_is_pressed=False,
                ):
                └── if start_is_pressed:
                    └── if key_is_close:
                        └── return brake_is_pressed
                            return False
                    return False
        
      • if the brake is pressed it returns True

        ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=True
        ) -> True
        └── def ignition(
                    start_is_pressed, key_is_close=False,
                    brake_is_pressed=False,
                ):
                └── if start_is_pressed:
                    └── if key_is_close:
                        └── return brake_is_pressed
                            return True
                    return False
        

test_brake_pressed_key_close_start_not_pressed

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

  • I add a value for the brake_is_pressed parameter to the assertion in test_key_close_start_not_pressed for the case where the start button is NOT pressed AND the key is close to the ignition AND the brake is being pressed

    key

    brake

    start button

    output

    close

    pressed

    NOT pressed

    False

    43    def test_key_close_start_not_pressed(self):
    44        self.assertFalse(
    45            src.car.ignition(
    46                start_is_pressed=False,
    47                key_is_close=True,
    48                brake_is_pressed=True,
    49            )
    50        )
    51
    52    def test_key_not_close_start_not_pressed(self):
    

    the test is still green.

    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=True
    ) -> False
    
  • I change the name from test_key_close_start_not_pressed to test_brake_pressed_key_close_start_not_pressed

    34    def test_brake_not_pressed_key_not_close_start_pressed(self):
    35        self.assertFalse(
    36            src.car.ignition(
    37                start_is_pressed=True,
    38                key_is_close=False,
    39                brake_is_pressed=False,
    40            )
    41        )
    42
    43    def test_brake_pressed_key_close_start_not_pressed(self):
    44        self.assertFalse(
    45            src.car.ignition(
    46                start_is_pressed=False,
    47                key_is_close=True,
    48                brake_is_pressed=True,
    49            )
    50        )
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_brake_pressed_key_close_start_not_pressed'
    

test_brake_not_pressed_key_close_start_not_pressed

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 AND the key is close to the ignition AND the brake is NOT pressed

    key

    brake

    start button

    output

    close

    NOT pressed

    NOT pressed

    False

    43    def test_brake_pressed_key_close_start_not_pressed(self):
    44        self.assertFalse(
    45            src.car.ignition(
    46                start_is_pressed=False,
    47                key_is_close=True,
    48                brake_is_pressed=True,
    49            )
    50        )
    51
    52    def test_brake_not_pressed_key_close_start_not_pressed(self):
    53        self.assertTrue(
    54            src.car.ignition(
    55                start_is_pressed=False,
    56                key_is_close=True,
    57                brake_is_pressed=False,
    58            )
    59        )
    60
    61    def test_key_not_close_start_not_pressed(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

GREEN: make it pass


  • I change assertTrue to assertFalse to match reality

    52      def test_brake_not_pressed_key_close_start_not_pressed(self):
    53          self.assertFalse(
    54              src.car.ignition(
    55                  start_is_pressed=False,
    56                  key_is_close=True,
    57                  brake_is_pressed=False,
    58              )
    59          )
    60
    61      def test_key_not_close_start_not_pressed(self):
    

    the test passes.

    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=False
    ) -> False
    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=True
    ) -> False
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_brake_not_pressed_key_close_start_not_pressed'
    

test_brake_pressed_key_not_close_start_not_pressed

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

  • I add values for the key_is_close and brake_is_pressed parameters to test_key_not_close_start_not_pressed for if the start button is NOT pressed AND the key is NOT close to the ignition AND the brake is being pressed

    key

    brake

    start button

    output

    NOT close

    pressed

    NOT pressed

    False

    61    def test_key_not_close_start_not_pressed(self):
    62        self.assertFalse(
    63            src.car.ignition(
    64                start_is_pressed=False,
    65                key_is_close=False,
    66                brake_is_pressed=True,
    67            )
    68        )
    69
    70
    71# Exceptions seen
    

    the test is still green.

    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=True
    ) -> False
    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=False
    ) -> False
    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=True
    ) -> False
    
  • I change the name from test_key_not_close_start_not_pressed to test_brake_pressed_key_not_close_start_not_pressed

    52    def test_brake_not_pressed_key_close_start_not_pressed(self):
    53        self.assertFalse(
    54            src.car.ignition(
    55                start_is_pressed=False,
    56                key_is_close=True,
    57                brake_is_pressed=False,
    58            )
    59        )
    60
    61    def test_brake_pressed_key_not_close_start_not_pressed(self):
    62        self.assertFalse(
    63            src.car.ignition(
    64                start_is_pressed=False,
    65                key_is_close=False,
    66                brake_is_pressed=True,
    67            )
    68        )
    69
    70
    71# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_brake_pressed_key_not_close_start_not_pressed'
    

test_brake_not_pressed_key_not_close_start_not_pressed

  • 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 AND the key is NOT close to the ignition AND the brake is NOT pressed

    key

    brake

    start button

    output

    NOT close

    NOT pressed

    NOT pressed

    False

    61    def test_brake_pressed_key_not_close_start_not_pressed(self):
    62        self.assertFalse(
    63            src.car.ignition(
    64                start_is_pressed=False,
    65                key_is_close=False,
    66                brake_is_pressed=True,
    67            )
    68        )
    69
    70    def test_brake_not_pressed_key_not_close_start_not_pressed(self):
    71        self.assertTrue(
    72            src.car.ignition(
    73                start_is_pressed=False,
    74                key_is_close=False,
    75                brake_is_pressed=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 to match the result of the call to src.car.ignition in test_brake_not_pressed_key_not_close_start_not_pressed

    70    def test_brake_not_pressed_key_not_close_start_not_pressed(self):
    71        self.assertFalse(
    72            src.car.ignition(
    73                start_is_pressed=False,
    74                key_is_close=False,
    75                brake_is_pressed=False,
    76            )
    77        )
    78
    79
    80# Exceptions seen
    

    the test passes.

  • I add a git commit message in the other terminal

    git commit -am \
    'add test_brake_not_pressed_key_not_close_start_not_pressed'
    

When the ignition function is called. It checks if the start button is pressed.

  • If the start button is NOT pressed it returns False

    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=False
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False,
            ):
            ├── if start_is_pressed:
                   if key_is_close:
                       return brake_is_pressed
            └── return False
    
    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=True
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False,
            ):
            ├── if start_is_pressed:
                   if key_is_close:
                       return brake_is_pressed
            └── return False
    
    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=False
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False,
            ):
            ├── if start_is_pressed:
                   if key_is_close:
                       return brake_is_pressed
            └── return False
    
    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=True
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False,
            ):
            ├── if start_is_pressed:
                   if key_is_close:
                       return brake_is_pressed
            └── return False
    
  • If the start button is pressed it checks if the key is close to the ignition

    • if the key is NOT close to the ignition it leaves the if statements then returns False

      ignition(
          start_is_pressed=True, key_is_close=False,
          brake_is_pressed=False
      ) -> False
      └── def ignition(
                  start_is_pressed, key_is_close=False,
                  brake_is_pressed=False,
              ):
              └── if start_is_pressed:
              ┌───┴── if key_is_close:
                         return brake_is_pressed
              └── return False
      
      ignition(
          start_is_pressed=True, key_is_close=False,
          brake_is_pressed=True
      ) -> False
      └── def ignition(
                  start_is_pressed, key_is_close=False,
                  brake_is_pressed=False,
              ):
              └── if start_is_pressed:
              ┌───┴── if key_is_close:
                         return brake_is_pressed
              └── return False
      
    • if the key is close to the ignition it returns the value of brake_is_pressed

      • if the brake is NOT pressed it returns False

        ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=False
        ) -> False
        └── def ignition(
                    start_is_pressed, key_is_close=False,
                    brake_is_pressed=False,
                ):
                └── if start_is_pressed:
                    └── if key_is_close:
                        └── return brake_is_pressed
                            return False
                    return False
        
      • if the brake is pressed it returns True

        ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=True
        ) -> True
        └── def ignition(
                    start_is_pressed, key_is_close=False,
                    brake_is_pressed=False,
                ):
                └── if start_is_pressed:
                    └── if key_is_close:
                        └── return brake_is_pressed
                            return True
                    return False
        

The truth table for the Car Ignition is

key

brake

start button

output

NOT close

NOT pressed

pressed

False

NOT close

pressed

pressed

False

close

NOT pressed

pressed

False

close

pressed

pressed

True

key

brake

start button

output

NOT close

NOT pressed

NOT pressed

False

NOT close

pressed

NOT pressed

False

close

NOT pressed

NOT pressed

False

close

pressed

NOT pressed

False

I want to make sure the car is in park before it can start, so it does not immediately move when it is turned on (that would be a problem). The inputs will then be

  • was the start button pressed?

  • is the key close to the ignition?

  • is the brake being pressed?

  • is the gear in park?


test_park_w_brake_not_pressed_key_not_close_start_not_pressed

RED: make it fail


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

  • I add a value for in_park to the assertion in test_brake_not_pressed_key_not_close_start_not_pressed for the case where the start button is NOT pressed AND the key is NOT close to the ignition AND the brake is being NOT pressed AND the car gear is in park, to test_brake_not_pressed_key_not_close_start_not_pressed

    key

    brake

    start

    gear

    output

    NOT close

    NOT pressed

    NOT pressed

    in park

    False

    70    def test_brake_not_pressed_key_not_close_start_not_pressed(self):
    71        self.assertFalse(
    72            src.car.ignition(
    73                start_is_pressed=False,
    74                key_is_close=False,
    75                brake_is_pressed=False,
    76                in_park=True,
    77            )
    78        )
    79
    80
    81# Exceptions seen
    

    the terminal shows TypeError

    TypeError: ignition() got
               an unexpected keyword argument 'in_park'
    

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


GREEN: make it pass


  • I add in_park to the function signature in car.py

    1def ignition(
    2        start_is_pressed, key_is_close=False,
    3        brake_is_pressed=False, in_park,
    4    ):
    5        if start_is_pressed:
    6            if key_is_close:
    7                return brake_is_pressed
    8        return False
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: parameter without a default follows
                 parameter with a default
    

    because parameters without default values must come before parameters with default values.

  • I add a default value for the in_park parameter in the function signature to make it a choice

    1def ignition(
    2        start_is_pressed, key_is_close=False,
    3        brake_is_pressed=False, in_park=False,
    4    ):
    

    the test passes.

    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=False, in_park=True
    ) -> False
    

REFACTOR: make it better


  • I add an assertion for if the start button is NOT pressed AND the key is NOT close to the ignition AND the brake is being NOT pressed AND the car gear is NOT in park

    key

    brake

    start

    gear

    output

    NOT close

    NOT pressed

    NOT pressed

    NOT in park

    False

    70    def test_brake_not_pressed_key_not_close_start_not_pressed(self):
    71        self.assertFalse(
    72            src.car.ignition(
    73                start_is_pressed=False,
    74                key_is_close=False,
    75                brake_is_pressed=False,
    76                in_park=True,
    77            )
    78        )
    79        self.assertTrue(
    80            src.car.ignition(
    81                start_is_pressed=False,
    82                key_is_close=False,
    83                brake_is_pressed=False,
    84                in_park=False,
    85            )
    86        )
    87
    88
    89# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the ignition function returns False and the assertion expects True

  • I change assertTrue to assertFalse to match the result of the call

    70    def test_brake_not_pressed_key_not_close_start_not_pressed(self):
    71        self.assertFalse(
    72            src.car.ignition(
    73                start_is_pressed=False,
    74                key_is_close=False,
    75                brake_is_pressed=False,
    76                in_park=True,
    77            )
    78        )
    79        self.assertFalse(
    80            src.car.ignition(
    81                start_is_pressed=False,
    82                key_is_close=False,
    83                brake_is_pressed=False,
    84                in_park=False,
    85            )
    86        )
    87
    88
    89# Exceptions seen
    90# AssertionError
    91# NameError
    92# AttributeError
    93# TypeError
    94# SyntaxError
    

    the test passes.

    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=False, in_park=True
    ) -> False
    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=False, in_park=False
    ) -> False
    
  • I change the name of the test to from:ref:test_brake_not_pressed_key_not_close_start_not_pressed to test_park_w_brake_not_pressed_key_not_close_start_not_pressed

    61    def test_brake_pressed_key_not_close_start_not_pressed(self):
    62        self.assertFalse(
    63            src.car.ignition(
    64                start_is_pressed=False,
    65                key_is_close=False,
    66                brake_is_pressed=True,
    67            )
    68        )
    69
    70    def test_park_w_brake_not_pressed_key_not_close_start_not_pressed(self):
    71        self.assertFalse(
    72            src.car.ignition(
    73                start_is_pressed=False,
    74                key_is_close=False,
    75                brake_is_pressed=False,
    76                in_park=True,
    77            )
    78        )
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_park_w_brake_not_pressed_key_not_close_start_not_pressed'
    

test_park_w_brake_pressed_key_not_close_start_not_pressed

RED: make it fail


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

  • I add a value for the in_park parameter to the assertion in test_brake_pressed_key_not_close_start_not_pressed for if the start button is NOT pressed AND the key is NOT close to the ignition AND the brake is being pressed AND the car gear is in park

    key

    brake

    start

    gear

    output

    NOT close

    pressed

    NOT pressed

    in park

    False

    61    def test_brake_pressed_key_not_close_start_not_pressed(self):
    62        self.assertFalse(
    63            src.car.ignition(
    64                start_is_pressed=False,
    65                key_is_close=False,
    66                brake_is_pressed=True,
    67                in_park=True,
    68            )
    69        )
    70
    71    def test_park_w_brake_not_pressed_key_not_close_start_not_pressed(self):
    

    the test is still green.

    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=True, in_park=True
    ) -> False
    
  • I add an assertion for if the start button is NOT pressed AND the key is NOT close to the ignition AND the brake is being pressed AND the car gear is NOT in park

    key

    brake

    start

    gear

    output

    NOT close

    pressed

    NOT pressed

    NOT in park

    False

    61    def test_brake_pressed_key_not_close_start_not_pressed(self):
    62        self.assertFalse(
    63            src.car.ignition(
    64                start_is_pressed=False,
    65                key_is_close=False,
    66                brake_is_pressed=True,
    67                in_park=True,
    68            )
    69        )
    70        self.assertTrue(
    71            src.car.ignition(
    72                start_is_pressed=False,
    73                key_is_close=False,
    74                brake_is_pressed=True,
    75                in_park=False,
    76            )
    77        )
    78
    79    def test_brake_not_pressed_key_not_close_start_not_pressed(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_brake_pressed_key_not_close_start_not_pressed

61    def test_brake_pressed_key_not_close_start_not_pressed(self):
62        self.assertFalse(
63            src.car.ignition(
64                start_is_pressed=False,
65                key_is_close=False,
66                brake_is_pressed=True,
67                in_park=True,
68            )
69        )
70        self.assertFalse(
71            src.car.ignition(
72                start_is_pressed=False,
73                key_is_close=False,
74                brake_is_pressed=True,
75                in_park=False,
76            )
77        )
78
79    def test_brake_not_pressed_key_not_close_start_not_pressed(self):

the test passes.

ignition(
    start_is_pressed=False, key_is_close=False,
    brake_is_pressed=True, in_park=True
) -> False
ignition(
    start_is_pressed=False, key_is_close=False,
    brake_is_pressed=True, in_park=False
) -> False

REFACTOR: make it better


  • I change the name from test_brake_pressed_key_not_close_start_not_pressed to test_park_w_brake_pressed_key_not_close_start_not_pressed

    52    def test_brake_not_pressed_key_close_start_not_pressed(self):
    53        self.assertFalse(
    54            src.car.ignition(
    55                start_is_pressed=False,
    56                key_is_close=True,
    57                brake_is_pressed=False,
    58            )
    59        )
    60
    61    def test_park_w_brake_pressed_key_not_close_start_not_pressed(self):
    62        self.assertFalse(
    63            src.car.ignition(
    64                start_is_pressed=False,
    65                key_is_close=False,
    66                brake_is_pressed=True,
    67                in_park=True,
    68            )
    69        )
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_park_w_brake_pressed_key_not_close_start_not_pressed'
    

test_park_w_brake_not_pressed_key_close_start_not_pressed

RED: make it fail


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

  • I add a value for the in_park parameter to the assertion in test_brake_not_pressed_key_close_start_not_pressed for if the start button is NOT pressed AND the key is close to the ignition AND the brake is NOT pressed AND the car gear is in park

    key

    brake

    start

    gear

    output

    close

    NOT pressed

    NOT pressed

    in park

    False

    52    def test_brake_not_pressed_key_close_start_not_pressed(self):
    53        self.assertFalse(
    54            src.car.ignition(
    55                start_is_pressed=False,
    56                key_is_close=True,
    57                brake_is_pressed=False,
    58                in_park=True,
    59            )
    60        )
    61
    62    def test_park_w_brake_pressed_key_not_close_start_not_pressed(self):
    

    the test is still green.

    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=False, in_park=True
    ) -> False
    
  • I add an assertion for if the start button is NOT pressed AND the key is close to the ignition AND the brake is NOT pressed AND the car gear is NOT in park

    key

    brake

    start

    gear

    output

    close

    NOT pressed

    NOT pressed

    NOT in park

    False

    52    def test_brake_not_pressed_key_close_start_not_pressed(self):
    53        self.assertFalse(
    54            src.car.ignition(
    55                start_is_pressed=False,
    56                key_is_close=True,
    57                brake_is_pressed=False,
    58                in_park=True,
    59            )
    60        )
    61        self.assertTrue(
    62            src.car.ignition(
    63                start_is_pressed=False,
    64                key_is_close=True,
    65                brake_is_pressed=False,
    66                in_park=False,
    67            )
    68        )
    69
    70    def test_park_w_brake_pressed_key_not_close_start_not_pressed(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_brake_not_pressed_key_close_start_not_pressed

52    def test_brake_not_pressed_key_close_start_not_pressed(self):
53        self.assertFalse(
54            src.car.ignition(
55                start_is_pressed=False,
56                key_is_close=True,
57                brake_is_pressed=False,
58                in_park=True,
59            )
60        )
61        self.assertFalse(
62            src.car.ignition(
63                start_is_pressed=False,
64                key_is_close=True,
65                brake_is_pressed=False,
66                in_park=False,
67            )
68        )
69
70    def test_park_w_brake_pressed_key_not_close_start_not_pressed(self):

the test passes.

ignition(
    start_is_pressed=False, key_is_close=True,
    brake_is_pressed=False, in_park=True
) -> False
ignition(
    start_is_pressed=False, key_is_close=True,
    brake_is_pressed=False, in_park=False
) -> False

REFACTOR: make it better


  • I change the name of the test from test_brake_not_pressed_key_close_start_not_pressed to test_park_w_brake_not_pressed_key_close_start_not_pressed

    43    def test_brake_pressed_key_close_start_not_pressed(self):
    44        self.assertFalse(
    45            src.car.ignition(
    46                start_is_pressed=False,
    47                key_is_close=True,
    48                brake_is_pressed=True,
    49            )
    50        )
    51
    52    def test_park_w_brake_not_pressed_key_close_start_not_pressed(self):
    53        self.assertFalse(
    54            src.car.ignition(
    55                start_is_pressed=False,
    56                key_is_close=True,
    57                brake_is_pressed=False,
    58                in_park=True,
    59            )
    60        )
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_park_w_brake_not_pressed_key_close_start_not_pressed'
    

test_park_w_brake_pressed_key_close_start_not_pressed

RED: make it fail


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

  • I add a value for the in_park parameter to the assertion in test_brake_pressed_key_close_start_not_pressed for if the start button is NOT pressed AND the key is close to the ignition AND the brake is being pressed AND the car gear is in park

    key

    brake

    start button

    gear

    output

    close

    pressed

    NOT pressed

    in park

    False

    43    def test_brake_pressed_key_close_start_not_pressed(self):
    44        self.assertFalse(
    45            src.car.ignition(
    46                start_is_pressed=False,
    47                key_is_close=True,
    48                brake_is_pressed=True,
    49                in_park=True,
    50            )
    51        )
    52
    53    def test_park_w_brake_not_pressed_key_close_start_not_pressed(self):
    

    the test is still green.

    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=True, in_park=True
    ) -> False
    
  • I add an assertion for if the start button is NOT pressed AND the key is close to the ignition AND the brake is being pressed AND the car gear is NOT in park

    key

    brake

    start

    gear

    output

    close

    pressed

    NOT pressed

    NOT in park

    False

    43    def test_brake_pressed_key_close_start_not_pressed(self):
    44        self.assertFalse(
    45            src.car.ignition(
    46                start_is_pressed=False,
    47                key_is_close=True,
    48                brake_is_pressed=True,
    49                in_park=True,
    50            )
    51        )
    52        self.assertTrue(
    53            src.car.ignition(
    54                start_is_pressed=False,
    55                key_is_close=True,
    56                brake_is_pressed=True,
    57                in_park=False,
    58            )
    59        )
    60
    61    def test_park_w_brake_not_pressed_key_close_start_not_pressed(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_brake_pressed_key_close_start_not_pressed

43    def test_brake_pressed_key_close_start_not_pressed(self):
44        self.assertFalse(
45            src.car.ignition(
46                start_is_pressed=False,
47                key_is_close=True,
48                brake_is_pressed=True,
49                in_park=True,
50            )
51        )
52        self.assertFalse(
53            src.car.ignition(
54                start_is_pressed=False,
55                key_is_close=True,
56                brake_is_pressed=True,
57                in_park=False,
58            )
59        )
60
61    def test_park_w_brake_not_pressed_key_close_start_not_pressed(self):

the test passes.

ignition(
    start_is_pressed=False, key_is_close=True,
    brake_is_pressed=True, in_park=True
) -> False
ignition(
    start_is_pressed=False, key_is_close=True,
    brake_is_pressed=True, in_park=False
) -> False

REFACTOR: make it better


  • I change the name from test_brake_pressed_key_close_start_not_pressed to test_park_w_brake_pressed_key_close_start_not_pressed

    34    def test_brake_not_pressed_key_not_close_start_pressed(self):
    35        self.assertFalse(
    36            src.car.ignition(
    37                start_is_pressed=True,
    38                key_is_close=False,
    39                brake_is_pressed=False,
    40            )
    41        )
    42
    43    def test_park_w_brake_pressed_key_close_start_not_pressed(self):
    44        self.assertFalse(
    45            src.car.ignition(
    46                start_is_pressed=False,
    47                key_is_close=True,
    48                brake_is_pressed=True,
    49                in_park=True,
    50            )
    51        )
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_park_w_brake_pressed_key_close_start_not_pressed'
    

test_park_w_brake_not_pressed_key_not_close_start_pressed

RED: make it fail


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

  • I add a value for the in_park parameter to the assertion in test_brake_not_pressed_key_not_close_start_pressed for if the start button is pressed AND the key is NOT close to the ignition AND the brake is NOT pressed AND the car gear is in park

    key

    brake

    start

    gear

    output

    NOT close

    NOT pressed

    pressed

    in park

    False

    34    def test_brake_not_pressed_key_not_close_start_pressed(self):
    35        self.assertFalse(
    36            src.car.ignition(
    37                start_is_pressed=True,
    38                key_is_close=False,
    39                brake_is_pressed=False,
    40                in_park=True,
    41            )
    42        )
    43
    44    def test_park_w_brake_pressed_key_close_start_not_pressed(self):
    

    the test is still green.

    ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=False, in_park=True
    ) -> False
    
  • I add an assertion for if the start button is pressed AND the key is NOT close to the ignition AND the brake is NOT pressed AND the car gear is NOT in park

    key

    brake

    start

    gear

    output

    NOT close

    NOT pressed

    pressed

    NOT in park

    False

    34    def test_brake_not_pressed_key_not_close_start_pressed(self):
    35        self.assertFalse(
    36            src.car.ignition(
    37                start_is_pressed=True,
    38                key_is_close=False,
    39                brake_is_pressed=False,
    40                in_park=True,
    41            )
    42        )
    43        self.assertTrue(
    44            src.car.ignition(
    45                start_is_pressed=True,
    46                key_is_close=False,
    47                brake_is_pressed=False,
    48                in_park=False,
    49            )
    50        )
    51
    52    def test_park_w_brake_pressed_key_close_start_not_pressed(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_brake_not_pressed_key_not_close_start_pressed

34    def test_brake_not_pressed_key_not_close_start_pressed(self):
35        self.assertFalse(
36            src.car.ignition(
37                start_is_pressed=True,
38                key_is_close=False,
39                brake_is_pressed=False,
40                in_park=True,
41            )
42        )
43        self.assertFalse(
44            src.car.ignition(
45                start_is_pressed=True,
46                key_is_close=False,
47                brake_is_pressed=False,
48                in_park=False,
49            )
50        )
51
52    def test_park_w_brake_pressed_key_close_start_not_pressed(self):

the test passes.

ignition(
    start_is_pressed=True, key_is_close=False,
    brake_is_pressed=False, in_park=False
) -> False
ignition(
    start_is_pressed=True, key_is_close=False,
    brake_is_pressed=False, in_park=True
) -> False

REFACTOR: make it better


  • I change the name from test_brake_not_pressed_key_not_close_start_pressed to test_park_w_brake_not_pressed_key_not_close_start_pressed

    25    def test_brake_pressed_key_not_close_start_pressed(self):
    26        self.assertFalse(
    27            src.car.ignition(
    28                start_is_pressed=True,
    29                key_is_close=False,
    30                brake_is_pressed=True,
    31            )
    32        )
    33
    34    def test_park_w_brake_not_pressed_key_not_close_start_pressed(self):
    35        self.assertFalse(
    36            src.car.ignition(
    37                start_is_pressed=True,
    38                key_is_close=False,
    39                brake_is_pressed=False,
    40                in_park=True,
    41            )
    42        )
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_park_w_brake_not_pressed_key_not_close_start_pressed'
    

test_park_w_brake_pressed_key_not_close_start_pressed

RED: make it fail


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

  • I add a value for the in_park parameter to the assertion in test_brake_pressed_key_not_close_start_pressed for if the start button is pressed AND the key is NOT close to the ignition AND the brake is pressed AND the car gear is in park

    key

    brake

    start

    gear

    output

    NOT close

    pressed

    pressed

    in park

    False

    25    def test_brake_pressed_key_not_close_start_pressed(self):
    26        self.assertFalse(
    27            src.car.ignition(
    28                start_is_pressed=True,
    29                key_is_close=False,
    30                brake_is_pressed=True,
    31                in_park=True,
    32            )
    33        )
    34
    35    def test_park_w_brake_not_pressed_key_not_close_start_pressed(self):
    

    the test is still green.

    ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=True, in_park=True
    ) -> False
    
  • I add an assertion for if the start button is pressed AND the key is NOT close to the ignition AND the brake is pressed AND the car gear is NOT in park

    key

    brake

    start

    gear

    output

    NOT close

    pressed

    pressed

    NOT in park

    False

    25    def test_brake_pressed_key_not_close_start_pressed(self):
    26        self.assertFalse(
    27            src.car.ignition(
    28                start_is_pressed=True,
    29                key_is_close=False,
    30                brake_is_pressed=True,
    31                in_park=True,
    32            )
    33        )
    34        self.assertTrue(
    35            src.car.ignition(
    36                start_is_pressed=True,
    37                key_is_close=False,
    38                brake_is_pressed=True,
    39                in_park=False,
    40            )
    41        )
    42
    43    def test_park_w_brake_not_pressed_key_not_close_start_pressed(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_brake_pressed_key_not_close_start_pressed

25    def test_brake_pressed_key_not_close_start_pressed(self):
26        self.assertFalse(
27            src.car.ignition(
28                start_is_pressed=True,
29                key_is_close=False,
30                brake_is_pressed=True,
31                in_park=True,
32            )
33        )
34        self.assertFalse(
35            src.car.ignition(
36                start_is_pressed=True,
37                key_is_close=False,
38                brake_is_pressed=True,
39                in_park=False,
40            )
41        )
42
43    def test_park_w_brake_not_pressed_key_not_close_start_pressed(self):

the test passes.

ignition(
    start_is_pressed=True, key_is_close=False,
    brake_is_pressed=True, in_park=True
) -> False
ignition(
    start_is_pressed=True, key_is_close=False,
    brake_is_pressed=True, in_park=False
) -> False

REFACTOR: make it better


  • I change the name from test_brake_pressed_key_not_close_start_pressed to test_park_w_brake_pressed_key_not_close_start_pressed

    16    def test_brake_not_pressed_key_close_start_pressed(self):
    17        self.assertFalse(
    18            src.car.ignition(
    19                start_is_pressed=True,
    20                key_is_close=True,
    21                brake_is_pressed=False,
    22            )
    23        )
    24
    25    def test_park_w_brake_pressed_key_not_close_start_pressed(self):
    26        self.assertFalse(
    27            src.car.ignition(
    28                start_is_pressed=True,
    29                key_is_close=False,
    30                brake_is_pressed=True,
    31                in_park=True,
    32            )
    33        )
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_park_w_brake_pressed_key_not_close_start_pressed'
    

test_park_w_brake_not_pressed_key_close_start_pressed

RED: make it fail


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

  • I add a value for the in_park parameter to the assertion in test_brake_not_pressed_key_close_start_pressed for if the start button is pressed AND the key is close to the ignition AND the brake is NOT pressed AND the car gear is in park

    key

    brake

    start

    gear

    output

    close

    NOT pressed

    pressed

    in park

    False

    16    def test_brake_not_pressed_key_close_start_pressed(self):
    17        self.assertFalse(
    18            src.car.ignition(
    19                start_is_pressed=True,
    20                key_is_close=True,
    21                brake_is_pressed=False,
    22                in_park=True,
    23            )
    24        )
    25
    26    def test_park_w_brake_pressed_key_not_close_start_pressed(self):
    

    the test is still green.

    ignition(
        start_is_pressed=True, key_is_close=True,
        brake_is_pressed=False, in_park=True
    ) -> False
    
  • I add an assertion for if the start button is pressed AND the key is close to the ignition AND the brake is NOT pressed AND the car gear is NOT in park

    key

    brake

    start

    gear

    output

    close

    NOT pressed

    pressed

    NOT in park

    False

    16    def test_brake_not_pressed_key_close_start_pressed(self):
    17        self.assertFalse(
    18            src.car.ignition(
    19                start_is_pressed=True,
    20                key_is_close=True,
    21                brake_is_pressed=False,
    22                in_park=True,
    23            )
    24        )
    25        self.assertTrue(
    26            src.car.ignition(
    27                start_is_pressed=True,
    28                key_is_close=True,
    29                brake_is_pressed=False,
    30                in_park=False,
    31            )
    32        )
    33
    34    def test_park_w_brake_pressed_key_not_close_start_pressed(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_brake_not_pressed_key_close_start_pressed

16    def test_brake_not_pressed_key_close_start_pressed(self):
17        self.assertFalse(
18            src.car.ignition(
19                start_is_pressed=True,
20                key_is_close=True,
21                brake_is_pressed=False,
22                in_park=True,
23            )
24        )
25        self.assertFalse(
26            src.car.ignition(
27                start_is_pressed=True,
28                key_is_close=True,
29                brake_is_pressed=False,
30                in_park=False,
31            )
32        )
33
34    def test_park_w_brake_pressed_key_not_close_start_pressed(self):

the test passes.

ignition(
    start_is_pressed=True, key_is_close=True,
    brake_is_pressed=False, in_park=True
) -> False
ignition(
    start_is_pressed=True, key_is_close=True,
    brake_is_pressed=False, in_park=False
) -> False

REFACTOR: make it better


  • I change the name from test_brake_not_pressed_key_close_start_pressed to test_park_w_brake_not_pressed_key_close_start_pressed

     7    def test_brake_pressed_key_close_start_pressed(self):
     8        self.assertTrue(
     9            src.car.ignition(
    10                start_is_pressed=True,
    11                key_is_close=True,
    12                brake_is_pressed=True,
    13            )
    14        )
    15
    16    def test_park_w_brake_not_pressed_key_close_start_pressed(self):
    17        self.assertFalse(
    18            src.car.ignition(
    19                start_is_pressed=True,
    20                key_is_close=True,
    21                brake_is_pressed=False,
    22                in_park=True,
    23            )
    24        )
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_park_w_brake_not_pressed_key_close_start_pressed'
    

test_park_w_brake_pressed_key_close_start_pressed

RED: make it fail


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

  • I add a value for the in_park parameter to the assertion in test_brake_pressed_key_close_start_pressed for if the start button is pressed AND the key is close to the ignition AND the brake is being pressed AND the car gear is in park

    key

    brake

    start

    gear

    output

    close

    pressed

    pressed

    in park

    True

     7    def test_brake_pressed_key_close_start_pressed(self):
     8        self.assertTrue(
     9            src.car.ignition(
    10                start_is_pressed=True,
    11                key_is_close=True,
    12                brake_is_pressed=True,
    13                in_park=True,
    14            )
    15        )
    16
    17    def test_park_w_brake_not_pressed_key_close_start_pressed(self):
    

    the test is still green.

    ignition(
        start_is_pressed=True, key_is_close=True,
        brake_is_pressed=True, in_park=True
    ) -> True
    
  • I add an assertion for if the start button is pressed AND the key is close to the ignition AND the brake is being pressed AND the car gear is NOT in park

    key

    brake

    start

    gear

    output

    close

    pressed

    pressed

    NOT in park

    False

     7    def test_brake_pressed_key_close_start_pressed(self):
     8        self.assertTrue(
     9            src.car.ignition(
    10                start_is_pressed=True,
    11                key_is_close=True,
    12                brake_is_pressed=True,
    13                in_park=True,
    14            )
    15        )
    16        self.assertFalse(
    17            src.car.ignition(
    18                start_is_pressed=True,
    19                key_is_close=True,
    20                brake_is_pressed=True,
    21                in_park=False,
    22            )
    23        )
    24
    25    def test_park_w_brake_not_pressed_key_close_start_pressed(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

GREEN: make it pass


I add an if statement to the ignition function in car.py

 1def ignition(
 2        start_is_pressed, key_is_close=False,
 3        brake_is_pressed=False, in_park=False,
 4    ):
 5    if in_park == False:
 6        return False
 7    if start_is_pressed:
 8        if key_is_close:
 9            return brake_is_pressed
10    return False

the test passes.

ignition(
    start_is_pressed=True, key_is_close=True,
    brake_is_pressed=True, in_park=True
) -> True
ignition(
    start_is_pressed=True, key_is_close=True,
    brake_is_pressed=True, in_park=False
) -> False

REFACTOR: make it better


  • I make the if statement simpler with Logical Negation (NOT)

    5    # if in_park == False:
    6    if not in_park:
    7        return False
    

    the test is still green, because if something == False is the same as if not something == True is the same as if not something.

  • I want the function to check if the start button is pressed before it checks the other conditions. I add an if statement

     5    # if in_park == False:
     6    # if not in_park:
     7    #     return False
     8    if start_is_pressed:
     9        if key_is_close:
    10            # return brake_is_pressed
    11            if brake_is_pressed:
    12                return in_park
    13    return False
    

    still green.

  • I use Logical Conjunction (AND) to put the if statements together

     5    # if in_park == False:
     6    # if not in_park:
     7    #     return False
     8    # if start_is_pressed:
     9    #     if key_is_close:
    10            # return brake_is_pressed
    11            # if brake_is_pressed:
    12            #     return in_park
    13    if (
    14        start_is_pressed
    15        and key_is_close
    16        and brake_is_pressed
    17    ):
    18        return in_park
    19    return False
    

    green.

  • I remove the commented lines from the ignition function

     1def ignition(
     2        start_is_pressed, key_is_close=False,
     3        brake_is_pressed=False, in_park=False,
     4    ):
     5    if (
     6        start_is_pressed
     7        and key_is_close
     8        and brake_is_pressed
     9    ):
    10        return in_park
    11    return False
    
  • I change the name from test_brake_pressed_key_close_start_pressed to test_park_w_brake_pressed_key_close_start_pressed

     5class TestCar(unittest.TestCase):
     6
     7    def test_park_w_brake_pressed_key_close_start_pressed(self):
     8        self.assertTrue(
     9            src.car.ignition(
    10                start_is_pressed=True,
    11                key_is_close=True,
    12                brake_is_pressed=True,
    13                in_park=True,
    14            )
    15        )
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_park_w_brake_pressed_key_close_start_pressed'
    

When the ignition function is called. It checks if the start button is pressed then if the key is close to the ignition then if the brake is pressed

  • If the start button is NOT pressed it returns False

    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=False, in_park=True
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
            ┌───┴── start_is_pressed
                   and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=False, in_park=False
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
            ┌───┴── start_is_pressed
                   and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=True, in_park=True
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
            ┌───┴── start_is_pressed
                   and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
    ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=True, in_park=False
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
            ┌───┴── start_is_pressed
                   and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=False, in_park=True
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
            ┌───┴── start_is_pressed
                   and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=False, in_park=False
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
            ┌───┴── start_is_pressed
                   and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=True, in_park=True
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
            ┌───┴── start_is_pressed
                   and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
    ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=True, in_park=False
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
            ┌───┴── start_is_pressed
                   and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
  • if the start button is pressed AND the key is NOT close to the ignition it leaves the if statements then returns False

    ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=False, in_park=False
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
                ├── start_is_pressed
            ┌───┴── and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
    ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=False, in_park=True
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
                ├── start_is_pressed
            ┌───┴── and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
    ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=True, in_park=True
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
                ├── start_is_pressed
            ┌───┴── and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
    ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=True, in_park=False
    ) -> False
    └── def ignition(
                start_is_pressed, key_is_close=False,
                brake_is_pressed=False, in_park=False,
            ):
            └── if (
                ├── start_is_pressed
            ┌───┴── and key_is_close
                   and brake_is_pressed
               ):
                   return in_park
            └── return False
    
  • if the start button is pressed AND the key is close to the ignition it checks if the brake is being pressed

    • if the start button is pressed AND the key is close to the ignition AND the brake is NOT pressed it leaves the if statement then returns False

      ignition(
          start_is_pressed=True, key_is_close=True,
          brake_is_pressed=False, in_park=True
      ) -> False
      └── def ignition(
                  start_is_pressed, key_is_close=False,
                  brake_is_pressed=False, in_park=False,
              ):
              └── if (
                  ├── start_is_pressed
                  ├── and key_is_close
              ┌───┴── and brake_is_pressed
                 ):
                     return in_park
              └── return False
      
      ignition(
          start_is_pressed=True, key_is_close=True,
          brake_is_pressed=False, in_park=False
      ) -> False
      └── def ignition(
                  start_is_pressed, key_is_close=False,
                  brake_is_pressed=False, in_park=False,
              ):
              └── if (
                  ├── start_is_pressed
                  ├── and key_is_close
              ┌───┴── and brake_is_pressed
                 ):
                     return in_park
              └── return False
      
    • if the start button is pressed AND the key is close to the ignition AND the brake is pressed it returns the value of in_park

      • if the car gear is NOT in park it returns False

        ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=True, in_park=False
        ) -> False
        └── def ignition(
                    start_is_pressed, key_is_close=False,
                    brake_is_pressed=False, in_park=False,
                ):
                └── if (
                    ├── start_is_pressed
                    ├── and key_is_close
                    └── and brake_is_pressed
                    ):
                    └── return in_park
                        return False
                    return False
        
      • if the car gear is in park it returns True

        ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=True, in_park=True
        ) -> True
        └── def ignition(
                    start_is_pressed, key_is_close=False,
                    brake_is_pressed=False, in_park=False,
                ):
                └── if (
                    ├── start_is_pressed
                    ├── and key_is_close
                    └── and brake_is_pressed
                    ):
                    └── return in_park
                        return True
                    return False
        

close the project

  • I close test_car.py and car.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 car with these inputs:

  • was the start button pressed?

  • is the key close to the ignition?

  • is the brake being pressed?

  • is the car in park?

the inputs gave me this truth table

key

brake

start

gear

output

close

pressed

pressed

in park

True

close

pressed

pressed

NOT in park

False

close

NOT pressed

pressed

in park

False

close

NOT pressed

pressed

NOT in park

False

key

brake

start

gear

output

NOT close

pressed

pressed

in park

False

NOT close

pressed

pressed

NOT in park

False

NOT close

NOT pressed

pressed

in park

False

NOT close

NOT pressed

pressed

NOT in park

False

key

brake

start

gear

output

close

pressed

NOT pressed

in park

False

close

pressed

NOT pressed

NOT in park

False

close

NOT pressed

NOT pressed

in park

False

close

NOT pressed

NOT pressed

NOT in park

False

key

brake

start

gear

output

NOT close

pressed

NOT pressed

in park

False

NOT close

pressed

NOT pressed

NOT in park

False

NOT close

NOT pressed

NOT pressed

in park

False

NOT close

NOT pressed

NOT pressed

NOT in park

False

the only time I can start this car is if the start button is pressed AND the key is close to the ignition AND the brake is being pressed AND the car gear is in park.


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 an Elevator?


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.