truth table: test binary operations with lists

I want to use for loops in the truth table project for binary operations

preview

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


open the project


test_binary_operations

RED: make it fail


I add a test with a for loop for all the binary operations in tests/test_binary.py

truth_table/tests/test_binary.py
11class TestBinaryOperations(unittest.TestCase):
12
13    def test_binary_operations(self):
14        for operation, tt_out, tf_out, ft_out, ff_out in (
15            (src.truth_table.contradiction, False, False, False, False),
16            (src.truth_table.logical_conjunction, True, False, False, False),
17            (src.truth_table.project_second, True, False, True, False),
18            (src.truth_table.converse_non_implication, False, False, True, False),
19            (src.truth_table.negate_first, False, False, True, True),
20            (src.truth_table.logical_nand, False, True, True, True),
21            (src.truth_table.tautology, True, True, True, True),
22            (src.truth_table.logical_disjunction, True, True, True, False),
23            (src.truth_table.exclusive_disjunction, False, True, True, False),
24            (src.truth_table.material_non_implication, False, True, False, False),
25            (src.truth_table.project_first, True, True, False, False),
26            (src.truth_table.converse_implication, True, True, False, True),
27            (src.truth_table.negate_second, False, True, False, True),
28            (src.truth_table.logical_nor, False, False, False, True),
29            (src.truth_table.logical_equality, True, False, False, True),
30            (src.truth_table.material_implication, True, False, True, True),
31        ):
truth_table/tests/test_binary.py
32            with self.subTest(operation=operation.__name__):
33                self.assertEqual(operation(*FF), 'BOOM!!!')
34                self.assertEqual(operation(*FT), 'BAP!!!')
35                self.assertEqual(operation(*TF), 'POW!!!')
36                self.assertEqual(operation(*TT), 'TING!!!')
37
38    def test_contradiction(self):

the terminal is my friend, and shows AssertionError

terminal
SUBFAILED(operation='contradiction') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='logical_conjunction') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='project_second') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='converse_non_implication') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='negate_first') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='logical_nand') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='tautology') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='logical_disjunction') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='exclusive_disjunction') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='material_non_implication') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='project_first') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='converse_implication') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='negate_second') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='logical_nor') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='logical_equality') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='material_implication') ...AssertionError: True != 'BOOM!!!'

