booleans 2: test with bool

I used bool the way I used the assertFalse method in test_what_is_false and the assertTrue method in test_what_is_true while I was refactoring if statements in Truth Table: Binary Operations 1.

I want to practice using bool in test_what_is_true and test_what_is_false.


preview

Here are the tests I have at the end of this chapter

 1import unittest
 2
 3
 4class TestBooleans(unittest.TestCase):
 5
 6    def test_what_is_false(self):
 7        self.assertIsInstance(False, (bool, int))
 8        self.assertNotIsInstance(False, float)
 9
10        for false_item in (
11            False,
12            None, bool(None),
13            0, 0.0, bool(0), bool(0.0),
14            str(), bool(str()),
15            tuple(), bool(tuple()),
16            list(), bool(list()),
17            set(), bool(set()),
18            dict(), bool(dict()),
19        ):
20            with self.subTest(item=false_item):
21                self.assertFalse(false_item)
22
23    def test_what_is_true(self):
24        self.assertIsInstance(True, (bool, int))
25        self.assertNotIsInstance(True, float)
26
27        for true_item in (
28            True,
29            -1, bool(-1), 1, bool(1),
30            -0.1, bool(-0.1), 0.1, bool(0.1),
31            "text", bool("text"),
32            ((1, 2, 3, 'n')), bool((1, 2, 3, 'n')),
33            [1, 2, 3, 'n'], bool([1, 2, 3, 'n']),
34            {1, 2, 3, 'n'}, bool({1, 2, 3, 'n'}),
35            {'key': 'value'}, bool({'key': 'value'}),
36        ):
37            with self.subTest(item=true_item):
38                self.assertTrue(true_item)
39
40    def test_the_value_of_false(self):
41        self.assertEqual(False+1, 1)
42        self.assertEqual(False-1, -1)
43        self.assertEqual(False*1, 0)
44        with self.assertRaises(ZeroDivisionError):
45            1 / False
46
47    def test_the_value_of_true(self):
48        self.assertEqual(True+1, 2)
49        self.assertEqual(True-1, 0)
50        self.assertEqual(True*1, 1)
51        self.assertEqual(True/1, 1)
52
53
54# NOTES
55# a dictionary with things is True
56# a set with things is True
57# a list with things is True
58# a tuple with things is True
59# a string with things is True
60# positive and negative numbers are True
61# True is True
62# True is not false
63# True is a boolean
64# True is an integer
65# True is not a float
66# True is 1
67# the empty dictionary is False
68# the empty set is False
69# the empty list is False
70# the empty tuple is False
71# the empty string is False
72# 0 is False
73# None is False
74# False is False
75# False is not true
76# False is a boolean
77# False is an integer
78# False is not a float
79# False is 0
80
81
82# Exceptions seen
83# AssertionError

open the project

  • I change directory to the booleans folder

    cd booleans
    

    the terminal shows I am in the booleans folder

    .../pumping_python/booleans
    
  • I use pytest-watcher to run the tests

    uv run pytest-watcher . --now
    

    the terminal shows

    rootdir: .../pumping_python/booleans
    configfile: pyproject.toml
    collected 2 items
    
    tests/test_booleans.py ..                                     [100%]
    
    ======================== 2 passed in X.YZs =========================
    
  • I hold ctrl on the keyboard, then click on tests/test_booleans.py to open it in the editor


bool at work


RED: make it fail


I add a new assertion to the test_what_is_false method

 6    def test_what_is_false(self):
 7        self.assertIsInstance(False, bool)
 8        self.assertFalse(False)
 9        self.assertFalse(None)
10        self.assertTrue(bool(None))
11        self.assertFalse(0)

the terminal shows AssertionError

AssertionError: False is not true

the result of bool(None) is False


GREEN: make it pass


I change assertTrue to assertFalse

10        self.assertFalse(bool(None))

the test passes


