booleans 2: test with bool

I want to use the bool built-in function with the assertFalse method in test_what_is_false and the assertTrue method in test_what_is_true.

The bool function tells if Python groups the thing in parentheses as True or False. Which means the result of bool(something) is True or False.


preview

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

 1import unittest
 2
 3
 4class TestBooleans(unittest.TestCase):
 5
 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):
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
50# a dictionary with things is True
51# a set with things is True
52# a list with things is True
53# a tuple with things is True
54# a string with things is True
55# positive and negative numbers are True
56# True is True
57# True is not false
58# True is a boolean
59# the empty dictionary is False
60# the empty set is False
61# the empty list is False
62# the empty tuple is False
63# the empty string is False
64# 0 is False
65# None is False
66# False is False
67# False is not true
68# False is a boolean
69
70
71# Exceptions seen
72# 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 is my friend, and 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)
12        self.assertFalse(0.0)
13        self.assertFalse(str())
14        self.assertFalse(tuple())
15        self.assertFalse(list())
16        self.assertFalse(set())
17        self.assertFalse(dict())
18
19    def test_what_is_true(self):

the terminal is my friend, and 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)
    14        self.assertFalse(str())
    15        self.assertFalse(tuple())
    16        self.assertFalse(list())
    17        self.assertFalse(set())
    18        self.assertFalse(dict())
    19
    20    def test_what_is_true(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    the result of bool(0) is False

  • I change the assertion

    12        self.assertFalse(bool(0))
    

    the test passes

  • I add a failing 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.assertTrue(bool(0.0))
    15        self.assertFalse(str())
    16        self.assertFalse(tuple())
    17        self.assertFalse(list())
    18        self.assertFalse(set())
    19        self.assertFalse(dict())
    20
    21    def test_what_is_true(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    the result of bool(0.0) is False

  • I change the assertion

    14        self.assertFalse(bool(0.0))
    

    the test passes

  • I add an 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.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 is my friend, and shows AssertionError

    AssertionError: False is not true
    

    the result of bool(str()) is False

  • 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 is my friend, and shows AssertionError

    AssertionError: False is not true
    

    the result of bool(tuple()) is False

  • I change the assertion

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

    the test passes

  • I add another failing assertion

    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 is my friend, and shows AssertionError

    AssertionError: False is not true
    

    the result of bool(list()) is False

  • I make the assertion True

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

    the test passes

  • I add another failing assertion

    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 is my friend, and shows AssertionError

    AssertionError: False is not true
    

    the result of bool(set()) is False

  • 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 is my friend, and shows AssertionError

    AssertionError: False is not true
    

    the result of bool(dict()) is False

  • 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

  • The assertFalse method uses an if statement to check if what it gets as input is False, this is what the code looks like

    def assertFalse(self, something, message=None):
        """Check that something is false."""
        if something:
            raise AssertionError(message)
    

    here is what happens when the assertFalse method is called

    click here to see the actual code for the assertFalse method


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 is my friend, and shows AssertionError

AssertionError: True is not false

the result of bool(-1) is True


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 is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the result of bool(1) is True

  • I change the assertion

    32        self.assertTrue(bool(1))
    

    the test passes

  • 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.assertFalse(bool(-0.1))
    35        self.assertTrue(0.1)
    36        self.assertTrue("text")
    37        self.assertTrue((1, 2, 3, 'n'))
    38        self.assertTrue([1, 2, 3, 'n'])
    39        self.assertTrue({1, 2, 3, 'n'})
    40        self.assertTrue({'key': 'value'})
    41
    42
    43# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the result of bool(-0.1) is True

  • 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 is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the result of bool(0.1) is True

  • 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'})
    43
    44
    45# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the result of bool("text") is True

  • I change assertFalse to assertTrue

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

    the test 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'})
    44
    45
    46# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the result of bool((1, 2, 3, 'n')) is True

  • 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'})
    45
    46
    47# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the result of bool([1, 2, 3, 'n']) is True

  • 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 is my friend, and shows AssertionError

    AssertionError: True is not
    

    the result of bool({1, 2, 3, 'n'}) is True

  • 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 is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the result of bool({'key': 'value'}) is True

  • 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

  • The assertTrue method uses an if statement to check if what it gets as input is False, this is what the code looks like

    def assertTrue(self, something, message=None):
        """Check that something is true."""
        if not something:
            raise AssertionError(message)
    

    here is what happens when the assertTrue method is called

    click here to see the actual code for the assertTrue method


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) == assertFalse(bool(anything))

  • assertFalse(anything) checks if the result of bool(anything) is True and raises AssertionError if it is

  • assertTrue(anything) == assertTrue(bool(anything))

  • assertTrue(anything) checks if the result of bool(anything) is False and it raises AssertionError if it is

you can take a look at the source code here


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