AssertionError


I can use assertions when making a program 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.

Assertions can help catch things that break passing tests when I add new lines of code. They also help me answer 2 questions

  • what is the same?

  • what is different?

The difference between my expectations (tests and ideas) and reality (what happens when the program runs), tells me what to change to make them match. The closer my program is to reality, the better.

preview

Here are the tests I have by the end of the chapter

 1import unittest
 2
 3
 4class TestAssertionError(unittest.TestCase):
 5
 6    def test_what_is_an_assertion(self):
 7        assert 1 + 1 == 2
 8        self.assertEqual(1+1, 2)
 9
10        assert '1' + '1' == '11'
11        self.assertEqual('1'+'1', '11')
12
13        assert 'I am' + ' a programmer' == 'I am a programmer'
14        self.assertEqual('I am'+' a programmer', 'I am a programmer')
15
16    def test_assertion_error_w_none(self):
17        assert None is None
18        self.assertIsNone(None)
19
20        assert False is not None
21        self.assertIsNotNone(False)
22
23        assert True is not None
24        self.assertIsNotNone(True)
25
26    def test_assertion_error_w_false(self):
27        assert True is not False
28        self.assertFalse(False)
29
30    def test_assertion_error_w_true(self):
31        assert False is not True
32        self.assertTrue(True)
33
34    def test_assertion_error_w_equality(self):
35        assert None == None
36        self.assertEqual(None, None)
37
38        assert False != None
39        self.assertNotEqual(False, None)
40
41        assert True != None
42        self.assertNotEqual(True, None)
43
44        assert True != False
45        self.assertNotEqual(True, False)
46
47        assert False == False
48        self.assertEqual(False, False)
49
50        assert False != True
51        self.assertNotEqual(False, True)
52
53        assert True == True
54        self.assertEqual(True, True)
55
56
57# NOTES
58# True is True and equal to True
59# False is not True and not equal to True
60# False is False and equal to False
61# True is not False and not equal to False
62# True is not None and not equal to None
63# False is not None and not equal to None
64# None is None and equal to None
65
66
67# Exceptions Encountered
68# AssertionError

questions about AssertionError

Here are questions you can answer after going through this chapter