GREEN: make it pass


  • I change the expectation of self.assertEqual(operation(*FF), 'BOOM!!!')

    truth_table/tests/test_binary.py
    32            with self.subTest(operation=operation.__name__):
    33                self.assertEqual(operation(*FF), ff_out)
    34                self.assertEqual(operation(*FT), 'BAP!!!')
    

    the terminal is my friend, and shows AssertionError

    terminal
    SUBFAILED(operation='contradiction') ...AssertionError: False != 'BAP!!!'
    SUBFAILED(operation='logical_conjunction') ...AssertionError: False != 'BAP!!!'
    SUBFAILED(operation='project_second') ...AssertionError: True != 'BAP!!!'
    SUBFAILED(operation='converse_non_implication') ...AssertionError: True != 'BAP!!!'
    SUBFAILED(operation='negate_first') ...AssertionError: True != 'BAP!!!'
    SUBFAILED(operation='logical_nand') ...AssertionError: True != 'BAP!!!'
    SUBFAILED(operation='tautology') ...AssertionError: True != 'BAP!!!'
    SUBFAILED(operation='logical_disjunction') ...AssertionError: True != 'BAP!!!'
    SUBFAILED(operation='exclusive_disjunction') ...AssertionError: True != 'BAP!!!'
    SUBFAILED(operation='material_non_implication') ...AssertionError: False != 'BAP!!!'
    SUBFAILED(operation='project_first') ...AssertionError: False != 'BAP!!!'
    SUBFAILED(operation='converse_implication') ...AssertionError: False != 'BAP!!!'
    SUBFAILED(operation='negate_second') ...AssertionError: False != 'BAP!!!'
    SUBFAILED(operation='logical_nor') ...AssertionError: False != 'BAP!!!'
    SUBFAILED(operation='logical_equality') ...AssertionError: False != 'BAP!!!'
    SUBFAILED(operation='material_implication') ...AssertionError: True != 'BAP!!!'
    
  • I change the expectation of self.assertEqual(operation(*FT), 'BAP!!!')

    truth_table/tests/test_binary.py
    32            with self.subTest(operation=operation.__name__):
    33                self.assertEqual(operation(*FF), ff_out)
    34                self.assertEqual(operation(*FT), ft_out)
    35                self.assertEqual(operation(*TF), 'POW!!!')
    

    the terminal is my friend, and shows AssertionError

    terminal
    SUBFAILED(operation='contradiction') ...AssertionError: False != 'POW!!!'
    SUBFAILED(operation='logical_conjunction') ...AssertionError: False != 'POW!!!'
    SUBFAILED(operation='project_second') ...AssertionError: False != 'POW!!!'
    SUBFAILED(operation='converse_non_implication') ...AssertionError: False != 'POW!!!'
    SUBFAILED(operation='negate_first') ...AssertionError: False != 'POW!!!'
    SUBFAILED(operation='logical_nand') ...AssertionError: True != 'POW!!!'
    SUBFAILED(operation='tautology') ...AssertionError: True != 'POW!!!'
    SUBFAILED(operation='logical_disjunction') ...AssertionError: True != 'POW!!!'
    SUBFAILED(operation='exclusive_disjunction') ...AssertionError: True != 'POW!!!'
    SUBFAILED(operation='material_non_implication') ...AssertionError: True != 'POW!!!'
    SUBFAILED(operation='project_first') ...AssertionError: True != 'POW!!!'
    SUBFAILED(operation='converse_implication') ...AssertionError: True != 'POW!!!'
    SUBFAILED(operation='negate_second') ...AssertionError: True != 'POW!!!'
    SUBFAILED(operation='logical_nor') ...AssertionError: False != 'POW!!!'
    SUBFAILED(operation='logical_equality') ...AssertionError: False != 'POW!!!'
    SUBFAILED(operation='material_implication') ...AssertionError: False != 'POW!!!'
    
  • I change the expectation of self.assertEqual(operation(*TF), 'POW!!!')

    truth_table/tests/test_binary.py
    32            with self.subTest(operation=operation.__name__):
    33                self.assertEqual(operation(*FF), ff_out)
    34                self.assertEqual(operation(*FT), ft_out)
    35                self.assertEqual(operation(*TF), tf_out)
    36                self.assertEqual(operation(*TT), 'TING!!!')
    

    the terminal is my friend, and shows AssertionError

    terminal
    SUBFAILED(operation='contradiction') ...AssertionError: False != 'TING!!!'
    SUBFAILED(operation='logical_conjunction') ...AssertionError: True != 'TING!!!'
    SUBFAILED(operation='project_second') ...AssertionError: True != 'TING!!!'
    SUBFAILED(operation='converse_non_implication') ...AssertionError: False != 'TING!!!'
    SUBFAILED(operation='negate_first') ...AssertionError: False != 'TING!!!'
    SUBFAILED(operation='logical_nand') ...AssertionError: False != 'TING!!!'
    SUBFAILED(operation='tautology') ...AssertionError: True != 'TING!!!'
    SUBFAILED(operation='logical_disjunction') ...AssertionError: True != 'TING!!!'
    SUBFAILED(operation='exclusive_disjunction') ...AssertionError: False != 'TING!!!'
    SUBFAILED(operation='material_non_implication') ...AssertionError: False != 'TING!!!'
    SUBFAILED(operation='project_first') ...AssertionError: True != 'TING!!!'
    SUBFAILED(operation='converse_implication') ...AssertionError: True != 'TING!!!'
    SUBFAILED(operation='negate_second') ...AssertionError: False != 'TING!!!'
    SUBFAILED(operation='logical_nor') ...AssertionError: False != 'TING!!!'
    SUBFAILED(operation='logical_equality') ...AssertionError: True != 'TING!!!'
    SUBFAILED(operation='material_implication') ...AssertionError: True != 'TING!!!'
    
  • I change the expectation of self.assertEqual(operation(*TT), 'TING!!!')

    truth_table/tests/test_binary.py
    32            with self.subTest(operation=operation.__name__):
    33                self.assertEqual(operation(*FF), ff_out)
    34                self.assertEqual(operation(*FT), ft_out)
    35                self.assertEqual(operation(*TF), tf_out)
    36                self.assertEqual(operation(*TT), tt_out)
    37
    38    def test_contradiction(self):
    

    the test passes.


