what are booleans?


Imagine we divide the universe in two and the options are things that are True and things that are False. These are the booleans: True and False.

I used assertIs and assertIsNot in test_assertion_error_w_false and test_assertion_error_w_true in the assertion_error project, where I saw that

I want to test what groups Python places the objects seen so far (None, integers, floats, strings, tuples, lists, sets and dictionaries). Which do you think will be False or True?

preview

I have these tests by 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, int))
  8        self.assertNotIsInstance(
  9            False,
 10            (
 11                float, tuple, str,
 12                list, set, dict
 13            )
 14        )
 15        self.assertIs(False, False)
 16        self.assertIs(bool(False), False)
 17        self.assertFalse(bool(False))
 18        self.assertFalse(False)
 19
 20    def test_what_is_true(self):
 21        self.assertIsInstance(True, (bool, int))
 22        self.assertNotIsInstance(
 23            True,
 24            (
 25                float, tuple, str,
 26                list, set, dict
 27            )
 28        )
 29        self.assertIs(True, True)
 30        self.assertIs(bool(True), True)
 31        self.assertTrue(bool(True))
 32        self.assertTrue(True)
 33
 34    def test_is_none_falsy_or_truthy(self):
 35        self.assertFalse(bool(None))
 36        self.assertFalse(None)
 37        self.assertIsNot(None, False)
 38
 39    def test_is_an_integer_falsy_or_truthy(self):
 40        a_negative_integer = -1
 41        self.assertTrue(bool(a_negative_integer))
 42        self.assertTrue(a_negative_integer)
 43        self.assertIsNot(a_negative_integer, True)
 44
 45        self.assertFalse(bool(0))
 46        self.assertFalse(0)
 47        self.assertIsNot(0, False)
 48
 49        a_positive_integer = 1
 50        self.assertTrue(bool(a_positive_integer))
 51        self.assertTrue(a_positive_integer)
 52        self.assertIsNot(a_positive_integer, True)
 53
 54    def test_is_a_float_falsy_or_truthy(self):
 55        a_negative_float = -0.1
 56        self.assertTrue(bool(a_negative_float))
 57        self.assertTrue(a_negative_float)
 58        self.assertIsNot(a_negative_float, True)
 59
 60        self.assertFalse(bool(0.0))
 61        self.assertFalse(0.0)
 62        self.assertIsNot(0.0, False)
 63
 64        a_positive_float = 0.1
 65        self.assertTrue(bool(a_positive_float))
 66        self.assertTrue(a_positive_float)
 67        self.assertIsNot(a_positive_float, True)
 68
 69    def test_is_a_string_falsy_or_truthy(self):
 70        self.assertFalse(bool(str()))
 71        self.assertFalse(str())
 72        self.assertIsNot(str(), False)
 73
 74        a_string = "string with things"
 75        self.assertTrue(bool(a_string))
 76        self.assertTrue(a_string)
 77        self.assertIsNot(a_string, True)
 78
 79    def test_is_a_tuple_falsy_or_truthy(self):
 80        self.assertFalse(bool(tuple()))
 81        self.assertFalse(tuple())
 82        self.assertIsNot(tuple(), False)
 83
 84        a_tuple = (0, 1, 2, 'n')
 85        self.assertTrue(bool(a_tuple))
 86        self.assertTrue(a_tuple)
 87        self.assertIsNot(a_tuple, True)
 88
 89    def test_is_a_list_falsy_or_truthy(self):
 90        self.assertFalse(bool(list()))
 91        self.assertFalse(list())
 92        self.assertIsNot(list(), False)
 93
 94        a_list = [0, 1, 2, 'n']
 95        self.assertTrue(bool(a_list))
 96        self.assertTrue(a_list)
 97        self.assertIsNot(a_list, True)
 98
 99    def test_is_a_set_falsy_or_truthy(self):
100        self.assertFalse(bool(set()))
101        self.assertFalse(set())
102        self.assertIsNot(set(), False)
103
104        a_set = {0, 1, 2, 'n'}
105        self.assertTrue(bool(a_set))
106        self.assertTrue(a_set)
107        self.assertIsNot(a_set, True)
108
109    def test_is_a_dictionary_falsy_or_truthy(self):
110        self.assertFalse(bool(dict()))
111        self.assertFalse(dict())
112        self.assertIsNot(dict(), False)
113
114        a_dictionary = {'key': 'value'}
115        self.assertTrue(bool(a_dictionary))
116        self.assertTrue(a_dictionary)
117        self.assertIsNot(a_dictionary, True)
118
119    def test_the_value_of_false(self):
120        self.assertEqual(False+1, 1)
121        self.assertEqual(False-1, -1)
122        self.assertEqual(False*1, 0)
123
124        # raises ZeroDivisionError
125        # 1 / False
126
127    def test_the_value_of_true(self):
128        self.assertEqual(True+1, 2)
129        self.assertEqual(True-1, 0)
130        self.assertEqual(True*1, 1)
131        self.assertEqual(True/1, 1)
132
133
134# NOTES
135# bool(a dictionary with things) is True
136# bool(a set with things) is True
137# bool(a list with things) is True
138# bool(a tuple with things) is True
139# bool(a string with things) is True
140# bool(a positive number) is True
141# bool(a negative number) is True
142# True is True
143# True is an integer
144# the value of True is 1
145# True is a boolean
146# True is NOT False
147# bool(the empty dictionary) is False
148# bool(the empty set) is False
149# bool(the empty list) is False
150# bool(the empty tuple) is False
151# bool(the empty string) is False
152# bool(zero) is False
153# bool(None) is False
154# False is False
155# False is an integer
156# the value of False is 0
157# False is a boolean
158# False is NOT True
159
160
161# Exceptions seen
162# AssertionError
163# ZeroDivisionError

questions about Booleans

Questions to think about as we divide Python’s basic objects into False and True


start the project

  • I name this project booleans

  • I open a terminal

  • I use uv to make a directory for the project and initialize it

    uv init booleans
    
  • I change directory to the project

    cd booleans
    
  • I make a directory for the tests

    mkdir tests
    
  • I make the tests directory a Python package

    Danger

    use 2 underscores (__) before and after init for __init__.py not _init_.py

    touch tests/__init__.py
    
    New-Item tests/__init__.py
    
  • I make an empty file for the tests in the tests folder

    touch tests/test_booleans.py
    
    New-Item tests/test_booleans.py
    
  • I open test_booleans.py

  • I delete all the text then add the first failing test to test_booleans.py

    1import unittest
    2
    3
    4class TestBooleans(unittest.TestCase):
    5
    6    def test_failure(self):
    7        self.assertEqual(False, True)
    
  • I go back to the terminal to make a requirements file for the Python packages I need

    echo "pytest" > requirements.txt
    
  • I add pytest-watcher to the requirements file

    echo "pytest-watcher" >> requirements.txt
    
  • I use uv to install pytest-watcher with the requirements file

    uv add --requirement requirements.txt
    
  • I add the new files and folder to git for tracking

    git add .
    
  • I add a git commit message

    git commit --all --message \
    'setup project'
    
  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal is my friend, and shows AssertionError

    ======================== FAILURES ========================
    _______________ TestBooleans.test_failure ________________
    
    self = <tests.test_booleans.TestBooleans testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertEqual(False, True)
    E       AssertionError: False != True
    
    tests/test_booleans.py:7: AssertionError
    ================ short test summary info =================
    FAILED tests/test_booleans.py::TestBooleans::test_failure - AssertionError: False != True
    =================== 1 failed in X.YZs ====================
    

    if the terminal does not show the same error, then check if

    • your tests/__init__.py has two underscores (__) before and after init for __init__.py not _init_.py

    • you ran echo "pytest-watcher" >> requirements.txt, to add pytest-watcher to the requirements file

    and try uv run pytest-watcher . --now again

  • I add AssertionError to the list of Exceptions seen, in test_booleans.py

     4class TestBooleans(unittest.TestCase):
     5
     6    def test_failure(self):
     7        self.assertEqual(False, True)
     8
     9
    10# Exceptions seen
    11# AssertionError
    
  • I change True to False in the assertion

     6    def test_failure(self):
     7        # self.assertEqual(False, True)
     8        self.assertEqual(False, False)
     9
    10
    11# Exceptions seen
    

    the test passes.


test_what_is_false

RED: make it fail


I change test_failure to test_what_is_false with assertNotIsInstance

 4class TestBooleans(unittest.TestCase):
 5
 6    def test_what_is_false(self):
 7        self.assertNotIsInstance(False, bool)
 8
 9
10# Exceptions seen
11# AssertionError

the terminal is my friend, and shows AssertionError

AssertionError: False
    is an instance of <class 'bool'>

this was also in test_is_none_a_boolean.


GREEN: make it pass


I change assertNotIsInstance to the assertIsInstance method

 6    def test_what_is_false(self):
 7        # self.assertNotIsInstance(False, bool)
 8        self.assertIsInstance(False, bool)
 9
10
11# Exceptions seen

the test passes.


REFACTOR: make it better


  • I add a comment

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9
    10
    11# NOTES
    12# False is a boolean
    13
    14
    15# Exceptions seen
    16# AssertionError
    

    also from test_is_none_a_boolean.

  • test_when_year_of_birth_is_not_an_integer showed me that False is an integer. I add an assertion for it

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9        self.assertNotIsInstance(False, int)
    10
    11
    12# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False
        is an instance of <class 'int'>
    

    in Python, False is a boolean and an integer.

  • I add a comment

    12# NOTES
    13# False is an integer
    14# False is a boolean
    15
    16
    17# Exceptions seen
    18# AssertionError
    
  • I change assertNotIsInstance to assertIsInstance

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9        # self.assertNotIsInstance(False, int)
    10        self.assertIsInstance(False, int)
    11
    12
    13# NOTES
    

    the test passes.


how to test if something is an instance of more than one object

