AssertionError



The AssertionError is raised to show that a statement is False. It was introduced in how to make a python test driven development environment with the first failing test

self.assertFalse(True)

which is like

assert True is False

When building a program I can use assert statements to make sure something is True before it continues, I can also use them to test how the program behaves, for example when it is given inputs, which can help catch things that break previous tested behavior when added. They also help me answer 2 questions

  • what is the same?

  • what is different?

The difference between my expectations and reality (what happens when the program runs), gives me a clue about what to change to make them match.


test_assertion_error_w_none

red: make it fail

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

    ./makePythonTdd.sh assertion_error
    

    on Windows without Windows Subsystem Linux use makePythonTdd.ps1

    ./makePythonTdd.ps1 assertion_error
    

    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_assertion_error.py:7: AssertionError
    
  • I hold ctrl (windows/linux) or option (mac) on the keyboard and use the mouse to click on tests/test_assertion_error.py:7 to open it in the editor

  • then change True to False to make the test pass

  • and change test_failure to test_assertion_error_w_none

    import unittest
    
    
    class TestAssertionError(unittest.TestCase):
    
        def test_assertion_error_w_none(self):
            assert None is not None
    

    the terminal shows AssertionError

    E    assert None is not None
    
    tests/test_assertion_error.py:7: AssertionError
    

    the assert at the beginning of the line makes the statement something like “DO NOT CONTINUE, UNLESS None is NOT None”. The AssertionError is raised to break execution of the program because the statement is False

green: make it pass

When I change the failing line

def test_assertion_error_w_none(self):
    assert None is None

the test passes because this statement is True

refactor: make it better

I can also make assertions with some methods from the unittest.TestCase class

  • I add another failing line using the assertIsNotNone method which checks if something is NOT None

    def test_assertion_error_w_none(self):
        assert None is None
        self.assertIsNotNone(None)
    

    the terminal shows a more descriptive message for the AssertionError

    AssertionError: unexpectedly None
    

    when I change the statement to use the assertIsNone method which checks if something is None

    def test_assertion_error_w_none(self):
        assert None is None
        self.assertIsNone(None)
    

    the test passes and I add a note

    # NOTES
    # None is None
    
    
    # Exceptions Encountered
    # AssertionError
    
  • then I add a new failing line

    def test_assertion_error_w_none(self):
        assert None is None
        self.assertIsNone(None)
    
        assert False is None
    

    the terminal shows AssertionError

    E    assert False is None
    

    I change the line to make it True

    assert False is not None
    

    and the test passes. I add another line with the assertIsNone method which checks if something is None

    def test_assertion_error_w_none(self):
        assert None is None
        self.assertIsNone(None)
    
        assert False is not None
        self.assertIsNone(False)
    

    the terminal shows AssertionError

    AssertionError: False is not None
    

    I change the statement

    assert False is None
    self.assertIsNotNone(False)
    

    and the test passes. I add another note

    # NOTES
    # False is not None
    # None is None
    
  • then I add a failing line

    def test_assertion_error_w_none(self):
        assert None is None
        self.assertIsNone(None)
    
        assert False is not None
        self.assertIsNotNone(False)
    
        assert True is None
    

    the terminal shows AssertionError

    E    assert True is None
    

    I change the statement to make it True

    assert True is not None
    

    and the terminal shows green again. I add a failing line using the assertIsNone method

    def test_assertion_error_w_none(self):
        assert None is None
        self.assertIsNone(None)
    
        assert False is not None
        self.assertIsNotNone(False)
    
        assert True is not None
        self.assertIsNone(True)
    

    the terminal shows AssertionError

    AssertionError: True is not None
    

    when I make the statement True

    assert True is not None
    self.assertIsNotNone(True)
    

    the test passes and I add a note

    # NOTES
    # True is not None
    # False is not None
    # None is None
    

test_assertion_error_w_false

I can test if something is False

red: make it fail

I add a failing test

def test_assertion_error_w_false(self):
    assert True is False

the terminal shows AssertionError

E    assert True is False

green: make it pass

I change the line to make it True