REFACTOR: make it better


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

    truth_table/tests/test_binary.py
    32            with self.subTest(operation=operation.__name__):
    33                self.assertEqual(operation(*FF), ff_out)
    34                self.assertEqual(operation(*FT), ft_out)
    35                self.assertEqual(operation(*TF), tf_out)
    36                self.assertEqual(operation(*TT), tt_out)
    37
    38
    39# Exceptions seen
    40# AttributeError
    41# TypeError
    42# AssertionError
    43# SyntaxError
    
  • I change the assertions in test_binary_operations with another for loop

    truth_table/tests/test_binary.py
    13    def test_binary_operations(self):
    14        for operation, tt_out, tf_out, ft_out, ff_out in (
    15            (src.truth_table.contradiction, False, False, False, False),
    16            (src.truth_table.logical_conjunction, True, False, False, False),
    17            (src.truth_table.project_second, True, False, True, False),
    18            (src.truth_table.converse_non_implication, False, False, True, False),
    19            (src.truth_table.negate_first, False, False, True, True),
    20            (src.truth_table.logical_nand, False, True, True, True),
    21            (src.truth_table.tautology, True, True, True, True),
    22            (src.truth_table.logical_disjunction, True, True, True, False),
    23            (src.truth_table.exclusive_disjunction, False, True, True, False),
    24            (src.truth_table.material_non_implication, False, True, False, False),
    25            (src.truth_table.project_first, True, True, False, False),
    26            (src.truth_table.converse_implication, True, True, False, True),
    27            (src.truth_table.negate_second, False, True, False, True),
    28            (src.truth_table.logical_nor, False, False, False, True),
    29            (src.truth_table.logical_equality, True, False, False, True),
    30            (src.truth_table.material_implication, True, False, True, True),
    31        ):
    
    truth_table/tests/test_binary.py
    32            for inputs, result in (
    33                (FF, ff_out),
    34                (FT, ft_out),
    35                (TF, tf_out),
    36                (TT, tt_out)
    37            ):
    38                with self.subTest(
    39                    operation=operation.__name__,
    40                    inputs=inputs, result=result
    41                ):
    42                    self.assertEqual(operation(*inputs), 'BOOM!!!')
    43
    44
    45# Exceptions seen
    

    the terminal shows AssertionError for sixty four sub tests.

  • I change the expectation of self.assertEqual(operation(*inputs), 'BOOM!!!')

    truth_table/tests/test_binary.py
    38                with self.subTest(
    39                    operation=operation.__name__,
    40                    inputs=inputs, result=result
    41                ):
    42                    self.assertEqual(operation(*inputs), result)
    43
    44
    45# Exceptions seen
    

    the test is green again.

  • I no longer need the TT, TF, FT and FF global variables because they are only used in one place. I write out their values

    truth_table/tests/test_binary.py
    32            for inputs, result in (
    33                ((False, False), ff_out),
    34                ((False, True), ft_out),
    35                ((True, False), tf_out),
    36                ((True, True), tt_out)
    37            ):
    38                with self.subTest(
    39                    operation=operation.__name__,
    40                    inputs=inputs, result=result
    41                ):
    42                    self.assertEqual(operation(*inputs), result)
    43
    44
    45# Exceptions seen
    
  • I remove the TT, TF, FT and FF global variables because they are no longer used

    truth_table/tests/test_binary.py
    1import src.truth_table
    2import unittest
    3
    4
    5class TestBinaryOperations(unittest.TestCase):
    
  • I add a git commit message

    terminal
    git commit -am \
    'extract test_binary_operations'
    

refactor test_binary_operations

RED: make it fail


I rewrite the test a different way without listing four booleans for each operation

truth_table/tests/test_binary.py
 5class TestBinaryOperations(unittest.TestCase):
 6
 7    def test_binary_operations(self):
 8        booleans = (False, True)
 9        for first in booleans:
10            for second in booleans:
11                for operation, expectation in (
12                    (src.truth_table.contradiction, 'BOOM!!!'),
13                    (src.truth_table.logical_conjunction, 'BAP!!!'),
14                    (src.truth_table.project_second, 'POW!!!'),
15                    (src.truth_table.converse_non_implication, 'BOOM!!!'),
16                    (src.truth_table.negate_first, 'BAP!!!'),
17                    (src.truth_table.logical_nand, 'POW!!!'),
18                    (src.truth_table.tautology, 'BOOM!!!'),
19                    (src.truth_table.logical_disjunction, 'BAP!!!'),
20                    (src.truth_table.exclusive_disjunction, 'POW!!!'),
21                    (src.truth_table.material_non_implication, 'BOOM!!!'),
22                    (src.truth_table.project_first, 'BAP!!!'),
23                    (src.truth_table.converse_implication, 'POW!!!'),
24                    (src.truth_table.negate_second, 'BOOM!!!'),
25                    (src.truth_table.logical_nor, 'BOOM!!!'),
26                    (src.truth_table.logical_equality, 'BAP!!!'),
27                    (src.truth_table.material_implication, 'BOOM!!!'),
28                ):
truth_table/tests/test_binary.py
29                    with self.subTest(
30                        operation.__name__,
31                        first=first,
32                        second=second
33                    ):
34                        self.assertEqual(
35                            operation(first, second),
36                            expectation
37                        )
38
39
40# Exceptions seen

the terminal shows AssertionError for sixty four sub tests. Can you make the tests pass without looking at how I solve it below? You can come back to compare solutions when you are done or if you get stuck.