REFACTOR: make it better


  • I add another assertion with bool

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(bool(None))
    11        self.assertFalse(0)
    12        self.assertTrue(bool(0))
    13        self.assertFalse(0.0)
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I change the assertion

    12        self.assertFalse(bool(0))
    

    the test passes

  • I add an assert method

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(bool(None))
    11        self.assertFalse(0)
    12        self.assertFalse(bool(0))
    13        self.assertFalse(0.0)
    14        self.assertTrue(bool(0.0))
    15        self.assertFalse(str())
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I change the assertion

    14        self.assertFalse(bool(0.0))
    

    the test passes

  • I add an assertion

    15        self.assertFalse(str())
    16        self.assertTrue(bool(str()))
    17        self.assertFalse(tuple())
    18        self.assertFalse(list())
    19        self.assertFalse(set())
    20        self.assertFalse(dict())
    21
    22    def test_what_is_true(self):
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I change assertTrue to assertFalse

    16        self.assertFalse(bool(str()))
    

    the test passes

  • I add another assertion

    15        self.assertFalse(str())
    16        self.assertFalse(bool(str()))
    17        self.assertFalse(tuple())
    18        self.assertTrue(bool(tuple()))
    19        self.assertFalse(list())
    20        self.assertFalse(set())
    21        self.assertFalse(dict())
    22
    23    def test_what_is_true(self):
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I change the assertion

    18        self.assertFalse(bool(tuple()))
    

    the test passes

  • I add another line

    15        self.assertFalse(str())
    16        self.assertFalse(bool(str()))
    17        self.assertFalse(tuple())
    18        self.assertFalse(bool(tuple()))
    19        self.assertFalse(list())
    20        self.assertTrue(bool(list()))
    21        self.assertFalse(set())
    22        self.assertFalse(dict())
    23
    24    def test_what_is_true(self):
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I make the assertion True

    20    self.assertFalse(bool(list()))
    

    the test passes

  • I add another line

    19        self.assertFalse(list())
    20        self.assertFalse(bool(list()))
    21        self.assertFalse(set())
    22        self.assertTrue(bool(set()))
    23        self.assertFalse(dict())
    24
    25    def test_what_is_true(self):
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I change assertTrue to assertFalse

    22        self.assertFalse(bool(set()))
    

    the test passes

  • One more

    19        self.assertFalse(list())
    20        self.assertFalse(bool(list()))
    21        self.assertFalse(set())
    22        self.assertFalse(bool(set()))
    23        self.assertFalse(dict())
    24        self.assertTrue(bool(dict()))
    25
    26    def test_what_is_true(self):
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I change the assertion

     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, bool)
     8        self.assertFalse(False)
     9        self.assertFalse(None)
    10        self.assertFalse(bool(None))
    11        self.assertFalse(0)
    12        self.assertFalse(bool(0))
    13        self.assertFalse(0.0)
    14        self.assertFalse(bool(0.0))
    15        self.assertFalse(str())
    16        self.assertFalse(bool(str()))
    17        self.assertFalse(tuple())
    18        self.assertFalse(bool(tuple()))
    19        self.assertFalse(list())
    20        self.assertFalse(bool(list()))
    21        self.assertFalse(set())
    22        self.assertFalse(bool(set()))
    23        self.assertFalse(dict())
    24        self.assertFalse(bool(dict()))
    25
    26    def test_what_is_true(self):
    

    the test passes


more bool at work

Oh No! More of the same thing


RED: make it fail


I add an assertion with bool to test_what_is_true

26    def test_what_is_true(self):
27        self.assertIsInstance(True, bool)
28        self.assertTrue(True)
29        self.assertTrue(-1)
30        self.assertFalse(bool(-1))
31        self.assertTrue(1)
32        self.assertTrue(-0.1)
33        self.assertTrue(0.1)
34        self.assertTrue("text")
35        self.assertTrue((1, 2, 3, 'n'))
36        self.assertTrue([1, 2, 3, 'n'])
37        self.assertTrue({1, 2, 3, 'n'})
38        self.assertTrue({'key': 'value'})
39
40
41# NOTES

the terminal shows AssertionError

AssertionError: True is not false

GREEN: make it pass


I change assertFalse to assertTrue

30        self.assertTrue(bool(-1))

the test passes


