test traffic_light with dictionaries


I want to use a generator in the Traffic Light project.

preview

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

traffic_light/tests/test_traffic_light.py
1import itertools
2import src.traffic_light
3import unittest
4
5
6GREEN, YELLOW, RED = 'GREEN', 'YELLOW', 'RED'
7WALK, NOT_WALK = 'WALK', 'DO NOT WALK'
traffic_light/tests/test_traffic_light.py
10class TestTrafficLight(unittest.TestCase):
11
12    def test_traffic_light_control(self):
13        states = {
14            'cross': (
15                (GREEN, RED, False, (GREEN, RED, WALK)),
16                (GREEN, RED, True, (YELLOW, RED, NOT_WALK)),
17                (YELLOW, RED, False, (YELLOW, RED, NOT_WALK)),
18                (YELLOW, RED, True, (RED, RED, NOT_WALK)),
19                (RED, RED, False, (RED, RED, NOT_WALK)),
20                (RED, RED, True, (RED, GREEN, NOT_WALK)),
21            ),
22            'parallel': (
23                (RED, GREEN, False, (RED, GREEN, NOT_WALK)),
24                (RED, GREEN, True, (RED, YELLOW, NOT_WALK)),
25                (RED, YELLOW, False, (RED, YELLOW, NOT_WALK)),
26                (RED, YELLOW, True, (RED, RED, NOT_WALK)),
27                (RED, RED, False, (RED, RED, NOT_WALK)),
28                (RED, RED, True, (GREEN, RED, WALK)),
29            )
30        }
31        for red_phase in states:
32            for (
33                current_parallel,
34                current_cross, timer_done, result
35            ) in states[red_phase]:
36                with self.subTest(
37                    red_phase=red_phase,
38                    current_parallel=current_parallel,
39                    current_cross=current_cross,
40                    timer_done=timer_done,
41                ):
42                    self.assertEqual(
43                        src.traffic_light.control(
44                            red_phase=red_phase,
traffic_light/tests/test_traffic_light.py
46                            current_cross=current_cross,
47                            timer_done=timer_done,
48                        ),
49                        result
50                    )
51
52    def test_traffic_light_failsafe(self):
53        inputs = (GREEN, YELLOW, 'bad input')
54        for (
55            red_phase, current_parallel,
56            current_cross, timer_done
57        ) in itertools.product(
58            ('cross', 'parallel', 'bad input'),
59            inputs, inputs, (False, True)
60        ):
61            with self.subTest(
62                red_phase=red_phase,
63                current_parallel=current_parallel,
64                current_cross=current_cross,
65                timer_done=timer_done,
66            ):
67                self.assertEqual(
68                    src.traffic_light.control(
69                        red_phase=red_phase,
70                        current_parallel=current_parallel,
71                        current_cross=current_cross,
72                        timer_done=timer_done,
73                    ),
74                    (RED, RED, NOT_WALK)
75                )
76
77
78# Exceptions seen
79# AssertionError
80# NameError
81# AttributeError
82# TypeError

open the project


refactor test_traffic_light_control

RED: make it fail


I change the for loop in test_traffic_light_control to use names instead of list indexes

traffic_light/tests/test_traffic_light.py
11    def test_traffic_light_control(self):
12        for (
13            red_phase, current_parallel,
14            current_cross, timer_done, expectation
15        ) in (
16            ('cross', GREEN, RED, False, (GREEN, RED, WALK)),
17            ('cross', GREEN, RED, True, (YELLOW, RED, NOT_WALK)),
18            ('cross', YELLOW, RED, False, (YELLOW, RED, NOT_WALK)),

the terminal is my friend, and shows NameError

terminal
NameError: name 'row' is not defined

GREEN: make it pass


  • I comment out the variables that use list indexes

    traffic_light/tests/test_traffic_light.py
    26            ('parallel', RED, YELLOW, True, (RED, RED, NOT_WALK)),
    27            ('parallel', RED, RED, False, (RED, RED, NOT_WALK)),
    28            ('parallel', RED, RED, True, (GREEN, RED, WALK)),
    29        ):
    30            # red_phase = row[0]
    31            # current_parallel = row[1]
    32            # current_cross = row[2]
    33            # timer_done = row[3]
    34
    35            with self.subTest(
    36                red_phase=red_phase,
    37                current_parallel=current_parallel,
    38                current_cross=current_cross,
    39                timer_done=timer_done,
    40            ):
    

    the terminal still shows NameError, this time for twelve sub tests.

  • I change row[4] to expectation to use the name instead

    traffic_light/tests/test_traffic_light.py
    35            with self.subTest(
    36                red_phase=red_phase,
    37                current_parallel=current_parallel,
    38                current_cross=current_cross,
    39                timer_done=timer_done,
    40            ):
    41                self.assertEqual(
    42                    src.traffic_light.control(
    43                        red_phase=red_phase,
    44                        current_parallel=current_parallel,
    45                        current_cross=current_cross,
    46                        timer_done=timer_done,
    47                    ),
    48                    expectation
    49                )
    50
    51    def test_traffic_light_failsafe(self):
    

    the test is green again.