The instance methods can take a tuple of classes.

  • I put bool and int together

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9        # self.assertNotIsInstance(False, int)
    10        self.assertIsInstance(False, int)
    11        self.assertNotIsInstance(False, (bool, int))
    12
    13
    14# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False
        is an instance of <class 'bool'>
    
  • I change assertNotIsInstance to assertIsInstance for (False, (bool, int))

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        self.assertIsInstance(False, bool)
     9        # self.assertNotIsInstance(False, int)
    10        self.assertIsInstance(False, int)
    11        # self.assertNotIsInstance(False, (bool, int))
    12        self.assertIsInstance(False, (bool, int))
    13
    14
    15# NOTES
    

    the test passes.

  • I comment out the first assertions since they are covered by the new one

     6    def test_what_is_false(self):
     7        # self.assertNotIsInstance(False, bool)
     8        # self.assertIsInstance(False, bool)
     9        # self.assertNotIsInstance(False, int)
    10        # self.assertIsInstance(False, int)
    11        # self.assertNotIsInstance(False, (bool, int))
    12        self.assertIsInstance(False, (bool, int))
    13
    14
    15# NOTES
    
  • I add an assertion to test if False is a float

    11        # self.assertNotIsInstance(False, (bool, int))
    12        self.assertIsInstance(False, (bool, int))
    13        self.assertIsInstance(False, float)
    14
    15
    16# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False
        is not an instance of <class 'float'>
    
  • I change assertIsInstance to assertNotIsInstance for (False, float)

    11        # self.assertNotIsInstance(False, (bool, int))
    12        self.assertIsInstance(False, (bool, int))
    13        # self.assertIsInstance(False, float)
    14        self.assertNotIsInstance(False, float)
    15
    16
    17# NOTES
    

    the test passes.

  • I add an assertion to test if False is a string

    13        # self.assertIsInstance(False, float)
    14        self.assertNotIsInstance(False, float)
    15        self.assertIsInstance(False, str)
    16
    17
    18# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False
        is not an instance of <class 'str'>
    
  • I change assertIsInstance to assertNotIsInstance for (False, str)

    13        # self.assertIsInstance(False, float)
    14        self.assertNotIsInstance(False, float)
    15        # self.assertIsInstance(False, str)
    16        self.assertNotIsInstance(False, str)
    17
    18
    19# NOTES
    

    the test passes.

  • I add an assertion to test if False is a tuple

    15        # self.assertIsInstance(False, str)
    16        self.assertNotIsInstance(False, str)
    17        self.assertIsInstance(False, tuple)
    18
    19
    20# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False
        is not an instance of <class 'tuple'>
    
  • I change assertIsInstance to assertNotIsInstance for (False, tuple)

    15        # self.assertIsInstance(False, str)
    16        self.assertNotIsInstance(False, str)
    17        # self.assertIsInstance(False, tuple)
    18        self.assertNotIsInstance(False, tuple)
    19
    20
    21# NOTES
    

    the test passes.

  • I add an assertion to test if False is a list

    17        # self.assertIsInstance(False, tuple)
    18        self.assertNotIsInstance(False, tuple)
    19        self.assertIsInstance(False, list)
    20
    21
    22# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False
        is not an instance of <class 'list'>
    
  • I change assertIsInstance to assertNotIsInstance for (False, list)

    17        # self.assertIsInstance(False, tuple)
    18        self.assertNotIsInstance(False, tuple)
    19        # self.assertIsInstance(False, list)
    20        self.assertNotIsInstance(False, list)
    21
    22
    23# NOTES
    

    the test passes.

  • I add an assertion to test if False is a set

    19        # self.assertIsInstance(False, list)
    20        self.assertNotIsInstance(False, list)
    21        self.assertIsInstance(False, set)
    22
    23
    24# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False
        is not an instance of <class 'set'>
    
  • I change assertIsInstance to assertNotIsInstance for (False, set)

    19        # self.assertIsInstance(False, list)
    20        self.assertNotIsInstance(False, list)
    21        # self.assertIsInstance(False, set)
    22        self.assertNotIsInstance(False, set)
    23
    24
    25# NOTES
    

    the test passes.

  • I add an assertion to test if False is a dictionary

    21        # self.assertIsInstance(False, set)
    22        self.assertNotIsInstance(False, set)
    23        self.assertIsInstance(False, dict)
    24
    25
    26# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False
        is not an instance of <class 'dict'>
    
  • I change assertIsInstance to assertNotIsInstance for (False, dict)

    21        # self.assertIsInstance(False, set)
    22        self.assertNotIsInstance(False, set)
    23        # self.assertIsInstance(False, dict)
    24        self.assertNotIsInstance(False, dict)
    25
    26
    27# NOTES
    

    the test passes.

  • I use a tuple with float, str, tuple, list, set and dict with a call to assertIsInstance

    24        self.assertNotIsInstance(False, dict)
    25        self.assertIsInstance(
    26            False,
    27            (
    28                float, tuple, str,
    29                list, set, dict
    30            )
    31        )
    32
    33
    34# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False
        is not an instance of any of (
            <class 'float'>, <class 'tuple'>,
            <class 'str'>, <class 'list'>,
            <class 'set'>, <class 'dict'>
        )
    
  • I change assertIsInstance to assertNotIsInstance

    24        self.assertNotIsInstance(False, dict)
    25        # self.assertIsInstance(
    26        self.assertNotIsInstance(
    27            False,
    28            (
    29                float, tuple, str,
    30                list, set, dict
    31            )
    32        )
    33
    34
    35# NOTES
    

    the test passes.

  • I use the assertIsNot method like I did in test_assertion_error_w_false

    26        self.assertNotIsInstance(
    27            False,
    28            (
    29                float, tuple, str,
    30                list, set, dict
    31            )
    32        )
    33        self.assertIsNot(False, False)
    34
    35
    36# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: False
    

    because False is False.

  • I change assertIsNot to assertIs for (False, False)

    26        self.assertNotIsInstance(
    27            False,
    28            (
    29                float, tuple, str,
    30                list, set, dict
    31            )
    32        )
    33        # self.assertIsNot(False, False)
    34        self.assertIs(False, False)
    35
    36
    37# NOTES
    

    the test passes. I know this from test_assertion_error_w_false.


how to test if something is grouped as False

I can test if Python groups an object as False with the bool built-in function from The Python Standard Library. It returns False or True for the object given in parentheses.


RED: make it fail


  • I add an assertion with a call to bool

    33        # self.assertIsNot(False, False)
    34        self.assertIs(False, False)
    35        self.assertIsNot(bool(False), False)
    36
    37
    38# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: False
    

    because this happens when assertIsNot(bool(False), False) runs

    assertIsNot(bool(False), False)
    assertIsNot(False      , False)
    
  • I add a comment

    38# NOTES
    39# False is False
    40# False is an integer
    41# False is a boolean
    42
    43
    44# Exceptions seen
    45# AssertionError
    

GREEN: make it pass


I change the assertIsNot to assertIs to make the statement True

33        # self.assertIsNot(False, False)
34        self.assertIs(False, False)
35        # self.assertIsNot(bool(False), False)
36        self.assertIs(bool(False), False)
37
38
39# NOTES

the test passes because bool(False) is equal to False.


another way to test if something is grouped as False


The unittest.TestCase class has a method I can use to test if the result of calling the bool built-in function with an object is False - assertFalse.

assertFalse(something)

which is like

assertIs(bool(something), False)

it raises AssertionError if the object in parentheses is grouped as True.


RED: make it fail


  • I add assertFalse to the test

    35        # self.assertIsNot(bool(False), False)
    36        self.assertIs(bool(False), False)
    37        self.assertFalse(bool(True))
    38
    39
    40# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because this happens when assertFalse(bool(True)) runs

    assertFalse(bool(True))
    assertFalse(True)
    

    which raises AssertionError since the result of bool(True) is True not False.

  • I add a comment

    40# NOTES
    41# True is NOT False
    42# False is False
    43# False is an integer
    44# False is a boolean
    

GREEN: make it pass


I change True to False in the parentheses

35        # self.assertIsNot(bool(False), False)
36        self.assertIs(bool(False), False)
37        # self.assertFalse(bool(True))
38        self.assertFalse(bool(False))
39
40
41# NOTES

the test passes because bool(False) is False.


REFACTOR: make it better


  • I can skip bool and get the same result. I add another call to the assertFalse method

    37        # self.assertFalse(bool(True))
    38        self.assertFalse(bool(False))
    39        self.assertFalse(True)
    40
    41
    42# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because bool(True) is True.

  • I change True to False in the parentheses

    37        # self.assertFalse(bool(True))
    38        self.assertFalse(bool(False))
    39        # self.assertFalse(True)
    40        self.assertFalse(False)
    41
    42
    43# NOTES
    

    the test passes because bool(False) is False.

    The assertFalse method raises AssertionError if the result of a call to the bool built-in function with an object is True.

  • I remove the commented lines and single instance tests for float, str, tuple, list, set and dict

     4class TestBooleans(unittest.TestCase):
     5
     6    def test_what_is_false(self):
     7        self.assertIsInstance(False, (bool, int))
     8        self.assertNotIsInstance(
     9            False,
    10            (
    11                float, tuple, str,
    12                list, set, dict
    13            )
    14        )
    15        self.assertIs(False, False)
    16        self.assertIs(bool(False), False)
    17        self.assertFalse(bool(False))
    18        self.assertFalse(False)
    19
    20
    21# NOTES
    
  • I open a new terminal then make sure I am in the booleans folder

    cd booleans
    
  • I add a git commit message in the new terminal

    git commit --all --message \
    'add test_what_is_false'
    

False is a boolean.


test_what_is_true

RED: make it fail


I add test_what_is_true with assertNotIsInstance

18    self.assertFalse(False)
19
20    def test_what_is_true(self):
21        self.assertNotIsInstance(True, (bool, int))
22
23
24# NOTES

the terminal is my friend, and shows AssertionError

AssertionError: True
    is an instance of <class 'bool'>

this was also in test_is_none_a_boolean, without the tuple of classes.


GREEN: make it pass


I change assertNotIsInstance to the assertIsInstance method

18          self.assertFalse(False)
19
20      def test_what_is_true(self):
21          # self.assertNotIsInstance(True, (bool, int))
22          self.assertIsInstance(True, (bool, int))
23
24
25  # NOTES

the test passes.


