booleans



There are 2 booleans - True and False

test_what_is_false

red: make it fail

  • I open a terminal to run makePythonTdd.sh with booleans as the name of the project

    ./makePythonTdd.sh booleans
    

    on Windows without Windows Subsystem Linux use makePythonTdd.ps1

    ./makePythonTdd.ps1 booleans
    

    it makes the folders and files that are needed, installs packages, runs the first test, and the terminal shows AssertionError

    E       AssertionError: True is not false
    
    tests/test_booleans.py:7: AssertionError
    
  • I hold ctrl (windows/linux) or option (mac) on the keyboard and use the mouse to click on tests/test_booleans.py:7 to open it in the editor

  • then change True to False to make the test pass

  • and change test_failure to test_what_is_false to check if False is an instance of the boolean class

    import unittest
    
    
    class TestBooleans(unittest.TestCase):
    
        def test_what_is_false(self):
            self.assertNotIsInstance(False, bool)
    

    the terminal shows AssertionError

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

green: make it pass

I change assertNotIsInstance to assertIsInstance

def test_what_is_false(self):
    self.assertIsInstance(False, bool)

the test passes and I add a note

# NOTES
# False is a boolean


# Exceptions Encountered
# AssertionError

test_what_is_true

red: make it fail

I add another test

def test_what_is_false(self):
    self.assertIsInstance(False, bool)

def test_what_is_true(self):
    self.assertNotIsInstance(True, bool)

the terminal shows AssertionError

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

green: make it pass

I change the method

def test_what_is_true(self):
    self.assertIsInstance(True, bool)

the test passes and I add another note

# NOTES
# True is a boolean
# False is a boolean

refactor: make it better

  • I add a failing line to test_what_is_true

    def test_what_is_true(self):
        self.assertIsInstance(True, bool)
        self.assertTrue(False)
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    I add a note

    # NOTES
    # True is a boolean
    # False is not true
    # False is a boolean
    

    when I change assertTrue to assertFalse

    def test_what_is_true(self):
        self.assertIsInstance(True, bool)
        self.assertFalse(False)
    

    the test passes and I move the line to the test_what_is_false method

    def test_what_is_false(self):
        self.assertIsInstance(False, bool)
        self.assertFalse(False)
    
    def test_what_is_true(self):
        self.assertIsInstance(True, bool)
    

    then I add a note

    # NOTES
    # True is a boolean
    # False is false
    # False is not true
    # False is a boolean
    
  • I add a failing line to test_what_is_false

    def test_what_is_false(self):
        self.assertIsInstance(False, bool)
        self.assertFalse(False)
        self.assertFalse(True)
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    I add a note

    # NOTES
    # True is not false
    # True is a boolean
    # False is false
    # False is not true
    # False is a boolean
    

    then change assertFalse to assertTrue

    def test_what_is_false(self):
        self.assertIsInstance(False, bool)
        self.assertFalse(False)
        self.assertTrue(True)
    

    the test passes and I move the line to the test_what_is_true method

    def test_what_is_false(self):
        self.assertIsInstance(False, bool)
        self.assertFalse(False)
    
    def test_what_is_true(self):
        self.assertIsInstance(True, bool)
        self.assertTrue(True)
    

    then I add another note

    # NOTES
    # True is true
    # True is not false
    # True is a boolean
    # False is false
    # False is not true
    # False is a boolean
    

I want to test the other Python basic data types to see if any of them are False or True


is None False or True?

red: make it fail

I add a line

def test_what_is_true(self):
    self.assertIsInstance(True, bool)
    self.assertTrue(True)
    self.assertTrue(None)

the terminal shows AssertionError

AssertionError: None is not true

green: make it pass

I change the method

self.assertFalse(None)

and the terminal shows passing tests

refactor: make it better

I move the line to test_what_is_false

def test_what_is_false(self):
    self.assertIsInstance(False, bool)
    self.assertFalse(False)
    self.assertFalse(None)

def test_what_is_true(self):
    self.assertIsInstance(True, bool)
    self.assertTrue(True)

then add a note

# NOTES
# True is true
# True is not false
# True is a boolean
# None is false
# False is false
# False is not true
# False is a boolean

is an integer False or True?

red: make it fail

I add another line