REFACTOR: make it better

  • I remove the commented lines

    traffic_light/tests/test_traffic_light.py
    26        ('parallel', RED, YELLOW, True, (RED, RED, NOT_WALK)),
    27        ('parallel', RED, RED, False, (RED, RED, NOT_WALK)),
    28        ('parallel', RED, RED, True, (GREEN, RED, WALK)),
    29    ):
    30        with self.subTest(
    31            red_phase=red_phase,
    32            current_parallel=current_parallel,
    33            current_cross=current_cross,
    34            timer_done=timer_done,
    35        ):
    
  • I add a git commit message

    terminal
    git commit -am \
    'use names with test_traffic_light_control'
    

test_traffic_light_failsafe with dictionaries

RED: make it fail


I add a generator expression for the for loops in test_traffic_light_failsafe

traffic_light/tests/test_traffic_light.py
45    def test_traffic_light_failsafe(self):
46        inputs = (GREEN, YELLOW, 'bad input')
47        # for red_phase in ('cross', 'parallel', 'bad input'):
48        #     for current_parallel in inputs:
49        #         for current_cross in inputs:
50        #             for timer_done in (False, True):
51        for (
52            red_phase, current_parallel,
53            current_cross, timer_done
54        ) in (
55            (_, _, _, _)
56            for _ in ('cross', 'parallel', 'bad input')
57            for _ in inputs
58            for _ in inputs
59            for timer_done in (False, True)
60        ):
traffic_light/tests/test_traffic_light.py
61                        with self.subTest(
62                            red_phase=red_phase,
63                            current_parallel=current_parallel,
64                            current_cross=current_cross,
65                            timer_done=timer_done,
66                        ):
67                            raise Exception
68                            self.assertEqual(
69                                src.traffic_light.control(
70                                    red_phase=red_phase,
71                                    current_parallel=current_parallel,
72                                    current_cross=current_cross,
73                                    timer_done=timer_done,
74                                ),
75                                (RED, RED, NOT_WALK)
76                            )

the terminal is my friend, and shows Exception for fifty four sub tests.


GREEN: make it pass

I remove the Exception

traffic_light/tests/test_traffic_light.py
61                        with self.subTest(
62                            red_phase=red_phase,
63                            current_parallel=current_parallel,
64                            current_cross=current_cross,
65                            timer_done=timer_done,
66                        ):
67                            self.assertEqual(
68                                src.traffic_light.control(
69                                    red_phase=red_phase,
70                                    current_parallel=current_parallel,
71                                    current_cross=current_cross,
72                                    timer_done=timer_done,
73                                ),
74                                (RED, RED, NOT_WALK)
75                            )

the test is green again.


REFACTOR: make it better

  • I use itertools.product for the generator expression

    traffic_light/tests/test_traffic_light.py
    45    def test_traffic_light_failsafe(self):
    46        inputs = (GREEN, YELLOW, 'bad input')
    47        # for red_phase in ('cross', 'parallel', 'bad input'):
    48        #     for current_parallel in inputs:
    49        #         for current_cross in inputs:
    50        #             for timer_done in (False, True):
    51        # for (
    52        #     red_phase, current_parallel,
    53        #     current_cross, timer_done
    54        # ) in (
    55        #     (_, _, _, _)
    56        #     for _ in ('cross', 'parallel', 'bad input')
    57        #     for _ in inputs
    58        #     for _ in inputs
    59        #     for timer_done in (False, True)
    60        # ):
    
    traffic_light/tests/test_traffic_light.py
    61        for (
    62            red_phase, current_parallel,
    63            current_cross, timer_done
    64        ) in itertools.product(
    65            ('cross', 'parallel', 'bad input'),
    66            inputs, inputs, (False, True),
    67        ):
    68                        with self.subTest(
    69                            red_phase=red_phase,
    70                            current_parallel=current_parallel,
    71                            current_cross=current_cross,
    72                            timer_done=timer_done,
    73                        ):
    

    the terminal is my friend, and shows NameError

    terminal
    NameError: name 'itertools' is not defined.
               Did you forget to import 'itertools'?
    
  • I add an import statement for the itertools library

    traffic_light/tests/test_traffic_light.py
    1import itertools
    2import src.traffic_light
    3import unittest
    

    the test is green again.

  • I remove the commented lines, then fix indentation in test_traffic_light_failsafe

  • I add a git commit message

    terminal
    git commit -am \
    'test_traffic_light_failsafe with dictionaries'
    

close the project

  • I close tests/test_traffic_light.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests

  • I change directory to the parent of Traffic Light

    terminal
    cd ..
    

review

I can use a generator expression or itertools.product for nested for loops.


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 binary operations from the truth_table project with dictionaries?


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.