requirements

  • I pick assertion_error as the name of this project

  • I open a terminal

  • then I make a directory for the project

    mkdir assertion_error
    

    the terminal goes back to the command line

    .../pumping_python
    
  • I change directory to the project

    cd assertion_error
    

    the terminal shows I am now in the assertion_error folder

    .../pumping_python/assertion_error
    
  • I make a folder for the source code

    mkdir src
    

    the terminal goes back to the command line

    .../pumping_python/assertion_error
    
  • I use touch to make an empty file for the program in the src folder

    touch src/assertion_error.py
    

    on Windows without Windows Subsystem for Linux use New-Item src/assertion_error.py instead of touch src/assertion_error.py

    New-Item src/assertion_error.py
    

    the terminal goes back to the command line

    .../pumping_python/assertion_error
    
  • I make a directory for the tests

    mkdir tests
    

    the terminal goes back to the command line

  • I use touch to make an empty file in the tests folder to tell Python that it is a Python package

    Attention

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

    touch tests/__init__.py
    

    on Windows without Windows Subsystem for Linux use New-Item tests/__init__.py instead of touch tests/__init__.py

    New-Item tests/__init__.py
    

    the terminal goes back to the command line

  • I make an empty file for the actual test

    touch tests/test_assertion_error.py
    

    on Windows without Windows Subsystem for Linux use New-Item tests/test_assertion_error.py instead of touch tests/test_assertion_error.py

    New-Item tests/test_assertion_error.py
    

    the terminal goes back to the command line

  • I click on test_assertion_error.py in the Integrated Development Environment (IDE) to open it in the editor

    Tip

    I can open a file from the terminal in Visual Studio Code by typing code and the name of the file with

    code tests/test_assertion_error.py
    

    test_assertion_error.py opens up in the editor

  • I add the first failing test to test_assertion_error.py

     1import unittest
     2
     3
     4class TestAssertionError(unittest.TestCase):
     5
     6    def test_failure(self):
     7        self.assertFalse(True)
     8
     9
    10# Exceptions Encountered
    11# AssertionError
    
  • I make a virtual environment

    python3 -m venv .venv
    

    on Windows without Windows Subsystem for Linux use python3 -m venv .venv instead of python3 -m venv .venv

    python -m venv .venv
    

    the terminal takes some time then goes back to the command line

  • I activate the virtual environment

    source .venv/bin/activate
    

    on Windows without Windows Subsystem for Linux use .venv/bin/activate.ps1 instead of source .venv/bin/activate

    .venv/scripts/activate.ps1
    

    the terminal shows

    (.venv) .../pumping_python/assertion_error
    
  • I upgrade the Python package manager (pip) to the latest version

    python3 -m pip install --upgrade pip
    

    the terminal shows pip being uninstalled then installs the latest version or shows that it is already the latest version

  • I make a requirements.txt file for the Python programs my project needs

    echo "pytest-watch" > requirements.txt
    

    the terminal goes back to the command line

  • I use pip to use the requirements file to install pytest-watch

    python3 -m pip install --requirement requirements.txt
    

    on Windows without Windows Subsystem for Linux use python -m pip install --requirement requirements.txt instead of python3 -m pip install --requirement requirements.txt

    python -m pip install --requirement requirements.txt
    

    the terminal shows pip downloads and installs the Python programs that pytest-watch needs to run

  • I run the tests

    pytest-watch
    

    the terminal shows

    ================================ FAILURES ================================
    ____________________ TestAssertionError.test_failure _____________________
    
    self = <tests.test_assertion_error.TestAssertionError testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_assertion_error.py:7: AssertionError
    ======================== short test summary info =========================
    FAILED tests/test_assertion_error.py::TestAssertionError::test_failure - AssertionError: True is not false
    =========================== 1 failed in X.YZs ============================
    
  • I hold ctrl (Windows/Linux) or option or command (MacOS) on the keyboard and use the mouse to click on tests/test_assertion_error.py:7 to open it in the editor

  • then I change True to False in test_assertion_error.py

    7        self.assertFalse(False)
    

    the test passes


test_what_is_an_assertion

We know that the result of 1 + 1 is 2, but what if I said that '1' + '1' is '11', would you agree?

I can use assertions to make the computer check if these statements are True

RED: make it fail

  • I change test_failure to test_what_is_an_assertion with the first statement

     1import unittest
     2
     3
     4class TestAssertionError(unittest.TestCase):
     5
     6    def test_what_is_an_assertion(self):
     7        1 + 1 == 2
     8
     9
    10# Exceptions Encountered
    11# AssertionError
    

    == is the symbol for is equal which makes this statement read as 1 + 1 is equal to 2. The terminal still shows green

  • I change the statement to make it False

    7        1 + 1 == 11
    

    the terminal still shows green.

  • I want the test to fail when I write a statement that is not True. I change it to an assert statement

    7        assert 1 + 1 == 11
    

    the terminal shows AssertionError

    E       assert (1 + 1) == 11
    

GREEN: make it pass

I change the statement to make it True

7        assert 1 + 1 == 2

the test passes