REFACTOR: make it better


  • I add comments

    25# NOTES
    26# True is an integer
    27# True is a boolean
    28# True is NOT False
    29# False is False
    30# False is an integer
    31# False is a boolean
    32
    33
    34# Exceptions seen
    35# AssertionError
    

    in Python, True is a boolean and an integer.

  • I add a call to assertIsInstance with a tuple that has float, str, tuple, list, set and dict

    22        self.assertIsInstance(True, (bool, int))
    23        self.assertIsInstance(
    24            True,
    25            (
    26                float, tuple, str,
    27                list, set, dict
    28            )
    29        )
    30
    31
    32# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True
        is not an instance of any of (
            <class 'float'>, <class 'tuple'>,
            <class 'str'>, <class 'list'>,
            <class 'set'>, <class 'dict'>
        )
    
  • I change assertIsInstance to the assertNotIsInstance method

    22        self.assertIsInstance(True, (bool, int))
    23        # self.assertIsInstance(
    24        self.assertNotIsInstance(
    25            True,
    26            (
    27                float, tuple, str,
    28                list, set, dict
    29            )
    30        )
    31
    32
    33# NOTES
    

    the test passes.

  • I use the assertIsNot method like I did in test_assertion_error_w_true

    24        self.assertNotIsInstance(
    25            True,
    26            (
    27                float, tuple, str,
    28                list, set, dict
    29            )
    30        )
    31        self.assertIsNot(True, True)
    32
    33
    34# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: True
    

    because True is True.

  • I change assertIsNot to assertIs for (True, True)

    24        self.assertNotIsInstance(
    25            True,
    26            (
    27                float, tuple, str,
    28                list, set, dict
    29            )
    30        )
    31        # self.assertIsNot(True, True)
    32        self.assertIs(True, True)
    33
    34
    35# NOTES
    

    the test passes. I know this from test_assertion_error_w_true.


how to test if something is grouped as True

I can test if Python groups an object as True with the bool built-in function since it returns False or True for the object given in parentheses.


RED: make it fail


  • I add an assertion with a call to bool

    31        # self.assertIsNot(True, True)
    32        self.assertIs(True, True)
    33        self.assertIsNot(bool(True), True)
    34
    35
    36# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: True
    

    because this happens when assertIsNot(bool(True), True) runs

    assertIsNot(bool(True), True)
    assertIsNot(True      , True)
    
  • I add a comment

    36# NOTES
    37# True is True
    38# True is an integer
    39# True is a boolean
    40# True is NOT False
    41# False is False
    42# False is an integer
    43# False is a boolean
    

GREEN: make it pass


I change assertIsNot to assertIs

31        # self.assertIsNot(True, True)
32        self.assertIs(True, True)
33        # self.assertIsNot(bool(True), True)
34        self.assertIs(bool(True), True)
35
36
37# NOTES

the test passes because bool(True) is True.


another way to test if something is grouped as True


The unittest.TestCase class has a method I can use to test if the result of calling the bool built-in function with an object is True - assertTrue.

assertTrue(something)

which is like

assertIs(bool(something), True)

it raises AssertionError if the object in parentheses is grouped as False.


RED: make it fail


  • I add assertTrue to the test

    33        # self.assertIsNot(bool(True), True)
    34        self.assertIs(bool(True), True)
    35        self.assertTrue(bool(False))
    36
    37
    38# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because this happens when assertTrue(bool(False)) runs

    assertTrue(bool(False))
    assertTrue(False)
    

    which raises AssertionError since the result of bool(False) is False not True.

  • I add a comment

    39# NOTES
    40# True is True
    41# True is an integer
    42# True is a boolean
    43# True is NOT False
    44# False is False
    45# False is an integer
    46# False is a boolean
    47# False is NOT True
    48
    49
    50# Exceptions seen
    51# AssertionError
    

GREEN: make it pass


I change False to True in the parentheses

33        # self.assertIsNot(bool(True), True)
34        self.assertIs(bool(True), True)
35        # self.assertTrue(bool(False))
36        self.assertTrue(bool(True))
37
38
39# NOTES

the test passes because bool(True) is True.


REFACTOR: make it better


  • I add another call to the assertTrue method without bool to show that I can skip it

    35        # self.assertTrue(bool(False))
    36        self.assertTrue(bool(True))
    37        self.assertTrue(False)
    38
    39
    40# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because bool(False) is False.

  • I change False to True in the parentheses

    35        # self.assertTrue(bool(False))
    36        self.assertTrue(bool(True))
    37        # self.assertTrue(False)
    38        self.assertTrue(True)
    39
    40
    41# NOTES
    

    the test passes because bool(True) is True.

    The assertTrue method raises AssertionError if the result of a call to the bool built-in function with an object is False.

  • I remove the commented lines from test_what_is_true

    20    def test_what_is_true(self):
    21        self.assertIsInstance(True, (bool, int))
    22        self.assertNotIsInstance(
    23            True,
    24            (
    25                float, tuple, str,
    26                list, set, dict
    27            )
    28        )
    29        self.assertIs(True, True)
    30        self.assertIs(bool(True), True)
    31        self.assertTrue(bool(True))
    32        self.assertTrue(True)
    33
    34
    35# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_what_is_true'
    

True is a boolean.


test_is_none_falsy_or_truthy

Is None grouped as False or True?

RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test to see if None is grouped as False or True

    32    self.assertTrue(True)
    33
    34    def test_is_none_falsy_or_truthy(self):
    35        self.assertTrue(bool(None))
    36
    37
    38# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(None) is False.


GREEN: make it pass


I change assertTrue to assertFalse for bool(None)

34    def test_is_none_falsy_or_truthy(self):
35        # self.assertTrue(bool(None))
36        self.assertFalse(bool(None))
37
38
39# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    39# NOTES
    40# True is True
    41# True is an integer
    42# True is a boolean
    43# True is NOT False
    44# bool(None) is False
    45# False is False
    46# False is an integer
    47# False is a boolean
    48# False is NOT True
    
  • I add an assertion for None without bool

    34    def test_is_none_falsy_or_truthy(self):
    35        # self.assertTrue(bool(None))
    36        self.assertFalse(bool(None))
    37        self.assertTrue(None)
    38
    39
    40# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    

    A reminder of test_assertion_error_w_true.

  • I change assertTrue to assertFalse for None

    34    def test_is_none_falsy_or_truthy(self):
    35        # self.assertTrue(bool(None))
    36        self.assertFalse(bool(None))
    37        # self.assertTrue(None)
    38        self.assertFalse(None)
    39
    40
    41# NOTES
    

    the test passes because the result of bool(None) is False.

  • I add an assertion for if None is the same object as False

    37        # self.assertTrue(None)
    38        self.assertFalse(None)
    39        self.assertIs(None, False)
    40
    41
    42# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not False
    
  • I change assertIs to assertIsNot for (None, False)

    37        # self.assertTrue(None)
    38        self.assertFalse(None)
    39        # self.assertIs(None, False)
    40        self.assertIsNot(None, False)
    41
    42
    43# NOTES
    

    the test passes because None is not the same object as False.

  • I remove the commented lines from test_is_none_falsy_or_truthy

    34    def test_is_none_falsy_or_truthy(self):
    35        self.assertFalse(bool(None))
    36        self.assertFalse(None)
    37        self.assertIsNot(None, False)
    38
    39
    40# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_none_falsy_or_truthy'
    
  • False is not None.

  • None is not False

  • None is grouped as False.


test_is_an_integer_falsy_or_truthy

Is an integer grouped as False or True?

RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for if an integer (a whole number without decimals) is grouped as False or True

    39    self.assertIsNot(None, False)
    40
    41    def test_is_an_integer_falsy_or_truthy(self):
    42        self.assertFalse(bool(-1))
    43
    44
    45# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    
    • because the result of bool(-1) is True.

    • I use -1 for all the integers (whole numbers without decimals) that are smaller than 0.


GREEN: make it pass


I change assertFalse to assertTrue for bool(-1)