def test_what_is_false(self):
    self.assertIsInstance(False, bool)
    self.assertFalse(False)
    self.assertFalse(None)
    self.assertFalse(-1)

the terminal shows AssertionError

AssertionError: -1 is not false

green: make it pass

I change the method

self.assertTrue(-1)

and the test passes

refactor: make it better

  • then I move the line to test_what_is_true

    def test_what_is_true(self):
        ...
        self.assertTrue(-1)
    
  • I add a new line

    def test_what_is_true(self):
        ...
        self.assertTrue(-1)
        self.assertTrue(0)
    

    the terminal shows AssertionError

    AssertionError: 0 is not true
    

    I change the method

    self.assertFalse(0)
    

    the test passes and I move the line to test_what_is_false

    def test_what_is_false(self):
        ...
        self.assertFalse(0)
    
  • I add one more line

    def test_what_is_false(self):
        ...
        self.assertFalse(0)
        self.assertFalse(1)
    

    the terminal shows AssertionError

    AssertionError: 1 is not false
    

    when I change the method

    self.assertTrue(1)
    

    the test passes and I move the line to test_what_is_true

    def test_what_is_false(self):
        ...
        self.assertFalse(0)
    
    def test_what_is_true(self):
        ...
        self.assertTrue(-1)
        self.assertTrue(1)
    

    then I add notes

    # NOTES
    # positive and negative integers are true
    # True is true
    # True is not false
    # True is a boolean
    # 0 is false
    # None is false
    # False is false
    # False is not true
    # False is a boolean
    

is a float False or True?

red: make it fail

I add a line to test

def test_what_is_false(self):
    self.assertIsInstance(False, bool)
    self.assertFalse(False)
    self.assertFalse(None)
    self.assertFalse(0)
    self.assertFalse(-0.1)

the terminal shows AssertionError

AssertionError: -0.1 is not false

green: make it pass

I change the method

self.assertTrue(-0.1)

the test passes and I move the line to test_what_is_true

def test_what_is_true(self):
    ...
    self.assertTrue(-0.1)

refactor: make it better

  • I add a line

    def test_what_is_true(self):
        ...
        self.assertTrue(-0.1)
        self.assertTrue(0.0)
    

    the terminal shows AssertionError

    AssertionError: 0.0 is not true
    

    I change the method

    self.assertFalse(0.0)
    

    the terminal shows passing tests and I move the line to test_what_is_false

    def test_what_is_false(self):
        ...
        self.assertFalse(0.0)
    
  • I add another line

    def test_what_is_false(self):
        ...
        self.assertFalse(0.0)
        self.assertFalse(0.1)
    

    the terminal shows AssertionError

    AssertionError: 0.1 is not false
    

    when I change the method

    self.assertTrue(0.1)
    

    the test passes and I move the line to test_what_is_true

    def test_what_is_false(self):
        ...
        self.assertFalse(0.0)
    
    def test_what_is_true(self):
        ...
        self.assertTrue(-0.1)
        self.assertTrue(0.1)
    

    then I add notes

    # NOTES
    # positive and negative floats are true
    # positive and negative integers are true
    # True is true
    # True is not false
    # True is a boolean
    # 0.0 is false
    # 0 is false
    # None is false
    # False is false
    # False is not true
    # False is a boolean
    

    I can make the new notes simpler because floats and integers are numbers and 0.0 is the same as 0 even though they are different types

    # NOTES
    # positive and negative numbers are true
    # True is true
    # True is not false
    # True is a boolean
    # 0 is false
    # None is false
    # False is false
    # False is not true
    # False is a boolean
    

is a string False or True?

red: make it fail

I add a line to test

def test_what_is_true(self):
    self.assertIsInstance(True, bool)
    self.assertTrue(True)
    self.assertTrue(-1)
    self.assertTrue(1)
    self.assertTrue(-0.1)
    self.assertTrue(0.1)
    self.assertTrue(str())

the terminal shows AssertionError

AssertionError: '' is not true

the empty string is not True

green: make it pass

I change the method

self.assertFalse(str())

and the test passes