REFACTOR: make it better

  • I add another statement

     7        assert 1 + 1 == 2
     8        '1' + '1' == '2'
     9
    10
    11# Exceptions Encountered
    

    the test is still green

  • I add assert before the statement

    7        assert 1 + 1 == 2
    8        assert '1' + '1' == '2'
    

    the terminal shows AssertionError

    E       AssertionError: assert '11' == '2'
    E
    E         - 2
    E         + 11
    
    • - 2 shows my expectation - what I wrote as the result

    • + 11 shows reality - what the actual result is

  • I change the statement to make it True

    8        assert '1' + '1' == '11'
    

    the test passes

  • It is important to note that these 2 statements are not the same

    • 1 + 1 == 2 checks if the result of adding two numbers is equal to the number on the right side of the == symbol

    • '1' + '1' == '11' checks if the result of “adding” 2 strings is equal to the string on the right side of the == symbol. A string is any characters inside quotes, for example

      • 'single quotes'

      • '''triple single quotes'''

      • "double quotes"

      • """triple double quotes"""

    I add another statement to show this

    8        assert '1' + '1' == '11'
    9        'I am' + ' a programmer' == '11'
    

    the test is still green

  • I change the statement to an assertion

    9        assert 'I am' + ' a programmer' == '11'
    

    the terminal shows AssertionError

    E       AssertionError: assert 'I am a programmer' == '11'
    E
    E         - 11
    E         + I am a programmer
    
    • the - 11 shows my expectation - what I wrote as the result

    • the + I am a programmer shows reality - what the actual result is

  • I make the assertion True

    9        assert 'I am' + ' a programmer' == 'I am a programmer'
    

    the test passes

AssertionError happens when the statement after assert is False. It was in how to make a python test driven development environment with the first failing test

self.assertFalse(True)

which is like this assertion

assert True is False

I think of an assert statement as a command to the computer saying “DO NOT CONTINUE, UNLESS THIS STATEMENT IS TRUE”. The statement above can be thought of as “DO NOT CONTINUE, UNLESS True is False”. I expect this line to fail because True is not False. If it does not fail, then Python and I have a problem


test_assertion_error_w_none

None is used when there is no value, it is the simplest data structure in Python. I can use assertions to test if something is None.

RED: make it fail

  • I add a new failing test

     9        assert 'I am' + ' a programmer' == 'I am a programmer'
    10
    11    def test_assertion_error_w_none(self):
    12        assert None is not None
    13
    14
    15# Exceptions Encountered
    

    the terminal shows AssertionError

    E    assert None is not None
    

    the assert statement is False

GREEN: make it pass

I change the line to make it True

12        assert None is None

the test passes

REFACTOR: make it better

I can also make assertions with assert methods from the unittest.TestCase class, they have more descriptive messages

how to test if something is None

  • I add another failing line with the assertIsNotNone method which checks if the thing in parentheses (()) is NOT None

    11    def test_assertion_error_w_none(self):
    12        assert None is None
    13        self.assertIsNotNone(None)
    

    the terminal shows AssertionError with a more descriptive message

    AssertionError: unexpectedly None
    
  • I change the statement to use the assertIsNone method which checks if the thing in parentheses is None

    11    def test_assertion_error_w_none(self):
    12        assert None is None
    13        self.assertIsNone(None)
    

    the test passes. I like the messages from the assert methods better

  • I add a note

    13          self.assertIsNone(None)
    14
    15
    16# NOTES
    17# None is None
    18
    19
    20# Exceptions Encountered
    21# AssertionError
    
  • I add a new assertion to compare None with False, another simple data structure

    13        self.assertIsNone(None)
    14
    15        assert False is None
    

    the terminal shows AssertionError

    E    assert False is None
    

    I change the assert statement to make it True

    15        assert False is not None
    

    the test passes

  • I add another assertion with the assertIsNone method

    15        assert False is not None
    16        self.assertIsNone(False)
    

    the terminal shows AssertionError

    AssertionError: False is not None
    
  • I change assertIsNone to assertIsNotNone to make the assertion True

    16        self.assertIsNotNone(False)
    

    the test passes

  • I add another note

    19# NOTES
    20# False is not None
    21# None is None
    22
    23
    24# Exceptions Encountered
    25# AssertionError
    
  • I add another assertion to compare None with True, another simple data structure

    16        self.assertIsNotNone(False)
    17
    18        assert True is None
    19
    20
    21# NOTES
    

    the terminal shows AssertionError

    E    assert True is None
    
  • I change the assert statement to make it True

    18        assert True is not None
    

    the test passes

  • I add a failing line with the assertIsNone method

    18        assert True is not None
    19        self.assertIsNone(True)
    

    the terminal shows AssertionError

    AssertionError: True is not None
    
  • I make the statement True with assertIsNotNone

    18        assert True is not None
    19        self.assertIsNotNone(True)
    20
    21
    22# NOTES
    

    the test passes

  • I add a note

    22# NOTES
    23# True is not None
    24# False is not None
    25# None is None
    26
    27
    28# Exceptions Encountered
    29# AssertionError
    