39    def test_is_an_integer_falsy_or_truthy(self):
40        # self.assertFalse(bool(-1))
41        self.assertTrue(bool(-1))
42
43
44# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    44# NOTES
    45# bool(-1) is True
    46# True is True
    47# True is an integer
    48# True is a boolean
    49# True is NOT False
    50# bool(None) is False
    
  • I add an assertion for -1 without bool

    39    def test_is_an_integer_falsy_or_truthy(self):
    40        # self.assertFalse(bool(-1))
    41        self.assertTrue(bool(-1))
    42        self.assertFalse(-1)
    43
    44
    45# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: -1 is not false
    

    because the result of bool(-1) is True.

  • I change assertFalse to assertTrue for -1

    39    def test_is_an_integer_falsy_or_truthy(self):
    40        # self.assertFalse(bool(-1))
    41        self.assertTrue(bool(-1))
    42        # self.assertFalse(-1)
    43        self.assertTrue(-1)
    44
    45
    46# NOTES
    
    • The test passes because the result of bool(-1) is True.

    • A negative integer is grouped as True.

  • I add an assertion for if a negative integer is the same object as True

    42        # self.assertFalse(-1)
    43        self.assertTrue(-1)
    44        self.assertIs(-1, True)
    45
    46
    47# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: -1 is not True
    
  • I change assertIs to assertIsNot for (-1, True)

    42        # self.assertFalse(-1)
    43        self.assertTrue(-1)
    44        # self.assertIs(-1, True)
    45        self.assertIsNot(-1, True)
    46
    47
    48# NOTES
    

    the test passes because An integer is not the same object as True.

  • I add a variable for -1

    39    def test_is_an_integer_falsy_or_truthy(self):
    40        a_negative_integer = -1
    41        # self.assertFalse(bool(-1))
    42        self.assertTrue(bool(-1))
    
  • I use the variable to remove repetition of -1

    39    def test_is_an_integer_falsy_or_truthy(self):
    40        a_negative_integer = -1
    41        # self.assertFalse(bool(-1))
    42        # self.assertTrue(bool(-1))
    43        self.assertTrue(bool(a_negative_integer))
    44        # self.assertFalse(-1)
    45        # self.assertTrue(-1)
    46        self.assertTrue(a_negative_integer)
    47        # self.assertIs(-1, True)
    48        # self.assertIsNot(-1, True)
    49        self.assertIsNot(a_negative_integer, True)
    50
    51
    52# NOTES
    

    the test is still green.

  • I add an assertion to test if 0 is grouped as False or True

    49        self.assertIsNot(a_negative_integer, True)
    50
    51        self.assertTrue(bool(0))
    52
    53
    54# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(0) is False.

  • I change assertTrue to assertFalse for bool(0)

    49        self.assertTrue(a_negative_integer)
    50
    51        # self.assertTrue(bool(0))
    52        self.assertFalse(bool(0))
    53
    54
    55# NOTES
    

    the test passes.

  • I add a comment

    55# NOTES
    56# bool(-1) is True
    57# True is True
    58# True is an integer
    59# True is a boolean
    60# True is NOT False
    61# bool(0) is False
    62# bool(None) is False
    63# False is False
    64# False is an integer
    65# False is a boolean
    66# False is NOT True
    
  • I add an assertion for 0 without bool

    51        # self.assertTrue(bool(0))
    52        self.assertFalse(bool(0))
    53        self.assertTrue(0)
    54
    55
    56# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0 is not true
    

    because the result of bool(0) is False.

  • I change assertTrue to assertFalse for 0

    51        # self.assertTrue(bool(0))
    52        self.assertFalse(bool(0))
    53        # self.assertTrue(0)
    54        self.assertFalse(0)
    55
    56
    57# NOTES
    

    the test passes because the result of bool(0) is False.

  • I add an assertion for if 0 is the same object as False

    53        # self.assertTrue(0)
    54        self.assertFalse(0)
    55        self.assertIs(0, False)
    56
    57
    58# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0 is not False
    
  • I change assertIs to assertIsNot for (0, False)

    53        # self.assertTrue(0)
    54        self.assertFalse(0)
    55        # self.assertIs(0, False)
    56        self.assertIsNot(0, False)
    57
    58
    59# NOTES
    

    the test passes because an integer is not the same object as False.

  • I add an assertion to test if 1 is grouped as False or True

    56        self.assertIsNot(0, False)
    57
    58        self.assertFalse(bool(1))
    59
    60
    61# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    
    • because the result of bool(1) is True.

    • I use 1 for all the integers (whole numbers without decimals) that are bigger than 0.

  • I change assertFalse to assertTrue for bool(1)

    56        self.assertIsNot(0, False)
    57
    58        # self.assertFalse(bool(1))
    59        self.assertTrue(bool(1))
    60
    61
    62# NOTES
    

    the test passes.

  • I add a comment

    62# NOTES
    63# bool(1) is True
    64# bool(-1) is True
    65# True is True
    66# True is an integer
    67# True is a boolean
    68# True is NOT False
    69# bool(0) is False
    
  • I add an assertion for 1 without bool

    58        # self.assertFalse(bool(1))
    59        self.assertTrue(bool(1))
    60        self.assertFalse(1)
    61
    62
    63# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 1 is not false
    

    because the result of bool(1) is True.

  • I change assertFalse to assertTrue for 1

    58        # self.assertFalse(bool(1))
    59        self.assertTrue(bool(1))
    60        # self.assertFalse(1)
    61        self.assertTrue(1)
    62
    63
    64# NOTES
    
    • The test passes because the result of bool(1) is True.

    • A positive integer is grouped as True.

  • I add an assertion for if a positive integer is the same object as True

    60        # self.assertFalse(1)
    61        self.assertTrue(1)
    62        self.assertIs(1, True)
    63
    64
    65# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 1 is not True
    
  • I change assertIs to assertIsNot for (1, True)

    60        # self.assertFalse(1)
    61        self.assertTrue(1)
    62        # self.assertIs(1, True)
    63        self.assertIsNot(1, True)
    64
    65
    66# NOTES
    

    the test passes because An integer is not the same object as True.

  • I add a variable for 1

    56        self.assertIsNot(0, False)
    57
    58        a_positive_integer = 1
    59        # self.assertFalse(bool(1))
    
  • I use the variable to remove repetition of 1

    58        a_positive_integer = 1
    59        # self.assertFalse(bool(1))
    60        # self.assertTrue(bool(1))
    61        self.assertTrue(bool(a_positive_integer))
    62        # self.assertFalse(1)
    63        # self.assertTrue(1)
    64        self.assertTrue(a_positive_integer)
    65        # self.assertIs(1, True)
    66        # self.assertIsNot(1, True)
    67        self.assertIsNot(a_positive_integer, True)
    68
    69
    70# NOTES
    

    the test is still green.

  • I remove the commented lines from test_is_an_integer_falsy_or_truthy

    38    def test_is_an_integer_falsy_or_truthy(self):
    39        a_negative_integer = -1
    40        self.assertTrue(bool(a_negative_integer))
    41        self.assertTrue(a_negative_integer)
    42        self.assertIsNot(a_negative_integer, True)
    43
    44        self.assertFalse(bool(0))
    45        self.assertFalse(0)
    46        self.assertIsNot(0, False)
    47
    48        a_positive_integer = 1
    49        self.assertTrue(bool(a_positive_integer))
    50        self.assertTrue(a_positive_integer)
    51        self.assertIsNot(a_positive_integer, True)
    52
    53
    54# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_an_integer_falsy_or_truthy'
    

0 is grouped as False. Positive and Negative Integers are grouped as True.


test_is_a_float_falsy_or_truthy

Is a float grouped as False or True?

RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for if a float (binary floating point decimal number) is grouped as False or True

    52        self.assertIsNot(a_positive_integer, True)
    53
    54    def test_is_a_float_falsy_or_truthy(self):
    55        self.assertFalse(bool(-0.1))
    56
    57
    58# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    
    • because the result of bool(-0.1) is True.

    • I use -0.1 for all the floats (binary floating point decimal numbers) that are smaller than 0.0.


GREEN: make it pass


I change assertFalse to assertTrue for bool(-0.1)

54    def test_is_a_float_falsy_or_truthy(self):
55        # self.assertFalse(bool(-0.1))
56        self.assertTrue(bool(-0.1))
57
58
59# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    59# NOTES
    60# bool(-0.1) is True
    61# bool(1) is True
    62# bool(-1) is True
    63# True is True
    64# True is an integer
    65# True is a boolean
    66# True is NOT False
    67# bool(0) is False
    
  • I add an assertion for -0.1 without bool

    54    def test_is_a_float_falsy_or_truthy(self):
    55        # self.assertFalse(bool(-0.1))
    56        self.assertTrue(bool(-0.1))
    57        self.assertFalse(-0.1)
    58
    59
    60# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: -0.1 is not false
    

    because the result of bool(-0.1) is True for -0.1

  • I change assertFalse to assertTrue for -0.1

    54    def test_is_a_float_falsy_or_truthy(self):
    55        # self.assertFalse(bool(-0.1))
    56        self.assertTrue(bool(-0.1))
    57        # self.assertFalse(-0.1)
    58        self.assertTrue(-0.1)
    59
    60
    61# NOTES
    
    • The test passes because the result of bool(-0.1) is True.

    • A negative float is grouped as True.

  • I add an assertion for if a negative float is the same object as True

    57        # self.assertFalse(-0.1)
    58        self.assertTrue(-0.1)
    59        self.assertIs(-0.1, True)
    60
    61
    62# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: -0.1 is not True
    
  • I change assertIs to assertIsNot for (-0.1, True)

    57        # self.assertFalse(-0.1)
    58        self.assertTrue(-0.1)
    59        # self.assertIs(-0.1, True)
    60        self.assertIsNot(-0.1, True)
    61
    62
    63# NOTES
    

    the test passes because a float is not the same object as True.

  • I add a variable for -0.1

    54    def test_is_a_float_falsy_or_truthy(self):
    55        a_negative_float = -0.1
    56        # self.assertFalse(bool(-0.1))
    
  • I use the variable to remove repetition of -0.1

    54    def test_is_a_float_falsy_or_truthy(self):
    55        a_negative_float = -0.1
    56        # self.assertFalse(bool(-0.1))
    57        # self.assertTrue(bool(-0.1))
    58        self.assertTrue(bool(a_negative_float))
    59        # self.assertFalse(-0.1)
    60        # self.assertTrue(-0.1)
    61        self.assertTrue(a_negative_float)
    62        # self.assertIs(-0.1, True)
    63        # self.assertIsNot(-0.1, True)
    64        self.assertIsNot(a_negative_float, True)
    65
    66
    67# NOTES
    

    the test is still green.

  • I add an assertion to test if 0.0 is grouped as False or True

    64        self.assertIsNot(a_negative_float, True)
    65
    66        self.assertTrue(bool(0.0))
    67
    68
    69# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(0.0) is False.

  • I change assertTrue to assertFalse for 0.0

    64        self.assertIsNot(a_negative_float, True)
    65
    66        # self.assertTrue(bool(0.0))
    67        self.assertFalse(bool(0.0))
    68
    69
    70# NOTES
    

    the test passes.

  • I add a comment

    70# NOTES
    71# bool(-0.1) is True
    72# bool(1) is True
    73# bool(-1) is True
    74# True is True
    75# True is an integer
    76# True is a boolean
    77# True is NOT False
    78# bool(0.0) is False
    79# bool(0) is False
    80# bool(None) is False
    81# False is False
    82# False is an integer
    83# False is a boolean
    84# False is NOT True
    
  • I add an assertion for 0.0 without bool

    66        # self.assertTrue(bool(0.0))
    67        self.assertFalse(bool(0.0))
    68        self.assertTrue(0.0)
    69
    70
    71# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0.0 is not true
    

    because the result of bool(0.0) is False.

  • I change assertTrue to assertFalse for 0.0

    66        # self.assertTrue(bool(0.0))
    67        self.assertFalse(bool(0.0))
    68        # self.assertTrue(0.0)
    69        self.assertFalse(0.0)
    70
    71
    72# NOTES
    

    the test passes because the result of bool(0.0) is False.

  • I add an assertion for if 0.0 is the same object as False

    68        # self.assertTrue(0.0)
    69        self.assertFalse(0.0)
    70        self.assertIs(0.0, False)
    71
    72
    73# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0.0 is not False
    
  • I change assertIs to assertIsNot for (0.0, False)

    68        # self.assertTrue(0.0)
    69        self.assertFalse(0.0)
    70        # self.assertIs(0.0, False)
    71        self.assertIsNot(0.0, False)
    72
    73
    74# NOTES
    

    the test passes because a float is not the same object as False.

  • I add an assertion to test if 0.1 is grouped as False or True

    71        self.assertIsNot(0.0, False)
    72
    73        self.assertFalse(bool(0.1))
    74
    75
    76# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    
    • because the result of bool(0.1) is True.

    • I use 0.1 for all the floats (binary floating point decimal numbers) that are bigger than 0.0.

  • I change assertFalse to assertTrue for bool(0.1)

    71        self.assertIsNot(0.0, False)
    72
    73        # self.assertFalse(bool(0.1))
    74        self.assertTrue(bool(0.1))
    75
    76
    77# NOTES
    

    the test passes.

  • I add a comment

    68# NOTES
    69# bool(0.1) is True
    70# bool(-0.1) is True
    71# bool(1) is True
    72# bool(-1) is True
    
  • I add an assertion for 0.1 without bool

    73        # self.assertFalse(bool(0.1))
    74        self.assertTrue(bool(0.1))
    75        self.assertFalse(0.1)
    76
    77
    78# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0.1 is not false
    

    because the result of bool(0.1) is True.

  • I change assertTrue to assertFalse for 0.1

    73        # self.assertFalse(bool(0.1))
    74        self.assertTrue(bool(0.1))
    75        # self.assertFalse(0.1)
    76        self.assertTrue(0.1)
    77
    78
    79# NOTES
    
    • The test passes because the result of bool(0.1) is True.

    • A positive float is grouped as True.

  • I add an assertion for if a positive float is the same object as True

    75        # self.assertFalse(0.1)
    76        self.assertTrue(0.1)
    77        self.assertIs(0.1, True)
    78
    79
    80# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0.1 is not True
    
  • I change assertIs to assertIsNot for (0.1, True)

    75        # self.assertFalse(0.1)
    76        self.assertTrue(0.1)
    77        # self.assertIs(0.1, True)
    78        self.assertIsNot(0.1, True)
    79
    80
    81# NOTES
    

    the test passes because a float is not the same object as True.

  • I add a variable for 0.1

    71        self.assertIsNot(0.0, False)
    72
    73        a_positive_float = 0.1
    74        # self.assertFalse(bool(0.1))
    
  • I use the variable to remove repetition of 0.1

    73        a_positive_float = 0.1
    74        # self.assertFalse(bool(0.1))
    75        # self.assertTrue(bool(0.1))
    76        self.assertTrue(bool(a_positive_float))
    77        # self.assertFalse(0.1)
    78        # self.assertTrue(0.1)
    79        self.assertTrue(a_positive_float)
    80        # self.assertIs(0.1, True)
    81        # self.assertIsNot(0.1, True)
    82        self.assertIsNot(a_positive_float, True)
    83
    84
    85# NOTES
    

    the test is still green.

  • I remove the commented lines from test_is_a_float_falsy_or_truthy

    54    def test_is_a_float_falsy_or_truthy(self):
    55        a_negative_float = -0.1
    56        self.assertTrue(bool(a_negative_float))
    57        self.assertTrue(a_negative_float)
    58        self.assertIsNot(a_negative_float, True)
    59
    60        self.assertFalse(bool(0.0))
    61        self.assertFalse(0.0)
    62        self.assertIsNot(0.0, False)
    63
    64        a_positive_float = 0.1
    65        self.assertTrue(bool(a_positive_float))
    66        self.assertTrue(a_positive_float)
    67        self.assertIsNot(a_positive_float, True)
    68
    69
    70# NOTES
    
  • I make # bool(0.1) is True, # bool(-0.1) is True, # bool(1) is True, # bool(-1) is True, # bool(0.0) is False and # bool(0) is False simpler comments because floats and integers are numbers and 0.0 is equal to 0

    70# NOTES
    71# bool(a positive number) is True
    72# bool(a negative number) is True
    73# True is True
    74# True is an integer
    75# True is a boolean
    76# True is NOT False
    77# bool(zero) is False
    78# bool(None) is False
    79# False is False
    80# False is an integer
    81# False is a boolean
    82# False is NOT True
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_float_falsy_or_truthy'
    