REFACTOR: make it better


  • on to the next one

    26    def test_what_is_true(self):
    27        self.assertIsInstance(True, bool)
    28        self.assertTrue(True)
    29        self.assertTrue(-1)
    30        self.assertTrue(bool(-1))
    31        self.assertTrue(1)
    32        self.assertFalse(bool(1))
    33        self.assertTrue(-0.1)
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I change the assertion

    32        self.assertTrue(bool(1))
    

    the test passes. The result of bool(1) is True

  • I add another assertion

    26    def test_what_is_true(self):
    27        self.assertIsInstance(True, bool)
    28        self.assertTrue(True)
    29        self.assertTrue(-1)
    30        self.assertTrue(bool(-1))
    31        self.assertTrue(1)
    32        self.assertTrue(bool(1))
    33        self.assertTrue(-0.1)
    34        self.assertTrue(bool(-0.1))
    35        self.assertFalse(0.1)
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I change assertFalse to assertTrue

    34        self.assertTrue(bool(-0.1))
    

    the test passes

  • I add an assertion

    33        self.assertTrue(-0.1)
    34        self.assertTrue(bool(-0.1))
    35        self.assertTrue(0.1)
    36        self.assertFalse(bool(0.1))
    37        self.assertTrue("text")
    38        self.assertTrue((1, 2, 3, 'n'))
    39        self.assertTrue([1, 2, 3, 'n'])
    40        self.assertTrue({1, 2, 3, 'n'})
    41        self.assertTrue({'key': 'value'})
    42
    43# NOTES
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I change the assertion

    36        self.assertTrue(bool(0.1))
    

    the test passes

  • I add another one

    33        self.assertTrue(-0.1)
    34        self.assertTrue(bool(-0.1))
    35        self.assertTrue(0.1)
    36        self.assertTrue(bool(0.1))
    37        self.assertTrue("text")
    38        self.assertFalse(bool("text"))
    39        self.assertTrue((1, 2, 3, 'n'))
    40        self.assertTrue([1, 2, 3, 'n'])
    41        self.assertTrue({1, 2, 3, 'n'})
    42        self.assertTrue({'key': 'value'})
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I change assertFalse to assertTrue

    38        self.assertTrue(bool("text"))
    

    the tests passes

  • I add another assertion, when is this going to end?

    37        self.assertTrue("text")
    38        self.assertTrue(bool("text"))
    39        self.assertTrue((1, 2, 3, 'n'))
    40        self.assertFalse(bool((1, 2, 3, 'n')))
    41        self.assertTrue([1, 2, 3, 'n'])
    42        self.assertTrue({1, 2, 3, 'n'})
    43        self.assertTrue({'key': 'value'})
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I change assertFalse to assertTrue

    40        self.assertTrue(bool((1, 2, 3, 'n')))
    

    the test passes

  • another assertion

    37        self.assertTrue("text")
    38        self.assertTrue(bool("text"))
    39        self.assertTrue((1, 2, 3, 'n'))
    40        self.assertTrue(bool((1, 2, 3, 'n')))
    41        self.assertTrue([1, 2, 3, 'n'])
    42        self.assertFalse(bool([1, 2, 3, 'n']))
    43        self.assertTrue({1, 2, 3, 'n'})
    44        self.assertTrue({'key': 'value'})
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I change the assertion

    42        self.assertTrue(bool([1, 2, 3, 'n']))
    

    the test passes

  • I add the next assertion

    41        self.assertTrue([1, 2, 3, 'n'])
    42        self.assertTrue(bool([1, 2, 3, 'n']))
    43        self.assertTrue({1, 2, 3, 'n'})
    44        self.assertFalse(bool({1, 2, 3, 'n'}))
    45        self.assertTrue({'key': 'value'})
    46
    47
    48# NOTES
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I change assertFalse to assertTrue

    44        self.assertTrue(bool({1, 2, 3, 'n'}))
    

    the test passes

  • I add the last one. Finally!

    41        self.assertTrue([1, 2, 3, 'n'])
    42        self.assertTrue(bool([1, 2, 3, 'n']))
    43        self.assertTrue({1, 2, 3, 'n'})
    44        self.assertTrue(bool({1, 2, 3, 'n'}))
    45        self.assertTrue({'key': 'value'})
    46        self.assertFalse(bool({'key': 'value'}))
    47
    48
    49# NOTES
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I change assertFalse to assertTrue

    26    def test_what_is_true(self):
    27        self.assertIsInstance(True, bool)
    28        self.assertTrue(True)
    29        self.assertTrue(-1)
    30        self.assertTrue(bool(-1))
    31        self.assertTrue(1)
    32        self.assertTrue(bool(1))
    33        self.assertTrue(-0.1)
    34        self.assertTrue(bool(-0.1))
    35        self.assertTrue(0.1)
    36        self.assertTrue(bool(0.1))
    37        self.assertTrue("text")
    38        self.assertTrue(bool("text"))
    39        self.assertTrue((1, 2, 3, 'n'))
    40        self.assertTrue(bool((1, 2, 3, 'n')))
    41        self.assertTrue([1, 2, 3, 'n'])
    42        self.assertTrue(bool([1, 2, 3, 'n']))
    43        self.assertTrue({1, 2, 3, 'n'})
    44        self.assertTrue(bool({1, 2, 3, 'n'}))
    45        self.assertTrue({'key': 'value'})
    46        self.assertTrue(bool({'key': 'value'}))
    47
    48
    49# NOTES
    

    the test passes


close the project

  • I close test_booleans.py in the editor

  • I click in the terminal, then use q on the keyboard to leave the tests. The terminal goes back to the command line

  • I change directory to the parent of booleans

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory


review

  • bool(anything) returns True or False

  • assertFalse(anything) checks if the result of bool(anything) is False, it raises AssertionError if it is not

  • assertTrue(anything) checks if the result of bool(anything) is True, it raises AssertionError if it is not


code from the chapter

Do you want to see all the CODE I typed for the Truth Table?


what is next?

Would you like to continue testing binary operations?


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