I can use assertions to test if something is None


test_assertion_error_w_false

False is a simple data structures , it is one of the two boolean and is not None. I can use assertions to test if something is False or not

RED: make it fail

I add a failing test

19        self.assertIsNotNone(True)
20
21    def test_assertion_error_w_false(self):
22        assert True is False
23
24
25# NOTES

the terminal shows AssertionError

E    assert True is False

GREEN: make it pass

I change the assertion to make it True

22        assert True is not False

the test passes

REFACTOR: make it better

There is an assert method to check if something is False, it is the one from the first failing test

how to test if something is False

  • I add a failing line with the assertFalse method

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

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I change the assert statement to make it True

    23        self.assertFalse(False)
    24
    25
    26# NOTES
    

    the test passes

  • I add notes

    26# NOTES
    27# False is False
    28# True is not False
    29# True is not None
    30# False is not None
    31# None is None
    32
    33
    34# Exceptions Encountered
    35# AssertionError
    

I can use assertions to test if something is False or None


test_assertion_error_w_true

True is a simple data structures, it the other boolean and is not False or None. I can use assertions to test if something is True or not

RED: make it fail

I add a failing test

23        self.assertFalse(False)
24
25    def test_assertion_error_w_true(self):
26        assert False is True
27
28
29# NOTES

the terminal shows AssertionError

E    assert False is True

GREEN: make it pass

I change the assert statement to make it True

26        assert False is not True

the test passes

REFACTOR: make it better

There is an assert method to check if something is True

how to test if something is True

  • I add a failing assertion with the assertTrue method

    26        assert False is not True
    27        self.assertTrue(False)
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I change the assertion to make it True

    27        self.assertTrue(True)
    28
    29
    30# NOTES
    

    the test passes

  • I add more notes

    30# NOTES
    31# True is True
    32# False is not True
    33# False is False
    34# True is not False
    35# True is not None
    36# False is not None
    37# None is None
    38
    39
    40# Exceptions Encountered
    41# AssertionError
    

I can use assertions to test if something is True or False or None

All the assertions I have typed so far show that, True, False and None are different. They give me a basic expectation of Python because I can compare things with them.


test_assertion_error_w_equality

I can use assertions to test if 2 things are equal, like I did with test_what_is_an_assertion

RED: make it fail

I add a new failing test

27        self.assertTrue(True)
28
29    def test_assertion_error_w_equality(self):
30        assert None != None
31
32
33# NOTES

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 or “DO NOT CONTINUE unless None is NOT equal to None”

GREEN: make it pass

I change the assertion to make it True

30        assert None == None

the test passes.

REFACTOR: make it better

What is the difference between = and ==?

how to test if two things are Equal