‘0.0’ is grouped as False. Positive and Negative floats are grouped as True.


test_is_a_string_falsy_or_truthy

Is a string grouped as False or True?

RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for if a string (anything in quotes) is grouped as False or True

    67        self.assertIsNot(a_positive_float, True)
    68
    69    def test_is_a_string_falsy_or_truthy(self):
    70        self.assertTrue(bool(str()))
    71
    72
    73# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(str()) is False.


GREEN: make it pass


I change assertTrue to assertFalse for bool(str())

69    def test_is_a_string_falsy_or_truthy(self):
70        # self.assertTrue(bool(str()))
71        self.assertFalse(bool(str()))
72
73
74# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    74# NOTES
    75# bool(a positive number) is True
    76# bool(a negative number) is True
    77# True is True
    78# True is an integer
    79# True is a boolean
    80# True is NOT False
    81# bool(str()) is False
    82# bool(zero) is False
    
  • I add an assertion for str() without bool

    69    def test_is_a_string_falsy_or_truthy(self):
    70        # self.assertTrue(bool(str()))
    71        self.assertFalse(bool(str()))
    72        self.assertTrue(str())
    73
    74
    75# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: '' is not true
    
    • because the result of bool(str()) which is bool('') is False.

    • str() is another way to write the empty string:

  • I change assertTrue to assertFalse for str()

    69    def test_is_a_string_falsy_or_truthy(self):
    70        # self.assertTrue(bool(str()))
    71        self.assertFalse(bool(str()))
    72        # self.assertTrue(str())
    73        self.assertFalse(str())
    74
    75
    76# NOTES
    
    • The test passes because the result of bool(str()) is False.

    • The empty string is grouped as False.

  • I add an assertion for if the empty string is the same object as False

    72        # self.assertTrue(str())
    73        self.assertFalse(str())
    74        self.assertIs(str(), False)
    75
    76
    77# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: '' is not False
    
  • I change assertIs to assertIsNot for (str(), False)

    72        # self.assertTrue(str())
    73        self.assertFalse(str())
    74        # self.assertIs(str(), False)
    75        self.assertIsNot(str(), False)
    76
    77
    78# NOTES
    

    the test passes because a string is not the same object as False

  • I add an assertion to test if a string with things is grouped as False or True

    75        self.assertIsNot(str(), False)
    76
    77        self.assertFalse(bool("string with things"))
    78
    79
    80# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the result of bool("string with things") is True.

  • I change assertFalse to assertTrue for bool("string with things")

    75        self.assertIsNot(str(), False)
    76
    77        # self.assertFalse(bool("string with things"))
    78        self.assertTrue(bool("string with things"))
    79
    80
    81# NOTES
    

    the test passes.

  • I add a comment

    81# NOTES
    82# bool("string with things") is True
    83# bool(a positive number) is True
    84# bool(a negative number) is True
    85# True is True
    
  • I add an assertion for "string with things" without bool

    77        # self.assertFalse(bool("string with things"))
    78        self.assertTrue(bool("string with things"))
    79        self.assertFalse("string with things")
    80
    81
    82# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'string with things' is not false
    

    because the result of bool("string with things") is True.

  • I change assertFalse to assertTrue

    77        # self.assertFalse(bool("string with things"))
    78        self.assertTrue(bool("string with things"))
    79        # self.assertFalse("string with things")
    80        self.assertTrue("string with things")
    81
    82
    83# NOTES
    
    • The test passes because the result of bool("string with things") is True.

    • A string with things is grouped as True.

  • I add an assertion for if a string with things is the same object as True

    79        # self.assertFalse("string with things")
    80        self.assertTrue("string with things")
    81        self.assertIs("string with things", True)
    82
    83
    84# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'string with things' is not True
    
  • I change assertIs to assertIsNot for ("string with things", True)

    79        # self.assertFalse("string with things")
    80        self.assertTrue("string with things")
    81        # self.assertIs("string with things", True)
    82        self.assertIsNot("string with things", True)
    83
    84
    85# NOTES
    

    the test passes because a string is not the same object as True.

  • I add a variable for "string with things"

    75        self.assertIsNot(str(), False)
    76
    77        a_string = "string with things"
    78        # self.assertFalse(bool("string with things"))
    
  • I use the variable to remove repetition of "string with things"

    77        a_string = "string with things"
    78        # self.assertFalse(bool("string with things"))
    79        # self.assertTrue(bool("string with things"))
    80        self.assertTrue(bool(a_string))
    81        # self.assertFalse("string with things")
    82        # self.assertTrue("string with things")
    83        self.assertTrue(a_string)
    84        # self.assertIs("string with things", True)
    85        # self.assertIsNot("string with things", True)
    86        self.assertIsNot(a_string, True)
    87
    88
    89# NOTES
    

    the test is still green.

  • I remove the commented lines from test_is_a_string_falsy_or_truthy

    69    def test_is_a_string_falsy_or_truthy(self):
    70        self.assertFalse(bool(str()))
    71        self.assertFalse(str())
    72        self.assertIsNot(str(), False)
    73
    74        a_string = "string with things"
    75        self.assertTrue(bool(a_string))
    76        self.assertTrue(a_string)
    77        self.assertIsNot(a_string, True)
    78
    79
    80# NOTES
    
  • I change the new comments to make them clearer

    80# NOTES
    81# bool(a string with things) is True
    82# bool(a positive number) is True
    83# bool(a negative number) is True
    84# True is True
    85# True is an integer
    86# True is a boolean
    87# True is NOT False
    88# bool(the empty string) is False
    89# bool(zero) is False
    90# bool(None) is False
    91# False is False
    92# False is an integer
    93# False is a boolean
    94# False is NOT True
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_string_falsy_or_truthy'
    

The empty string is grouped as False. A string with things is grouped as True.


test_is_a_tuple_falsy_or_truthy

Is a tuple grouped as False or True?

RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for if a tuple (anything in parentheses ( ) separated by a comma) is grouped as False or True

    77        self.assertIsNot(a_string, True)
    78
    79    def test_is_a_tuple_falsy_or_truthy(self):
    80        self.assertTrue(bool(tuple()))
    81
    82
    83# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(tuple()) is False.


GREEN: make it pass


I change assertTrue to assertFalse for bool(tuple())

