booleans 2

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.


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 and 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 and use q on the keyboard to leave the tests and 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