There are assert methods to check if 2 things are equal or not.

  • I add assertNotEqual which checks if the 2 things in the parentheses are NOT equal

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

    the terminal shows AssertionError

    AssertionError: None == None
    
  • I change assertNotEqual to assertEqual which checks if the 2 things in the parentheses are equal

    31        self.assertEqual(None, None)
    

    the test passes

  • I add to the notes

    34# NOTES
    35# True is True
    36# False is not True
    37# False is False
    38# True is not False
    39# True is not None
    40# False is not None
    41# None is None and equal to None
    42
    43
    44# Exceptions Encountered
    45# AssertionError
    
  • I add a new failing assertion to compare False with None

    31        self.assertEqual(None, None)
    32
    33        assert False == None
    34
    35
    36# NOTES
    

    the terminal shows AssertionError

    E    assert False == None
    
  • I change the assert statement to make it True

    33        assert False != None
    

    the test passes

  • I add a failing assertion with the assertEqual method

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

    the terminal shows AssertionError

    AssertionError: False != None
    
  • I make the statement True with assertNotEqual

    34        self.assertNotEqual(False, None)
    

    the test passes

  • I add to the notes

    37# NOTES
    38# True is True
    39# False is not True
    40# False is False
    41# True is not False
    42# True is not None
    43# False is not None and not equal to None
    44# None is None and equal to None
    45
    46
    47# Exceptions Encountered
    48# AssertionError
    
  • I add the next failing assertion

    33        assert False != None
    34        self.assertNotEqual(False, None)
    35
    36        assert True == None
    37
    38# NOTES
    

    the terminal shows AssertionError

    E    assert True == None
    
  • I change the assert statement to make it True

    36        assert True != None
    

    the test passes

  • I add a failing assertion with assertEqual

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

    the terminal shows AssertionError

    AssertionError: True != None
    
  • I make the assertion True with assertNotEqual

    37        self.assertNotEqual(True, None)
    

    the test passes

  • I add a note

    40# NOTES
    41# True is True
    42# False is not True
    43# False is False
    44# True is not False
    45# True is not None and not equal to None
    46# False is not None and not equal to None
    47# None is None and equal to None
    48
    49
    50# Exceptions Encountered
    51# AssertionError
    
  • I add another failing assertion

    36        assert True != None
    37        self.assertNotEqual(True, None)
    38
    39        assert True == False
    40
    41
    42# NOTES
    

    the terminal shows AssertionError

    E    assert True == False
    
  • I change the assert statement to make it True

    39        assert True != False
    

    the test passes

  • I add a failing assertion with assertEqual

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

    the terminal shows AssertionError

    AssertionError: True != False
    
  • I change the assert method to assertNotEqual

    40        self.assertNotEqual(True, False)
    

    the test passes

  • I add a note

    43# NOTES
    44# True is True
    45# False is not True
    46# False is False
    47# True is not False and not equal to False
    48# True is not None and not equal to None
    49# False is not None and not equal to None
    50# None is None and equal to None
    51
    52
    53# Exceptions Encountered
    54# AssertionError
    
  • on to the next failing assertion

    40        self.assertNotEqual(True, False)
    41
    42        assert False != False
    43
    44
    45# NOTES
    

    the terminal shows AssertionError

    E    assert False != False
    
  • I make the assertion True

    42        assert False == False
    

    the test passes

  • I add another failing assert statement with assertNotEqual

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

    the terminal shows AssertionError

    AssertionError: False == False
    
  • I change assertNotEqual to assertEqual

    43        self.assertEqual(False, False)
    

    the test passes

  • I add a note

    46# NOTES
    47# True is True
    48# False is not True
    49# False is False and equal to False
    50# True is not False and not equal to False
    51# True is not None and not equal to None
    52# False is not None and not equal to None
    53# None is None and equal to None
    54
    55
    56# Exceptions Encountered
    57# AssertionError
    
  • I add a failing assert statement

    43        self.assertEqual(False, False)
    44
    45        assert False == True
    46
    47
    48# NOTES
    

    the terminal shows AssertionError

    E    assert False == True
    
  • I change the assertion to make it True

    45        assert False != True
    

    the test passes

  • I add another failing line with the assertEqual method

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

    the terminal shows AssertionError

    AssertionError: False != True
    
  • I make the statement True with assertNotEqual

    46        self.assertNotEqual(False, True)
    

    the test passes

  • I add another note

    49# NOTES
    50# True is True
    51# False is not True and not equal to True
    52# False is False and equal to False
    53# True is not False and not equal to False
    54# True is not None and not equal to None
    55# False is not None and not equal to None
    56# None is None and equal to None
    57
    58
    59# Exceptions Encountered
    60# AssertionError
    
  • time for the last assert statements

    46        self.assertNotEqual(False, True)
    47
    48        assert True != True
    49
    50
    51# NOTES
    

    the terminal shows AssertionError

    E    assert True != True
    
  • I make the assertion True

    48          assert True == True
    

    the test passes

  • I add a failing line with the assertNotEqual method

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

    the terminal shows AssertionError

    AssertionError: True == True
    
  • I change the assertNotEqual method to the assertEqual method

    29    def test_assertion_error_w_equality(self):
    30        assert None == None
    31        self.assertEqual(None, None)
    32
    33        assert False != None
    34        self.assertNotEqual(False, None)
    35
    36        assert True != None
    37        self.assertNotEqual(True, None)
    38
    39        assert True != False
    40        self.assertNotEqual(True, False)
    41
    42        assert False == False
    43        self.assertEqual(False, False)
    44
    45        assert False != True
    46        self.assertNotEqual(False, True)
    47
    48        assert True == True
    49        self.assertEqual(True, True)
    50
    51
    52# NOTES
    

    the test passes

  • I add a note for the last statement

    52# NOTES
    53# True is True and equal to True
    54# False is not True and not equal to True
    55# False is False and equal to False
    56# True is not False and not equal to False
    57# True is not None and not equal to None
    58# False is not None and not equal to None
    59# None is None and equal to None
    60
    61
    62# Exceptions Encountered
    63# AssertionError
    
  • I add calls to the assertEqual method in test_what_is_an_assertion

     4class TestAssertionError(unittest.TestCase):
     5
     6    def test_what_is_an_assertion(self):
     7        assert 1 + 1 == 2
     8        self.assertEqual(1+1, 3)
     9
    10        assert '1' + '1' == '11'
    11        assert 'I am' + ' a programmer' == 'I am a programmer'
    12
    13    def test_assertion_error_w_none(self):
    

    the terminal shows AssertionError

    AssertionError: 2 != 3
    
  • I change the expectation in the call to the assertEqual method to make the assertion True

    8        self.assertEqual(1+1, 2)
    

    the test passes

  • I add assertEqual for the next assertion

     8        self.assertEqual(1+1, 2)
     9
    10        assert '1' + '1' == '11'
    11        self.assertEqual('1'+'1', '2')
    12
    13        assert 'I am' + ' a programmer' == 'I am a programmer'
    

    the terminal shows

    AssertionError: '11' != '2'
    
  • I change the expectation to match reality

    11        self.assertEqual('1'+'1', '11')
    

    the test passes

  • I add assertEqual for the last assertion in test_what_is_an_assertion

    11        self.assertEqual('1'+'1', '11')
    12
    13        assert 'I am' + ' a programmer' == 'I am a programmer'
    14        self.assertEqual('I am'+' a programmer', '11')
    15
    16    def test_assertion_error_w_none(self):
    

    the terminal shows AssertionError

    AssertionError: 'I am a programmer' != '11'
    
  • I make the expectation match reality

    14        self.assertEqual('I am'+' a programmer', 'I am a programmer')
    

    the test passes

I can use assertions to test if 2 things are equal


close the project

  • I close the file(s) I had open in the editor(s)

  • I exit the tests in the terminal with Ctrl+C on the keyboard

  • I deactivate the virtual environment

    deactivate
    

    the terminal goes back to the command line, (.venv) is no longer on the left side

    .../pumping_python/assertion_error
    
  • I change directory to the parent of assertion_error

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory


review

I can use assert statements and assert methods to test if something is

and to test if 2 things are

for a total of 6 assert methods I can use when testing

How many questions can you answer about AssertionError?


code from the chapter

:ref:`Do you want to see all the CODE I typed in this chapter?<AssertionError: tests>


what is next?

Congratulations! You now know

Would you like to test functions?


please leave a review