refactor: make it better

  • I move the line to test_what_is_false

    def test_what_is_false(self):
        ...
        self.assertFalse(str())
    
  • then I add another line

    def test_what_is_false(self):
        ...
        self.assertFalse(str())
        self.assertFalse('text')
    

    the terminal shows AssertionError

    AssertionError: 'text' is not false
    

    I change the method

    self.assertTrue('text')
    

    the test passes and I move the line to test_what_is_true

    def test_what_is_false(self):
        ...
        self.assertFalse(str())
    
    def test_what_is_true(self):
        ...
        self.assertTrue('text')
    

    I add notes

    # NOTES
    # a string with things is true
    # positive and negative numbers are true
    # True is true
    # True is not false
    # True is a boolean
    # the empty string is false
    # 0 is false
    # None is false
    # False is false
    # False is not true
    # False is a boolean
    

is a tuple False or True?

red: make it fail

I add a line to see

def test_what_is_true(self):
    self.assertIsInstance(True, bool)
    self.assertTrue(True)
    self.assertTrue(-1)
    self.assertTrue(1)
    self.assertTrue(-0.1)
    self.assertTrue(0.1)
    self.assertTrue('text')
    self.assertTrue(tuple())

The terminal shows AssertionError

AssertionError: () is not true

the empty tuple is not True

green: make it pass

I change the method

self.assertTrue(tuple())

the test passes and I move the line to test_what_is_false

def test_what_is_false(self):
    ...
    self.assertFalse(tuple())

refactor: make it better

  • I add another line

    def test_what_is_false(self):
        ...
        self.assertFalse(tuple())
        self.assertFalse((1, 2, 3, 'n'))
    

    the terminal shows AssertionError

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

    I change the method

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

    the test passes and I move the line to test_what_is_true

    def test_what_is_false(self):
        ...
        self.assertFalse(tuple())
    
    def test_what_is_true(self):
        ...
        self.assertTrue((1, 2, 3, 'n'))
    

    then I add notes

    # NOTES
    # a tuple with things is true
    # a string with things is true
    # positive and negative numbers are true
    # True is true
    # True is not false
    # True is a boolean
    # the empty tuple is false
    # the empty string is false
    # 0 is false
    # None is false
    # False is false
    # False is not true
    # False is a boolean
    

is a list False or True?

red: make it fail

I add a line

def test_what_is_true(self):
    self.assertIsInstance(True, bool)
    self.assertTrue(True)
    self.assertTrue(-0.1)
    self.assertTrue(1)
    self.assertTrue(-0.1)
    self.assertTrue(0.1)
    self.assertTrue('text')
    self.assertTrue((1, 2, 3, 'n'))
    self.assertTrue(list())

the terminal shows AssertionError

AssertionError: [] is not true

the empty list is not True

green: make it pass

I change the method

self.assertFalse(list())

and the test passes, then I move the line to test_what_is_false

def test_what_is_false(self):
    ...
    self.assertFalse(list())

refactor: make it better

  • I add another line

    def test_what_is_false(self):
        ...
        self.assertFalse(list())
        self.assertFalse([1, 2, 3, 'n'])
    

    the terminal shows AssertionError

    AssertionError: [1, 2, 3, 'n'] is not false
    
  • I change the method

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

    the test passes and I move the line to test_what_is_true

    def test_what_is_false(self):
        ...
        self.assertFalse(list())
    
    def test_what_is_true(self):
        ...
        self.assertTrue([1, 2, 3, 'n'])
    

    then add notes

    # NOTES
    # a list with things is true
    # a tuple with things is true
    # a string with things is true
    # positive and negative numbers are true
    # True is true
    # True is not false
    # True is a boolean
    # the empty list is false
    # the empty tuple is false
    # the empty string is false
    # 0 is false
    # None is false
    # False is false
    # False is not true
    # False is a boolean
    

is a set False or True?

red: make it fail

I add a line to test

def test_what_is_true(self):
    self.assertIsInstance(True, bool)
    self.assertTrue(True)
    self.assertTrue(-0.1)
    self.assertTrue(0.1)
    self.assertTrue(-1)
    self.assertTrue(1)
    self.assertTrue('text')
    self.assertTrue((1, 2, 3, 'n'))
    self.assertTrue([1, 2, 3, 'n'])
    self.assertTrue(set())

the terminal shows AssertionError

AssertionError: set() is not true

the empty set is not True

green: make it pass

I change the method