79    def test_is_a_tuple_falsy_or_truthy(self):
80        # self.assertTrue(bool(tuple()))
81        self.assertFalse(bool(tuple()))
82
83
84# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    84# NOTES
    85# bool(a string with things) is True
    86# bool(a positive number) is True
    87# bool(a negative number) is True
    88# True is True
    89# True is an integer
    90# True is a boolean
    91# True is NOT False
    92# bool(the empty tuple) is False
    93# bool(the empty string) is False
    
  • I add an assertion for tuple() without bool

    79    def test_is_a_tuple_falsy_or_truthy(self):
    80        # self.assertTrue(bool(tuple()))
    81        self.assertFalse(bool(tuple()))
    82        self.assertTrue(tuple())
    83
    84
    85# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: () is not true
    
    • because the result of bool(tuple()) which is bool(()) is False.

    • tuple() is another way to write () (the empty tuple).

  • I change assertTrue to assertFalse

    79    def test_is_a_tuple_falsy_or_truthy(self):
    80        # self.assertTrue(bool(tuple()))
    81        self.assertFalse(bool(tuple()))
    82        # self.assertTrue(tuple())
    83        self.assertFalse(tuple())
    84
    85
    86# NOTES
    
    • The test passes because the result of bool(tuple()) is False.

    • The empty tuple is grouped as False.

  • I add an assertion for if the empty tuple is the same object as False

    82        # self.assertTrue(tuple())
    83        self.assertFalse(tuple())
    84        self.assertIs(tuple(), False)
    85
    86
    87# NOTES
    
  • I change assertIsNot to assertIs for (tuple(), False)

    82        # self.assertTrue(tuple())
    83        self.assertFalse(tuple())
    84        # self.assertIs(tuple(), False)
    85        self.assertIsNot(tuple(), False)
    86
    87
    88# NOTES
    

    the test passes because a tuple is not the same object as False.

  • I add an assertion to test if a tuple with things is grouped as False or True

    85        self.assertIsNot(tuple(), False)
    86
    87        self.assertFalse(bool((0, 1, 2, 'n')))
    88
    89
    90# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the result of bool((0, 1, 2, 'n')) is True.

  • I change assertFalse to assertTrue for bool((0, 1, 2, 'n'))

    85        self.assertIsNot(tuple(), False)
    86
    87        # self.assertFalse(bool((0, 1, 2, 'n')))
    88        self.assertTrue(bool((0, 1, 2, 'n')))
    89
    90
    91# NOTES
    

    the test passes.

  • I add a comment

    91# NOTES
    92# bool(a tuple with things) is True
    93# bool(a string with things) is True
    94# bool(a positive number) is True
    95# bool(a negative number) is True
    
  • I add an assertion for (0, 1, 2, 'n') without bool

    87        # self.assertFalse(bool((0, 1, 2, 'n')))
    88        self.assertTrue(bool((0, 1, 2, 'n')))
    89        self.assertFalse((0, 1, 2, 'n'))
    90
    91
    92# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (0, 1, 2, 'n') is not false
    

    because the result of bool((0, 1, 2, 'n')) is True.

  • I change assertFalse to assertTrue for (0, 1, 2, 'n')

    87        # self.assertFalse(bool((0, 1, 2, 'n')))
    88        self.assertTrue(bool((0, 1, 2, 'n')))
    89        # self.assertFalse((0, 1, 2, 'n'))
    90        self.assertTrue((0, 1, 2, 'n'))
    91
    92
    93# NOTES
    
    • The test passes because the result of bool((0, 1, 2, 'n')) is True.

    • A tuple with things is grouped as True.

  • I add a call to assertIs to test if a tuple with things is the same object as True

    89        # self.assertFalse((0, 1, 2, 'n'))
    90        self.assertTrue((0, 1, 2, 'n'))
    91        self.assertIs((0, 1, 2, 'n'), True)
    92
    93
    94# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (0, 1, 2, 'n') is not True
    
  • I change assertIs to assertIsNot for ((0, 1, 2, 'n'), True)

    89        # self.assertFalse((0, 1, 2, 'n'))
    90        self.assertTrue((0, 1, 2, 'n'))
    91        # self.assertIs((0, 1, 2, 'n'), True)
    92        self.assertIsNot((0, 1, 2, 'n'), True)
    93
    94
    95# NOTES
    

    the test passes because a tuple is not the same object as True.

  • I add a variable for (0, 1, 2, 'n')

    85        self.assertIsNot(tuple(), False)
    86
    87        a_tuple = (0, 1, 2, 'n')
    88        # self.assertFalse(bool((0, 1, 2, 'n')))
    
  • I use the variable to remove repetition of (0, 1, 2, 'n')

    87        a_tuple = (0, 1, 2, 'n')
    88        # self.assertFalse(bool((0, 1, 2, 'n')))
    89        # self.assertTrue(bool((0, 1, 2, 'n')))
    90        self.assertTrue(bool(a_tuple))
    91        # self.assertFalse((0, 1, 2, 'n'))
    92        # self.assertTrue((0, 1, 2, 'n'))
    93        self.assertTrue(a_tuple)
    94        # self.assertIs((0, 1, 2, 'n'), True)
    95        # self.assertIsNot((0, 1, 2, 'n'), True)
    96        self.assertIsNot(a_tuple, True)
    97
    98
    99# NOTES
    

    the test is still green.

  • I remove the commented lines from test_is_a_tuple_falsy_or_truthy

    79    def test_is_a_tuple_falsy_or_truthy(self):
    80        self.assertFalse(bool(tuple()))
    81        self.assertFalse(tuple())
    82        self.assertIsNot(tuple(), False)
    83
    84        a_tuple = (0, 1, 2, 'n')
    85        self.assertTrue(bool(a_tuple))
    86        self.assertTrue(a_tuple)
    87        self.assertIsNot(a_tuple, True)
    88
    89
    90# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_tuple_falsy_or_truthy'
    

The empty tuple is grouped as False. A tuple with things is grouped as True.


test_is_a_list_falsy_or_truthy

Is a list grouped as False or True?

RED: make it fail



GREEN: make it pass


I change assertTrue to assertFalse for bool(list())

89    def test_is_a_list_falsy_or_truthy(self):
90        # self.assertTrue(bool(list()))
91        self.assertFalse(bool(list()))
92
93
94# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

     94# NOTES
     95# bool(a tuple with things) is True
     96# bool(a string with things) is True
     97# bool(a positive number) is True
     98# bool(a negative number) is True
     99# True is True
    100# True is an integer
    101# True is a boolean
    102# True is NOT False
    103# bool(the empty list) is False
    104# bool(the empty tuple) is False
    
  • I add an assertion for list() without bool

    89    def test_is_a_list_falsy_or_truthy(self):
    90        # self.assertTrue(bool(list()))
    91        self.assertFalse(bool(list()))
    92        self.assertTrue(list())
    93
    94
    95# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: [] is not true
    
    • because the result of bool(list()) which is bool([]) is False.

    • list() is another way to write [] (the empty list).

  • I change assertTrue to assertFalse for list()

    89    def test_is_a_list_falsy_or_truthy(self):
    90        # self.assertTrue(bool(list()))
    91        self.assertFalse(bool(list()))
    92        # self.assertTrue(list())
    93        self.assertFalse(list())
    94
    95
    96# NOTES
    
  • I add an assertion to test if the empty list is the same object as False

    92        # self.assertTrue(list())
    93        self.assertFalse(list())
    94        self.assertIs(list(), False)
    95
    96
    97# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: [] is not False
    
  • I change assertIs to assertIsNot for (list(), False)

    92        # self.assertTrue(list())
    93        self.assertFalse(list())
    94        # self.assertIs(list(), False)
    95        self.assertIsNot(list(), False)
    96
    97
    98# NOTES
    

    the test passes because a list is not the same object as False.

  • I add an assertion to test if a list (anything in square brackets ‘[ ]’) with things is grouped as False or True

    94        self.assertIsNot(list(), False)
    95
    96        self.assertFalse(bool([0, 1, 2, 'n']))
    97
    98
    99# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the result of bool([0, 1, 2, 'n']) is True.

  • I change assertFalse to assertTrue for (bool([0, 1, 2, 'n']))

     95        self.assertIsNot(list(), False)
     96
     97        # self.assertFalse(bool([0, 1, 2, 'n']))
     98        self.assertTrue(bool([0, 1, 2, 'n']))
     99
    100
    101# NOTES
    

    the test passes.

  • I add a comment

    101# NOTES
    102# bool(a list with things) is True
    103# bool(a tuple with things) is True
    104# bool(a string with things) is True
    
  • I add an assertion without bool

     97        # self.assertFalse(bool([0, 1, 2, 'n']))
     98        self.assertTrue(bool([0, 1, 2, 'n']))
     99        self.assertFalse([0, 1, 2, 'n'])
    100
    101
    102# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: [0, 1, 2, 'n'] is not false
    

    because the result of bool([0, 1, 2, 'n']) is True.

  • I change assertFalse to assertTrue

     97        # self.assertFalse(bool([0, 1, 2, 'n']))
     98        self.assertTrue(bool([0, 1, 2, 'n']))
     99        # self.assertFalse([0, 1, 2, 'n'])
    100        self.assertTrue([0, 1, 2, 'n'])
    101
    102
    103# NOTES
    
    • The test passes because the result of bool([0, 1, 2, 'n']) is True.

    • A list with things is grouped as True.

  • I call assertIs to test if a list with things is the same object as True

     99        # self.assertFalse([0, 1, 2, 'n'])
    100        self.assertTrue([0, 1, 2, 'n'])
    101        self.assertIs([0, 1, 2, 'n'], True)
    102
    103
    104# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: [0, 1, 2, 'n'] is not True
    
  • I change assertIs to assertIsNot for ([0, 1, 2, 'n'], True)

     99        # self.assertFalse([0, 1, 2, 'n'])
    100        self.assertTrue([0, 1, 2, 'n'])
    101        # self.assertIs([0, 1, 2, 'n'], True)
    102        self.assertIsNot([0, 1, 2, 'n'], True)
    103
    104
    105# NOTES
    

    the test passes because a list is not the same object as True.

  • I add a variable for [0, 1, 2, 'n']

    95        self.assertIsNot(list(), False)
    96
    97        a_list = [0, 1, 2, 'n']
    98        # self.assertFalse(bool([0, 1, 2, 'n']))
    
  • I use the variable to remove repetition of [0, 1, 2, 'n']

     97        a_list = [0, 1, 2, 'n']
     98        # self.assertFalse(bool([0, 1, 2, 'n']))
     99        # self.assertTrue(bool([0, 1, 2, 'n']))
    100        self.assertTrue(bool(a_list))
    101        # self.assertFalse([0, 1, 2, 'n'])
    102        # self.assertTrue([0, 1, 2, 'n'])
    103        self.assertTrue(a_list)
    104        # self.assertIs([0, 1, 2, 'n'], True)
    105        # self.assertIsNot([0, 1, 2, 'n'], True)
    106        self.assertIsNot(a_list, True)
    107
    108
    109# NOTES
    

    the test is still green.

  • I remove the commented lines from test_is_a_list_falsy_or_truthy

    64    def test_is_a_list_falsy_or_truthy(self):
    65        self.assertFalse(bool(list()))
    66        self.assertFalse(list())
    67        self.assertIsNot(list(), False)
    68
    69        a_list = [0, 1, 2, 'n']
    70        self.assertTrue(bool(a_list))
    71        self.assertTrue(a_list)
    72        self.assertIsNot(a_list, True)
    73
    74
    75# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_list_falsy_or_truthy'
    

