test microwave with list comprehensions

I want to use for loops in the Microwave project. It has tests for a Microwave with these inputs:

  • is the Microwave door closed?

  • is the timer set?

  • is the Microwave too hot?

  • was the start button pressed?

the inputs gave me this truth table

door

timer

start button

too hot

output

closed

set

pressed

too hot

False

closed

set

pressed

NOT too hot

True

closed

set

NOT pressed

too hot

False

closed

set

NOT pressed

NOT too hot

False

door

timer

start button

too hot

output

closed

NOT set

pressed

too hot

False

closed

NOT set

pressed

NOT too hot

False

closed

NOT set

NOT pressed

too hot

False

closed

NOT set

NOT pressed

NOT too hot

False

door

timer

start button

too hot

output

open

set

pressed

too hot

False

open

set

pressed

NOT too hot

False

open

set

NOT pressed

too hot

False

open

set

NOT pressed

NOT too hot

False

door

timer

start button

too hot

output

open

NOT set

pressed

too hot

False

open

NOT set

pressed

NOT too hot

False

open

NOT set

NOT pressed

too hot

False

open

NOT set

NOT pressed

NOT too hot

False

The only time this Microwave heats up food is when the Microwave door is closed AND the timer is set AND the Microwave temperature is NOT too hot AND the start button is pressed.

preview

These is the test I have at the end of the chapter

microwave/tests/test_microwave.py
 1import src.microwave
 2import unittest
 3
 4
 5class TestMicrowave(unittest.TestCase):
 6
 7    def test_microwave(self):
 8        booleans = (False, True)
 9        for closed_door in booleans:
10            for timer_set in booleans:
11                for pressed_start in booleans:
12                    for too_hot in booleans:
13                        with self.subTest(
14                            closed_door=closed_door,
15                            timer_set=timer_set,
16                            pressed_start=pressed_start,
17                            too_hot=too_hot,
18                        ):
19                            result = src.microwave.microwave(
20                                closed_door=closed_door,
21                                timer_set=timer_set,
22                                pressed_start=pressed_start,
23                                too_hot=too_hot,
24                            )
25                            try:
26                                self.assertFalse(result)
27                            except AssertionError:
28                                self.assertTrue(result)
29
30
31# Exceptions seen
32# AssertionError
33# NameError
34# AttributeError
35# TypeError
36# SyntaxError

open the project

  • I open a terminal_

  • I change directory_ to the project

    terminal
    cd microwave
    
  • I open test_microwave.py from the tests folder_

  • I use `pytest-watcher`_ to run the tests automatically

    terminal
    uv run pytest-watcher . --now
    

extract test_microwave

RED: make it fail


I add a test with for loops for the sixteen cases

microwave/tests/test_microwave.py
 5class TestMicrowave(unittest.TestCase):
 6
 7    def test_microwave(self):
 8        booleans = (False, True)
 9        for closed_door in booleans:
10            for timer_set in booleans:
11                for pressed_start in booleans:
12                    for too_hot in booleans:
13                        with self.subTest(
14                            closed_door=closed_door,
15                            timer_set=timer_set,
16                            pressed_start=pressed_start,
17                            too_hot=too_hot,
18                        ):
microwave/tests/test_microwave.py
19                            result = src.microwave.microwave(
20                                closed_door=closed_door,
21                                timer_set=timer_set,
22                                pressed_start=pressed_start,
23                                too_hot=too_hot,
24                            )
25                            self.assertEqual(result, 'BOOM!!!')
26
27    def test_too_hot_w_timer_set_closed_door_pressed_start(self):

the terminal_ shows AssertionError for all sixteen cases.


GREEN: make it pass


  • I change self.assertEqual(result, 'BOOM!!!') to handle all the fifteen cases where the Microwave does NOT heat up food

    microwave/tests/test_microwave.py
    19                            result = src.microwave.microwave(
    20                                closed_door=closed_door,
    21                                timer_set=timer_set,
    22                                pressed_start=pressed_start,
    23                                too_hot=too_hot,
    24                            )
    25                            self.assertFalse(result)
    26
    27    def test_too_hot_w_timer_set_closed_door_pressed_start(self):
    

    the terminal_ shows AttributeError for the one case where the Microwave will heat up food

  • I move the assertion into a try statement for it

    microwave/tests/test_microwave.py
    19                            result = src.microwave.microwave(
    20                                closed_door=closed_door,
    21                                timer_set=timer_set,
    22                                pressed_start=pressed_start,
    23                                too_hot=too_hot,
    24                            )
    25                            try:
    26                                self.assertFalse(result)
    27                            except AssertionError:
    28                                self.assertTrue(result)
    29
    30    def test_too_hot_w_timer_set_closed_door_pressed_start(self):
    

REFACTOR: make it better


  • I remove all the other tests because they are now covered by test_microwave

    microwave/tests/test_microwave.py
    25                            try:
    26                                self.assertFalse(result)
    27                            except AssertionError:
    28                                self.assertTrue(result)
    29
    30
    31# Exceptions seen
    
  • I add a git_ commit message

    terminal
    git commit -am 'extract test_microwave'
    

close the project

  • I close tests/test_microwave.py

  • I click in the terminal_ where the tests are running

  • I use q on the keyboard to leave the tests

  • I `change directory`_ back to the parent of Microwave

    terminal
    cd ..
    

review

Another test with nested for loops, there has to be a better way


code from the chapter

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


what is next?

Would you like to test making a Car starter?


rate pumping python

If this has been a 7 star experience for you, please `CLICK HERE to leave a 5 star review of pumping python`_. It helps other people get into the book too.