GREEN: make it pass


  • I change the value of expectation for contradiction

    truth_table/tests/test_binary.py
    11                for operation, expectation in (
    12                    (src.truth_table.contradiction, False),
    13                    (src.truth_table.logical_conjunction, 'BAP!!!'),
    

    the terminal shows AssertionError for sixty sub tests.

  • I change the value of expectation for logical_conjunction

    truth_table/tests/test_binary.py
    11                for operation, expectation in (
    12                    (src.truth_table.contradiction, False),
    13                    (
    14                        src.truth_table.logical_conjunction,
    15                        (first, second) == (True, True)
    16                    ),
    17                    (src.truth_table.project_second, 'POW!!!'),
    

    the terminal shows AssertionError for fifty six sub tests.

  • I change the value of expectation for project_second

    truth_table/tests/test_binary.py
    13                    (
    14                        src.truth_table.logical_conjunction,
    15                        (first, second) == (True, True)
    16                    ),
    17                    (src.truth_table.project_second, second),
    18                    (src.truth_table.converse_non_implication, 'BOOM!!!'),
    

    the terminal shows AssertionError for fifty two sub tests.

  • I change the value of expectation for converse_non_implication

    truth_table/tests/test_binary.py
    17                    (src.truth_table.project_second, second),
    18                    (
    19                        src.truth_table.converse_non_implication,
    20                        (first, second) == (False, True)
    21                    ),
    22                    (src.truth_table.negate_first, 'BAP!!!'),
    

    the terminal shows AssertionError for forty eight sub tests.

  • I change the value of expectation for negate_first

    truth_table/tests/test_binary.py
    18                    (
    19                        src.truth_table.converse_non_implication,
    20                        (first, second) == (False, True)
    21                    ),
    22                    (src.truth_table.negate_first, not first),
    23                    (src.truth_table.logical_nand, 'POW!!!'),
    

    the terminal shows AssertionError for forty four sub tests.

  • I change the value of expectation for logical_nand

    truth_table/tests/test_binary.py
    22                    (src.truth_table.negate_first, not first),
    23                    (
    24                        src.truth_table.logical_nand,
    25                        (first, second) != (True, True)
    26                    ),
    27                    (src.truth_table.tautology, 'BOOM!!!'),
    

    the terminal shows AssertionError for forty sub tests.

  • I change the value of expectation for tautology

    truth_table/tests/test_binary.py
    23                    (
    24                        src.truth_table.logical_nand,
    25                        (first, second) != (True, True)
    26                    ),
    27                    (src.truth_table.tautology, True),
    28                    (src.truth_table.logical_disjunction, 'BAP!!!'),
    

    the terminal shows AssertionError for thirty six sub tests.

  • I change the value of expectation for logical_disjunction

    truth_table/tests/test_binary.py
    27                    (src.truth_table.tautology, True),
    28                    (
    29                        src.truth_table.logical_disjunction,
    30                        (first, second) != (False, False)
    31                    ),
    32                    (src.truth_table.exclusive_disjunction, 'POW!!!'),
    

    the terminal shows AssertionError for thirty two sub tests.

  • I change the value of expectation for exclusive_disjunction

    truth_table/tests/test_binary.py
    28                    (
    29                        src.truth_table.logical_disjunction,
    30                        (first, second) != (False, False)
    31                    ),
    32                    (
    33                        src.truth_table.exclusive_disjunction,
    34                        first != second
    35                    ),
    36                    (src.truth_table.material_non_implication, 'BOOM!!!'),
    

    the terminal shows AssertionError for twenty eight sub tests.

  • I change the value of expectation for material_non_implication

    truth_table/tests/test_binary.py
    32                    (
    33                        src.truth_table.exclusive_disjunction,
    34                        first != second
    35                    ),
    36                    (
    37                        src.truth_table.material_non_implication,
    38                        (first, second) == (True, False)
    39                    ),
    40                    (src.truth_table.project_first, 'BAP!!!'),
    

    the terminal shows AssertionError for twenty four sub tests.

  • I change the value of expectation for project_first

    truth_table/tests/test_binary.py
    36                    (
    37                        src.truth_table.material_non_implication,
    38                        (first, second) == (True, False)
    39                    ),
    40                    (src.truth_table.project_first, first),
    41                    (src.truth_table.converse_implication, 'POW!!!'),
    

    the terminal shows AssertionError for twenty sub tests.

  • I change the value of expectation for converse_implication

    truth_table/tests/test_binary.py
    40                    (src.truth_table.project_first, first),
    41                    (
    42                        src.truth_table.converse_implication,
    43                        (first, second) != (False, True)
    44                    ),
    45                    (src.truth_table.negate_second, 'BOOM!!!'),
    

    the terminal shows AssertionError for sixteen sub tests.

  • I change the value of expectation for negate_second

    truth_table/tests/test_binary.py
    41                    (
    42                        src.truth_table.converse_implication,
    43                        (first, second) != (False, True)
    44                    ),
    45                    (src.truth_table.negate_second, not second),
    46                    (src.truth_table.logical_nor, 'BOOM!!!'),
    

    the terminal shows AssertionError for twelve sub tests.

  • I change the value of expectation for logical_nor

    truth_table/tests/test_binary.py
    45                    (src.truth_table.negate_second, not second),
    46                    (
    47                        src.truth_table.logical_nor,
    48                        (first, second) == (False, False)
    49                    ),
    50                    (src.truth_table.logical_equality, 'BAP!!!'),
    

    the terminal shows AssertionError for eight sub tests.

  • I change the value of expectation for logical_equality

    truth_table/tests/test_binary.py
    46                    (
    47                        src.truth_table.logical_nor,
    48                        (first, second) == (False, False)
    49                    ),
    50                    (
    51                        src.truth_table.logical_equality,
    52                        first == second
    53                    ),
    54                    (src.truth_table.material_implication, 'BOOM!!!'),
    

    the terminal shows AssertionError for four sub tests.

  • I change the value of expectation for material_implication

    truth_table/tests/test_binary.py
    50                    (
    51                        src.truth_table.logical_equality,
    52                        first == second
    53                    ),
    54                    (
    55                        src.truth_table.material_implication,
    56                        (first, second) != (True, False)
    57                    ),
    58                ):
    59                    with self.subTest(
    60                        operation.__name__,
    61                        first=first,
    62                        second=second
    63                    ):
    

    the test passes.

  • I add a git commit message

    terminal
    git commit -am \
    'refactor test_binary_operations'
    

close the project

  • I close tests/test_binary.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 used for loops to turn all the tests into one which is simpler and at the same time more complex.


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 lists?


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.