self.assertFalse(set())

the test passes and I move the line to test_what_is_false

def test_what_is_false(self):
    ...
    self.assertFalse(set())

refactor: make it better

  • I add another line

    def test_what_is_false(self):
        ...
        self.assertFalse(set())
        self.assertFalse({1, 2, 3, 'n'})
    

    the terminal shows AssertionError

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

    I change the method

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

    the test passes and I move the line to test_what_is_true

    def test_what_is_false(self):
        ...
        self.assertFalse(set())
    
    def test_what_is_true(self):
        ...
        self.assertTrue({1, 2, 3, 'n'})
    

    I add more notes

    # NOTES
    # a set with things is true
    # a list with things is true
    # a tuple with things is true
    # a string with things is true
    # positive and negative numbers are true
    # True is true
    # True is not false
    # True is a boolean
    # the empty set is false
    # the empty list is false
    # the empty tuple is false
    # the empty string is false
    # 0 is false
    # None is false
    # False is false
    # False is not true
    # False is a boolean
    

is a dictionary False or True?

red: make it fail

I add a line to test if a dictionary is False or True

def test_what_is_true(self):
    self.assertIsInstance(True, bool)
    self.assertTrue(True)
    self.assertTrue(-1)
    self.assertTrue(1)
    self.assertTrue(-0.1)
    self.assertTrue(0.1)
    self.assertTrue('text')
    self.assertTrue((1, 2, 3, 'n'))
    self.assertTrue([1, 2, 3, 'n'])
    self.assertTrue({1, 2, 3, 'n'})
    self.assertTrue(dict())

the terminal shows AssertionError

AssertionError: {} is not true

the empty dictionary is not True

green: make it pass

when I change assertTrue to assertFalse

self.assertFalse(dict())

the test passes and I move the line to the test_what_is_false method

def test_what_is_false(self):
    self.assertIsInstance(False, bool)
    self.assertFalse(False)
    self.assertFalse(None)
    self.assertFalse(0)
    self.assertFalse(0.0)
    self.assertFalse(str())
    self.assertFalse(tuple())
    self.assertFalse(list())
    self.assertFalse(set())
    self.assertFalse(dict())

refactor: make it better

  • I add another line to test if a dictionary with things is also False

    def test_what_is_false(self):
        self.assertIsInstance(False, bool)
        self.assertFalse(False)
        self.assertFalse(None)
        self.assertFalse(0)
        self.assertFalse(0.0)
        self.assertFalse(str())
        self.assertFalse(tuple())
        self.assertFalse(list())
        self.assertFalse(set())
        self.assertFalse(dict())
        self.assertFalse({'key': 'value'})
    

    the terminal shows AssertionError

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

    I change assertFalse to assertTrue

    self.assertTrue({'key': 'value'})
    

    the test passes and I move the line to the test_what_is_true method

    def test_what_is_false(self):
        self.assertIsInstance(False, bool)
        self.assertFalse(False)
        self.assertFalse(None)
        self.assertFalse(0)
        self.assertFalse(0.0)
        self.assertFalse(str())
        self.assertFalse(tuple())
        self.assertFalse(list())
        self.assertFalse(set())
        self.assertFalse(dict())
    
    def test_what_is_true(self):
        self.assertIsInstance(True, bool)
        self.assertTrue(True)
        self.assertTrue(-1)
        self.assertTrue(1)
        self.assertTrue(-0.1)
        self.assertTrue(0.1)
        self.assertTrue('text')
        self.assertTrue((1, 2, 3, 'n'))
        self.assertTrue([1, 2, 3, 'n'])
        self.assertTrue({1, 2, 3, 'n'})
        self.assertTrue({'key': 'value'})
    

    then I add the last 2 notes

    # NOTES
    # a dictionary with things is true
    # a set with things is true
    # a list with things is true
    # a tuple with things is true
    # a string with things is true
    # positive and negative numbers are true
    # True is true
    # True is not false
    # True is a boolean
    # the empty dictionary is false
    # the empty set is false
    # the empty list is false
    # the empty tuple is false
    # the empty string is false
    # 0 is false
    # None is false
    # False is false
    # False is not true
    # False is a boolean
    

review

From the tests I can see that

Would you like to test the truth table?


data structures: Booleans: tests