def test_assertion_error_w_false(self):
    assert True is not False

and the terminal shows all tests are passing

red: make it fail

There is a method for this, it is the one from the first failing test which checks if something is False

def test_assertion_error_w_false(self):
    assert True is not False
    self.assertFalse(True)

the terminal shows AssertionError

AssertionError: True is not false

green: make it pass

I change the failing line

def test_assertion_error_w_false(self):
    assert True is not False
    self.assertFalse(False)

and the test passes. Time to add notes

# NOTES
# False is False
# True is not False
# True is not None
# False is not None
# None is None

test_assertion_error_w_true

I can test if something is True

red: make it fail

I add a failing test

def test_assertion_error_w_true(self):
    assert False is True

and the terminal shows AssertionError

E    assert False is True

green: make it pass

then I make the statement True

def test_assertion_error_w_true(self):
    assert False is not True

and the terminal shows green again

red: make it fail

there is also a method to check if something is True

def test_assertion_error_w_true(self):
    assert False is not True
    self.assertTrue(False)

the terminal shows AssertionError

AssertionError: False is not true

green: make it pass

I change the failing line to a True statement

def test_assertion_error_w_true(self):
    assert False is not True
    self.assertTrue(True)

and all tests are passing. I add more notes

# NOTES
# True is True
# False is not True
# False is False
# True is not False
# True is not None
# False is not None
# None is None

These statements can be summed up as - True, False and None are different. They set up a basic expectation because I can compare things to them.


test_assertion_error_w_equality

I can also test if 2 things are equal

red: make it fail

I add a new test

def test_assertion_error_w_equality(self):
    assert None != None

the terminal shows AssertionError

E    assert None != None

!= is the symbol for NOT equal which makes this statement read as assert None is NOT equal to None

green: make it pass

I change the failing line and the test passes

def test_assertion_error_w_equality(self):
    assert None == None

== is the symbol for is equal which makes this statement read as assert None is equal to None

