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
booleansfoldercd booleansthe terminal shows I am in the
booleansfolder.../pumping_python/booleansI use
pytest-watcherto run the testsuv run pytest-watcher . --nowthe 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.pyto 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 truethe result of
bool(0)is FalseI 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 truethe result of
bool(0.0)is FalseI 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 truethe result of
bool(str())is FalseI 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 truethe result of
bool(tuple())is FalseI 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 truethe result of
bool(list())is False-
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 truethe result of
bool(set())is FalseI 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 truethe result of
bool(dict())is FalseI 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
it raises AssertionError if
somethingis True, becauseif bool(something) == Trueis the same asif bool(something)is the same asif somethingit returns None if something is False because all functions return None by default, as if they have an invisible line that says return None
I do not have to give a value for the
messageparameter when I call assertFalse becauseself.assertFalse(something)is the same as
self.assertFalse(something, None)is the same as
self.assertFalse( something=something, message=None, )A function uses the default value for a parameter when it is called without the parameter
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 falsethe result of
bool(1)is TrueI 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# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: True is not falsethe result of
bool(-0.1)is TrueI 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# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: True is not falsethe result of
bool(0.1)is TrueI 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# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: True is not falsethe result of
bool("text")is TrueI 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# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: True is not falsethe result of
bool((1, 2, 3, 'n'))is TrueI 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# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: True is not falsethe result of
bool([1, 2, 3, 'n'])is TrueI 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# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: True is notthe result of
bool({1, 2, 3, 'n'})is TrueI 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# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: True is not falsethe result of
bool({'key': 'value'})is TrueI 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# NOTESthe 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
it raises AssertionError if
somethingis False, becauseif bool(something) == Falseis the same asif not bool(something) == Trueis the same asif not bool(something)is the same asif not somethingit returns None if something is True because all functions return None by default, as if they have an invisible line that says return None
I do not have to give a value for the
messageparameter when I call assertTrue becauseself.assertTrue(something)is the same as
self.assertTrue(something, None)is the same as
self.assertTrue( something=something, message=None, )A function uses the default value for a parameter when it is called without the parameter
close the project
review
bool(anything)returns True or FalseassertFalse(anything) == assertFalse(bool(anything))assertFalse(anything)checks if the result ofbool(anything)is True and raises AssertionError if it isassertTrue(anything) == assertTrue(bool(anything))assertTrue(anything)checks if the result ofbool(anything)is False and it raises AssertionError if it is
code from the chapter
Do you want to see all the CODE I typed for the Truth Table?
what is next?
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