The empty list is grouped as False. A list with things is grouped as True.


test_is_a_set_falsy_or_truthy

Is a set grouped as False or True?

RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for if a set (anything in curly braces { }, not key-value pairs) is grouped as False or True

     97        self.assertIsNot(a_list, True)
     98
     99    def test_is_a_set_falsy_or_truthy(self):
    100        self.assertTrue(bool(set()))
    101
    102
    103# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the result of bool(set()) is False.


GREEN: make it pass


I change assertTrue to assertFalse for bool(set())

 99    def test_is_a_set_falsy_or_truthy(self):
100        # self.assertTrue(bool(set()))
101        self.assertFalse(bool(set()))
102
103
104# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    104# NOTES
    105# bool(a list with things) is True
    106# bool(a tuple with things) is True
    107# bool(a string with things) is True
    108# bool(a positive number) is True
    109# bool(a negative number) is True
    110# True is True
    111# True is an integer
    112# True is a boolean
    113# True is NOT False
    114# bool(the empty set) is False
    115# bool(the empty list) is False
    
  • I add an assertion without bool

     99    def test_is_a_set_falsy_or_truthy(self):
    100        # self.assertTrue(bool(set()))
    101        self.assertFalse(bool(set()))
    102        self.assertTrue(set())
    103
    104
    105# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: set() is not true
    

    because the result of bool(set()) is False.

  • I change assertTrue to assertFalse for bool(set())

     99    def test_is_a_set_falsy_or_truthy(self):
    100        # self.assertTrue(bool(set()))
    101        self.assertFalse(bool(set()))
    102        # self.assertTrue(set())
    103        self.assertFalse(set())
    104
    105
    106# NOTES
    
    • The test passes because the result of bool(set()) is False.

    • The empty set (anything in curly braces { }, not key-value pairs) is grouped as False.

  • I add a call to assertIs to test if the empty set is the same object as False

    102        # self.assertTrue(set())
    103        self.assertFalse(set())
    104        self.assertIs(set(), False)
    105
    106
    107# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: set() is not False
    
  • I change assertIs to assertIsNot for (set(), False)

    102        # self.assertTrue(set())
    103        self.assertFalse(set())
    104        # self.assertIs(set(), False)
    105        self.assertIsNot(set(), False)
    106
    107
    108# NOTES
    

    the test passes because a set is not the same object as False.

  • I add an assertion to test if a set with things is grouped as False or True

    105        self.assertIsNot(set(), False)
    106
    107        self.assertFalse(bool({0, 1, 2, 'n'}))
    108
    109
    110# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the result of bool({0, 1, 2, 'n'}) is True.

  • I change assertFalse to assertTrue for bool({0, 1, 2, 'n'})

    105        self.assertIsNot(set(), False)
    106
    107        # self.assertFalse(bool({0, 1, 2, 'n'}))
    108        self.assertTrue(bool({0, 1, 2, 'n'}))
    109
    110
    111# NOTES
    

    the test passes.

  • I add a comment

    111# NOTES
    112# bool(a set with things) is True
    113# bool(a list with things) is True
    114# bool(a tuple with things) is True
    
  • I add an assertion for {0, 1, 2, 'n'} without bool

    107        # self.assertFalse(bool({0, 1, 2, 'n'}))
    108        self.assertTrue(bool({0, 1, 2, 'n'}))
    109        self.assertFalse({0, 1, 2, 'n'})
    110
    111
    112# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: {0, 1, 2, 'n'} is not false
    

    because the result of bool({0, 1, 2, 'n'}) is True.

  • I change assertFalse to assertTrue for {0, 1, 2, 'n'}

    107        # self.assertFalse(bool({0, 1, 2, 'n'}))
    108        self.assertTrue(bool({0, 1, 2, 'n'}))
    109        # self.assertFalse({0, 1, 2, 'n'})
    110        self.assertTrue({0, 1, 2, 'n'})
    111
    112
    113# NOTES
    
    • The test passes because the result of bool({0, 1, 2, 'n'}) is True.

    • A set with things is grouped as True.

  • I add an assertion for if a set with things is the same object as True

    109        # self.assertFalse({0, 1, 2, 'n'})
    110        self.assertTrue({0, 1, 2, 'n'})
    111        self.assertIs({0, 1, 2, 'n'}, True)
    112
    113
    114# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: {0, 1, 2, 'n'} is not True
    
  • I change assertIs to assertIsNot for {0, 1, 2, 'n'}

    109        # self.assertFalse({0, 1, 2, 'n'})
    110        self.assertTrue({0, 1, 2, 'n'})
    111        # self.assertIs({0, 1, 2, 'n'}, True)
    112        self.assertIsNot({0, 1, 2, 'n'}, True)
    113
    114
    115# NOTES
    

    the test passes because a set is not the same object as True.

  • I add a variable for {0, 1, 2, 'n'}

    105        self.assertIsNot(set(), False)
    106
    107        a_set = {0, 1, 2, 'n'}
    108        # self.assertFalse(bool({0, 1, 2, 'n'}))
    
  • I use the variable to remove repetition of {0, 1, 2, 'n'}

    107        a_set = {0, 1, 2, 'n'}
    108        # self.assertFalse(bool({0, 1, 2, 'n'}))
    109        # self.assertTrue(bool({0, 1, 2, 'n'}))
    110        self.assertTrue(bool(a_set))
    111        # self.assertFalse({0, 1, 2, 'n'})
    112        # self.assertTrue({0, 1, 2, 'n'})
    113        self.assertTrue(a_set)
    114        # self.assertIs({0, 1, 2, 'n'}, True)
    115        # self.assertIsNot({0, 1, 2, 'n'}, True)
    116        self.assertIsNot(a_set, True)
    117
    118
    119# NOTES
    

    the test is still green.

  • I remove the commented lines from test_is_a_set_falsy_or_truthy

     99    def test_is_a_set_falsy_or_truthy(self):
    100        self.assertFalse(bool(set()))
    101        self.assertFalse(set())
    102        self.assertIsNot(set(), False)
    103
    104        a_set = {0, 1, 2, 'n'}
    105        self.assertTrue(bool(a_set))
    106        self.assertTrue(a_set)
    107        self.assertIsNot(a_set, True)
    108
    109
    110# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_set_falsy_or_truthy'
    

The empty set is grouped as False. A set with things is grouped as True.


test_is_a_dictionary_falsy_or_truthy

Is a dictionary grouped as False or True?

RED: make it fail



GREEN: make it pass


I change assertTrue to assertFalse for bool(dict())

109    def test_is_a_dictionary_falsy_or_truthy(self):
110        # self.assertTrue(bool(dict()))
111        self.assertFalse(bool(dict()))
112
113
114# NOTES

the test passes.


REFACTOR: make it better


  • I add a comment

    114# NOTES
    115# bool(a set with things) is True
    116# bool(a list with things) is True
    117# bool(a tuple with things) is True
    118# bool(a string with things) is True
    119# bool(a positive number) is True
    120# bool(a negative number) is True
    121# True is True
    122# True is an integer
    123# True is a boolean
    124# True is NOT False
    125# bool(the empty dictionary) is False
    126# bool(the empty set) is False
    
  • I add an assertion for dict() without bool

    109    def test_is_a_dictionary_falsy_or_truthy(self):
    110        # self.assertTrue(bool(dict()))
    111        self.assertFalse(bool(dict()))
    112        self.assertTrue(dict())
    113
    114
    115# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: {} is not true
    
    • because the result of bool(dict()) is False.

    • dict() is another way to write {} (the empty dictionary).

  • I change assertTrue to assertFalse for dict()

    109    def test_is_a_dictionary_falsy_or_truthy(self):
    110        # self.assertTrue(bool(dict()))
    111        self.assertFalse(bool(dict()))
    112        # self.assertTrue(dict())
    113        self.assertFalse(dict())
    114
    115
    116# NOTES
    
    • The test passes because the result of bool(dict()) is False.

    • The empty dictionary is grouped as False.

  • I add a call to assertIs for if the empty dictionary is the same object as False

    112        # self.assertTrue(dict())
    113        self.assertFalse(dict())
    114        self.assertIs(dict(), False)
    115
    116
    117# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: {} is not False
    
  • I change assertIs to assertIsNot for dict()

    112        # self.assertTrue(dict())
    113        self.assertFalse(dict())
    114        # self.assertIs(dict(), False)
    115        self.assertIsNot(dict(), False)
    116
    117
    118# NOTES
    

    the test passes because a dictionary is not the same object as False.

  • I add an assertion to test if a dictionary with things is grouped as False or True

    115        self.assertIsNot(dict(), False)
    116
    117        self.assertFalse(bool({'key': 'value'}))
    118
    119
    120# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

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

  • I change assertFalse to assertTrue bool({'key': 'value'})

    115        self.assertIsNot(dict(), False)
    116
    117        # self.assertFalse(bool({'key': 'value'}))
    118        self.assertTrue(bool({'key': 'value'}))
    119
    120
    121# NOTES
    

    the test passes.

  • I add a comment about a dictionary with things

    121# NOTES
    122# bool(a dictionary with things) is True
    123# bool(a set with things) is True
    
  • I add an assertion for {'key': 'value'} without bool

    117        # self.assertFalse(bool({'key': 'value'}))
    118        self.assertTrue(bool({'key': 'value'}))
    119        self.assertFalse({'key': 'value'})
    120
    121
    122# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: {'key': 'value'} is not false
    

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

  • I change assertFalse to assertTrue for {'key': 'value'}

    117        # self.assertFalse(bool({'key': 'value'}))
    118        self.assertTrue(bool({'key': 'value'}))
    119        # self.assertFalse({'key': 'value'})
    120        self.assertTrue({'key': 'value'})
    121
    122
    123# NOTES
    
    • The test passes because the result of bool({'key': 'value'}) is True.

    • A dictionary with things is grouped as True.

  • I add a call to assertIs to test if a dictionary with things is the same object as True

    119        # self.assertFalse({'key': 'value'})
    120        self.assertTrue({'key': 'value'})
    121        self.assertIs({'key': 'value'}, True)
    122
    123
    124# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: {'key': 'value'} is not True
    
  • I change assertIs to assertIsNot for {'key': 'value'}

    119        # self.assertFalse({'key': 'value'})
    120        self.assertTrue({'key': 'value'})
    121        # self.assertIs({'key': 'value'}, True)
    122        self.assertIsNot({'key': 'value'}, True)
    123
    124
    125# NOTES
    

    the test passes because a dictionary is not the same object as True.

  • I add a variable for {'key': 'value'}

    115    self.assertIsNot(dict(), False)
    116
    117    a_dictionary = {'key': 'value'}
    118    # self.assertFalse(bool({'key': 'value'}))
    
  • I use the variable to remove repetition of {'key': 'value'}

    117        a_dictionary = {'key': 'value'}
    118        # self.assertFalse(bool({'key': 'value'}))
    119        # self.assertTrue(bool({'key': 'value'}))
    120        self.assertTrue(bool(a_dictionary))
    121        # self.assertFalse({'key': 'value'})
    122        # self.assertTrue({'key': 'value'})
    123        self.assertTrue(a_dictionary)
    124        # self.assertIs({'key': 'value'}, True)
    125        # self.assertIsNot({'key': 'value'}, True)
    126        self.assertIsNot(a_dictionary, True)
    127
    128
    129# NOTES
    

    the test is still green.

  • I remove the commented lines from test_is_a_dictionary_falsy_or_truthy

    109    def test_is_a_dictionary_falsy_or_truthy(self):
    110        self.assertFalse(bool(dict()))
    111        self.assertFalse(dict())
    112        self.assertIsNot(dict(), False)
    113
    114        a_dictionary = {'key': 'value'}
    115        self.assertTrue(bool(a_dictionary))
    116        self.assertTrue(a_dictionary)
    117        self.assertIsNot(a_dictionary, True)
    118
    119
    120# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_is_a_dictionary_falsy_or_truthy'
    