refactor: make it better

  • there is a method for this

    def test_assertion_error_w_equality(self):
        assert None == None
        self.assertNotEqual(None, None)
    

    unittest.TestCase.assertNotEqual checks if the 2 things given are NOT equal, and the terminal shows AssertionError

    AssertionError: None == None
    

    when I use the unittest.TestCase.assertEqual method to make the failing line True

    def test_assertion_error_w_equality(self):
        assert None == None
        self.assertEqual(None, None)
    

    the terminal shows passing tests and I update the notes

    # NOTES
    # True is True
    # False is not True
    # False is False
    # True is not False
    # True is not None
    # False is not None
    # None is None and equal to None
    
  • then I add a new failing line

    def test_assertion_error_w_equality(self):
        assert None == None
        self.assertEqual(None, None)
    
        assert False == None
    

    and the terminal shows AssertionError

    E    assert False == None
    

    when I change the failing line to a True statement

    assert False != None
    

    the terminal shows passing tests. I add the unittest.TestCase.assertEqual method

    assert False != None
    self.assertEqual(False, None)
    

    and the terminal shows AssertionError

    AssertionError: False != None
    

    I change the failing line to a True statement

    assert False != None
    self.assertNotEqual(False, None)
    

    and the test passes. I update the notes to say that

    # NOTES
    # True is True
    # False is not True
    # False is False
    # True is not False
    # True is not None
    # False is not None and not equal to None
    # None is None and equal to None
    
  • I add the next failing line

    def test_assertion_error_w_equality(self):
        assert None == None
        self.assertEqual(None, None)
    
        assert False != None
        self.assertNotEqual(False, None)
    
        assert True == None
    

    and get AssertionError

    E    assert True == None
    

    I change the statement

    assert True != None
    

    and the test passes. I do the same thing with an assert method

    assert True != None
    self.assertEqual(True, None)
    

    the terminal shows AssertionError

    AssertionError: True != None
    

    I change the method

    assert True != None
    self.assertNotEqual(True, None)
    

    and the test is green again. I add a note

    # NOTES
    # True is True
    # False is not True
    # False is False
    # True is not False
    # True is not None and not equal to None
    # False is not None and not equal to None
    # None is None and equal to None
    
  • then I add another failing line

    def test_assertion_error_w_equality(self):
        assert None == None
        self.assertEqual(None, None)
    
        assert False != None
        self.assertNotEqual(False, None)
    
        assert True != None
        self.assertNotEqual(True, None)
    
        assert True == False
    

    the terminal shows AssertionError

    E    assert True == False
    

    I change the failing line

    assert True != False
    

    and the test passes. I add a call to assertEqual

    assert True != False
    self.assertEqual(True, False)
    

    and get AssertionError

    AssertionError: True != False
    

    then I change the method

    assert True != False
    self.assertNotEqual(True, False)
    

    and the test passes. I add a note

    # NOTES
    # True is True
    # False is not True
    # False is False
    # True is not False and not equal to False
    # True is not None and not equal to None
    # False is not None and not equal to None
    # None is None and equal to None
    
  • on to the next line

    def test_assertion_error_w_equality(self):
        assert None == None
        self.assertEqual(None, None)
    
        assert False != None
        self.assertNotEqual(False, None)
    
        assert True != None
        self.assertNotEqual(True, None)
    
        assert True != False
        self.assertNotEqual(True, False)
    
        assert False != False
    

    which gives me AssertionError

    E    assert False != False
    

    I make the line True

    assert False == False
    

    and the test passes. I add another failing line

    assert False == False
    self.assertNotEqual(False, False)
    

    the terminal shows AssertionError

    AssertionError: False == False
    

    when I change the method

    assert False == False
    self.assertEqual(False, False)
    

    the test passes and I add a note

    # NOTES
    # True is True
    # False is not True
    # False is False and equal to False
    # True is not False and not equal to False
    # True is not None and not equal to None
    # False is not None and not equal to None
    # None is None and equal to None
    
  • I add another failing line

    def test_assertion_error_w_equality(self):
        assert None == None
        self.assertEqual(None, None)
    
        assert False != None
        self.assertNotEqual(False, None)
    
        assert True != None
        self.assertNotEqual(True, None)
    
        assert True != False
        self.assertNotEqual(True, False)
    
        assert False == False
        self.assertEqual(False, False)
    
        assert False == True
    

    the terminal shows AssertionError

    E    assert False == True
    

    I change the line

    assert False != True
    

    and the terminal shows passing tests. I add another failing line

    assert False != True
    self.assertEqual(False, True)
    

    and get AssertionError

    AssertionError: False != True
    

    then I change the method

    assert False != True
    self.assertNotEqual(False, True)
    

    and the test passes. I add another note

    # NOTES
    # True is True
    # False is not True and not equal to True
    # False is False and equal to False
    # True is not False and not equal to False
    # True is not None and not equal to None
    # False is not None and not equal to None
    # None is None and equal to None
    
  • time for the last statement

    def test_assertion_error_w_equality(self):
        assert None == None
        self.assertEqual(None, None)
    
        assert False != None
        self.assertNotEqual(False, None)
    
        assert True != None
        self.assertNotEqual(True, None)
    
        assert True != False
        self.assertNotEqual(True, False)
    
        assert False == False
        self.assertEqual(False, False)
    
        assert False != True
        self.assertNotEqual(False, True)
    
        assert True != True
    

    the terminal shows AssertionError

    E    assert True != True
    

    when I make the line True

    assert True == True
    

    the test passes. I add a line with the assert method

    assert True == True
    self.assertNotEqual(True, True)
    

    and the terminal shows AssertionError

    AssertionError: True == True
    

    I change the method

    assert True == True
    self.assertEqual(True, True)
    

    and all tests are passing. I add a note for the last statement

    # NOTES
    # True is True and equal to True
    # False is not True and not equal to True
    # False is False and equal to False
    # True is not False and not equal to False
    # True is not None and not equal to None
    # False is not None and not equal to None
    # None is None and equal to None
    

review

With assert statements and methods I can test if something is

Would you like to test the AttributeError?


AssertionError: tests