Car Ignition

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

    • 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

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

      .\makePythonTdd.ps1
      

    the terminal is my friend, and shows AssertionError

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

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

    the test passes.

  • I open a new terminal then change directory to car

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

    terminal
    git add .
    
  • I add a git commit message

    terminal
    git commit -am 'setup project'
    

I want the car to come ON 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

    the terminal is my friend, and shows NameError

    NameError: name 'src' is not defined
    

    because I do not have a definition for src in tests/test_car.py.


GREEN: make it pass


  • I add NameError to the list of Exceptions seen

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

  • I add AttributeError to the list of Exceptions seen

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

  • I delete all the text in the file then add a function named ignition to src/car/__init__.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 tests/test_car.py

  • 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

    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

    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

    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 come ON 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 start button is pressed AND the key is close to the ignition

    key

    start button

    output

    close

    pressed

    True

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



test_key_not_close_start_pressed

RED: make it fail



GREEN: make it pass


I add an if statement to src/car/__init__.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

    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

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



GREEN: make it pass


  • I add a return statement to the ignition function to make it clearer, in src/car/__init__.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 tests/test_car.py

    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

    src.car.ignition(
        key_is_close=False, start_is_pressed=False
    ) -> False
    └── src/car/__init__.py
        └── def ignition(start_is_pressed, key_is_close=False):
            ├── if start_is_pressed:
            │       return key_is_close
            └── return False
    
    src.car.ignition(
        key_is_close=True, start_is_pressed=False
    ) -> False
    └── src/car/__init__.py
        └── 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

      src.car.ignition(
          key_is_close=False, start_is_pressed=True
      ) -> False
      └── src/car/__init__.py
          └── 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

    src.car.ignition(
        key_is_close=True, start_is_pressed=True
    ) -> True
    └── src/car/__init__.py
        └── 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.


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 come ON 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

    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 src/car/__init__.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 tests/test_car.py

  • I add a default value for the brake_is_pressed parameter, in src/car/__init__.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



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

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

    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

  • 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

    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

    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

  • 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

    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

    the test passes.

  • I add a git commit message in the other terminal

    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

      src.car.ignition(
          start_is_pressed=True, key_is_close=False,
          brake_is_pressed=False
      ) -> False
      └── src/car/__init__.py
          └── 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
      
      src.car.ignition(
          start_is_pressed=True, key_is_close=False,
          brake_is_pressed=True
      ) -> False
      └── src/car/__init__.py
          └── 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

        src.car.ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=False
        ) -> False
        └── src/car/__init__.py
            └── 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

        src.car.ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=True
        ) -> True
        └── src/car/__init__.py
            └── 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


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

    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

    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

    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

    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

  • I add a git commit message in the other terminal

    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

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

GREEN: make it pass


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

  • If the start button is NOT pressed it returns False

    src.car.ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=False
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=True
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=False
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=True
    ) -> False
    └── src/car/__init__.py
        └── 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

      src.car.ignition(
          start_is_pressed=True, key_is_close=False,
          brake_is_pressed=False
      ) -> False
      └── src/car/__init__.py
          └── 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
      
      src.car.ignition(
          start_is_pressed=True, key_is_close=False,
          brake_is_pressed=True
      ) -> False
      └── src/car/__init__.py
          └── 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

        src.car.ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=False
        ) -> False
        └── src/car/__init__.py
            └── 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

        src.car.ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=True
        ) -> True
        └── src/car/__init__.py
            └── 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



GREEN: make it pass


  • I add in_park to the function signature in src/car/__init__.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

    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

    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 test_brake_not_pressed_key_not_close_start_not_pressed to test_park_w_brake_not_pressed_key_not_close_start_not_pressed

  • I add a git commit message in the other terminal

    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

    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

    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



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

    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

    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



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

    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

    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



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

    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

    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



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

    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

    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



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

    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

    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



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

    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

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


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

    src.car.ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=False, in_park=True
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=False, in_park=False
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=True, in_park=True
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=False, key_is_close=False,
        brake_is_pressed=True, in_park=False
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=False, in_park=True
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=False, in_park=False
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=True, in_park=True
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=False, key_is_close=True,
        brake_is_pressed=True, in_park=False
    ) -> False
    └── src/car/__init__.py
        └── 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

    src.car.ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=False, in_park=False
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=False, in_park=True
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=True, in_park=True
    ) -> False
    └── src/car/__init__.py
        └── 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
    
    src.car.ignition(
        start_is_pressed=True, key_is_close=False,
        brake_is_pressed=True, in_park=False
    ) -> False
    └── src/car/__init__.py
        └── 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

      src.car.ignition(
          start_is_pressed=True, key_is_close=True,
          brake_is_pressed=False, in_park=True
      ) -> False
      └── src/car/__init__.py
          └── 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
      
      src.car.ignition(
          start_is_pressed=True, key_is_close=True,
          brake_is_pressed=False, in_park=False
      ) -> False
      └── src/car/__init__.py
          └── 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

        src.car.ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=True, in_park=False
        ) -> False
        └── src/car/__init__.py
            └── 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

        src.car.ignition(
            start_is_pressed=True, key_is_close=True,
            brake_is_pressed=True, in_park=True
        ) -> True
        └── src/car/__init__.py
            └── 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 tests/test_car.py and src/car/__init__.py

  • I click in the terminal where the tests are running

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

    .../pumping_python
    

    I am back in the pumping_python directory.


review

I ran tests for a 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.