The empty dictionary is grouped as False. A dictionary with things is grouped as True.


test_the_value_of_false

Since False is an int, what is its value?


RED: make it fail


  • I go back to the terminal where the tests are running.

  • I add a test for the value of False

    117        self.assertIsNot(a_dictionary, True)
    118
    119    def test_the_value_of_false(self):
    120        self.assertEqual(False+1, None)
    121
    122
    123# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 1 != None
    

GREEN: make it pass


I change the expectation to match

119    def test_the_value_of_false(self):
120        # self.assertEqual(False+1, None)
121        self.assertEqual(False+1, 1)
122
123
124# NOTES

the test passes.


REFACTOR: make it better


  • I add an assertion with subtraction

    119    def test_the_value_of_false(self):
    120        # self.assertEqual(False+1, None)
    121        self.assertEqual(False+1, 1)
    122        self.assertEqual(False-1, 1)
    123
    124
    125# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: -1 != 1
    

    the value of False is 0.

  • I change my expectation to match reality

    119    def test_the_value_of_false(self):
    120        # self.assertEqual(False+1, None)
    121        self.assertEqual(False+1, 1)
    122        # self.assertEqual(False-1, 1)
    123        self.assertEqual(False-1, -1)
    124
    125
    126# NOTES
    

    the test passes.

  • I add an assertion with multiplication

    122        # self.assertEqual(False-1, 1)
    123        self.assertEqual(False-1, -1)
    124        self.assertEqual(False*1, -1)
    125
    126
    127# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0 != -1
    

    the value of False is 0.,

  • I change my expectation

    122        # self.assertEqual(False-1, 1)
    123        self.assertEqual(False-1, -1)
    124        # self.assertEqual(False*1, -1)
    125        self.assertEqual(False*1, 0)
    126
    127
    128# NOTES
    

    the test passes.

  • what happens if I divide a number by False?

    124        # self.assertEqual(False*1, -1)
    125        self.assertEqual(False*1, 0)
    126        1 / False
    127
    128
    129# NOTES
    

    the terminal is my friend, and shows ZeroDivisionError because False is 0

    ZeroDivisionError: division by zero
    
  • I add ZeroDivisionError to the list of Exceptions seen

    151# False is NOT True
    152
    153
    154# Exceptions seen
    155# AssertionError
    156# ZeroDivisionError
    
  • I comment the line out then add a note about ZeroDivisionError

    124        # self.assertEqual(False*1, -1)
    125        self.assertEqual(False*1, 0)
    126
    127        # raises ZeroDivisionError
    128        # 1 / False
    129
    130
    131# NOTES
    

    the test goes back to green.

  • I remove the other commented lines from test_the_value_of_false

    119    def test_the_value_of_false(self):
    120        self.assertEqual(False+1, 1)
    121        self.assertEqual(False-1, -1)
    122        self.assertEqual(False*1, 0)
    123
    124        # raises ZeroDivisionError
    125        # 1 / False
    126
    127
    128# NOTES
    
  • I add a comment

    128# NOTES
    129# bool(a dictionary with things) is True
    130# bool(a set with things) is True
    131# bool(a list with things) is True
    132# bool(a tuple with things) is True
    133# bool(a string with things) is True
    134# bool(a positive number) is True
    135# bool(a negative number) is True
    136# True is True
    137# True is an integer
    138# True is a boolean
    139# True is NOT False
    
    140# bool(the empty dictionary) is False
    141# bool(the empty set) is False
    142# bool(the empty list) is False
    143# bool(the empty tuple) is False
    144# bool(the empty string) is False
    145# bool(zero) is False
    146# bool(None) is False
    147# False is False
    148# False is an integer
    149# the value of False is 0
    150# False is a boolean
    151# False is NOT True
    152
    153
    154# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_the_value_of_false'
    

In Python, the value of False is 0.


test_the_value_of_true

If the value of False is 0, what is the value of True?

RED: make it fail


I add a test to find out the value of True

124          # raises ZeroDivisionError
125          # 1 / False
126
127      def test_the_value_of_true(self):
128          self.assertEqual(True+1, 1)
129
130
131  # NOTES

the terminal is my friend, and shows AssertionError

AssertionError: 2 != 1

The value of True is 1.


GREEN: make it pass


I change the expectation

127    def test_the_value_of_true(self):
128        # self.assertEqual(True+1, 1)
129        self.assertEqual(True+1, 2)
130
131
132# NOTES

the test passes.


REFACTOR: make it better


  • I add another assertion

    127    def test_the_value_of_true(self):
    128        # self.assertEqual(True+1, 1)
    129        self.assertEqual(True+1, 2)
    130        self.assertEqual(True-1, 2)
    131
    132
    133# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0 != 2
    

    The value of True is 1.

  • I change my expectation to match reality

    127    def test_the_value_of_true(self):
    128        # self.assertEqual(True+1, 1)
    129        self.assertEqual(True+1, 2)
    130        # self.assertEqual(True-1, 2)
    131        self.assertEqual(True-1, 0)
    132
    133
    134# NOTES
    

    the test passes.

  • I add an assertion with multiplication

    130        # self.assertEqual(True-1, 2)
    131        self.assertEqual(True-1, 0)
    132        self.assertEqual(True*1, 0)
    133
    134
    135# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 1 != 0
    

    The value of True is 1.

  • I change the expectation

    130        # self.assertEqual(True-1, 2)
    131        self.assertEqual(True-1, 0)
    132        # self.assertEqual(True*1, 0)
    133        self.assertEqual(True*1, 1)
    134
    135
    136# NOTES
    

    the test passes.

  • I add an assertion with division

    132        # self.assertEqual(True*1, 0)
    133        self.assertEqual(True*1, 1)
    134        self.assertEqual(True/2, 1)
    135
    136
    137# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0.5 != 1
    

    The value of True is 1.

  • I change the assertion

    132        # self.assertEqual(True*1, 0)
    133        self.assertEqual(True*1, 1)
    134        # self.assertEqual(True/2, 1)
    135        self.assertEqual(True/1, 1)
    136
    137
    138# NOTES
    

    the test passes.

  • I remove the commented lines from test_the_value_of_true

    127    def test_the_value_of_true(self):
    128        self.assertEqual(True+1, 2)
    129        self.assertEqual(True-1, 0)
    130        self.assertEqual(True*1, 1)
    131        self.assertEqual(True/1, 1)
    132
    133
    134# NOTES
    
  • I add a comment about the value of True

    134# NOTES
    135# bool(a dictionary with things) is True
    136# bool(a set with things) is True
    137# bool(a list with things) is True
    138# bool(a tuple with things) is True
    139# bool(a string with things) is True
    140# bool(a positive number) is True
    141# bool(a negative number) is True
    142# True is True
    143# True is an integer
    144# the value of True is 1
    145# True is a boolean
    146# True is NOT False
    
    147# bool(the empty dictionary) is False
    148# bool(the empty set) is False
    149# bool(the empty list) is False
    150# bool(the empty tuple) is False
    151# bool(the empty string) is False
    152# bool(zero) is False
    153# bool(None) is False
    154# False is False
    155# False is an integer
    156# the value of False is 0
    157# False is a boolean
    158# False is NOT True
    159
    160
    161# Exceptions seen
    162# AssertionError
    163# ZeroDivisionError
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_the_value_of_true'
    

In Python the value of True is 1.


close the project

  • I close test_booleans.py

  • I click in the terminal where the tests are running

  • I 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
    

review

I know that bool is an object. It only has two instances - False and True

In Python the following objects are grouped as

This comes in handy when I want programs to make decisions, because they can choose what to do based on if an object is grouped as False (0, empty or None ) or is grouped as True (positive and negative numbers or has something in it).

I can use assertFalse in place of assertIs(bool(x), False) and assertTrue for assertIs(bool(x), True).

How many questions can you answer after going through this chapter?


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

So far I have done the same steps to make a Python Test Driven Development environment. Would you like to see me turn those steps into a program to automatically make a Test Driven Development project with one command?.


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.