what is an assertion?


An assertion or assert statement is a way for me to tell Python, “DO NOT CONTINUE, if this statement is False”, or said a different way “GO TO THE NEXT LINE, ONLY if this statement is True”.

I use assertions in tests when making a program to make sure something is True about the program before I continue building. I use them to test ideas, without worrying about if I will remember the ideas later. I use them to test how programs behave, for example when given inputs.

Assertions can help catch things that make tests that were passing, start failing when I add new lines of code. They 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

I have these tests by the end of the chapter

  1def test_assert_keyword():
  2    assert 1 + 1 == 2
  3
  4    assert '1' + '1' == '11'
  5
  6    assert 'I am' + ' alive' == 'I am alive'
  7
  8
  9def test_assertion_error_w_none():
 10    assert None is None
 11
 12    assert False is not None
 13
 14    assert True is not None
 15
 16    assert 0 is not None
 17
 18    assert 0.0 is not None
 19
 20    assert '' is not None
 21
 22    assert () is not None
 23
 24    assert [] is not None
 25
 26    assert set() is not None
 27
 28    assert {} is not None
 29
 30
 31def test_assertion_error_w_false():
 32    assert None is not False
 33
 34    assert False is False
 35
 36    assert True is not False
 37
 38    assert 0 is not False
 39
 40    assert 0.0 is not False
 41
 42    assert '' is not False
 43
 44    assert () is not False
 45
 46    assert [] is not False
 47
 48    assert set() is not False
 49
 50    assert {} is not False
 51
 52
 53def test_assertion_error_w_true():
 54    assert None is not True
 55
 56    assert False is not True
 57
 58    assert True is True
 59
 60    assert 0 is not True
 61
 62    assert 0.0 is not True
 63
 64    assert '' is not True
 65
 66    assert () is not True
 67
 68    assert [] is not True
 69
 70    assert set() is not True
 71
 72    assert {} is not True
 73
 74
 75def test_assertion_error_w_equality():
 76    assert None == None
 77
 78    assert False != None
 79
 80    assert False != True
 81
 82    assert False == False
 83
 84    assert True != None
 85
 86    assert True == True
 87
 88
 89def test_assertion_error_w_is_vs_equal():
 90    assert 0 is not 0.0
 91
 92    assert 0 == 0.0
 93
 94
 95def will_not_run():
 96    assert False == True
 97
 98
 99def test_failure():
100    # assert False == True
101    assert False == False
102
103
104# NOTES
105# a dictionary is not True
106# a dictionary is not False
107# a dictionary is not None
108# a set is not True
109# a set is not False
110# a set is not None
111# a list is not True
112# a list is not False
113# a list is not None
114# a tuple is not True
115# a tuple is not False
116# a tuple is not None
117# a string is not True
118# a string is not False
119# a string is not None
120# a float is not True
121# a float is not False
122# a float is not None
123# an integer is not True
124# an integer is not False
125# an integer is not None
126# True is True and equal to True
127# True is not False and NOT equal to False
128# True is not None and NOT equal to None
129# False is not True and NOT equal to True
130# False is False and equal to False
131# False is not None and NOT equal to None
132# None is not True and NOT equal to True
133# None is not False and NOT equal to False
134# None is None and equal to None
135
136
137# Exceptions seen
138# AssertionError

questions about AssertionError

Questions to think about as I go through the chapter


start the project

  • I name this project assertion_error

  • I open a terminal

  • I change directory to the assertion_error folder in the pumping_python folder

    cd assertion_error
    

    the terminal shows

    cd: no such file or directory: assertion_error
    

    there is no folder with the name assertion_error in this folder.

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

    uv init assertion_error
    

    the terminal shows

    Initialized project `assertion-error`
    at `.../pumping_python/assertion_error`
    

    then goes back to the command line.

  • I change directory to the project

    cd assertion_error
    

    the terminal shows I am in the assertion_error folder

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

    mkdir tests
    

    the terminal goes back to the command line.

  • 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
    

    the terminal goes back to the command line.

  • I use the mv program to change the name of main.py to test_assertion_error.py and move it to the tests folder

    mv main.py tests/test_assertion_error.py
    
    Move-Item main.py tests/test_assertion_error.py
    

    the terminal goes back to the command line.

  • I open test_assertion_error.py

  • I delete the text in the file then add the first failing test to test_assertion_error.py

    1assert False is True
    
  • I go back to the terminal to make a requirements file for the Python packages I need

    echo "pytest" > requirements.txt
    

    the terminal goes back to the command line.

  • I add pytest-watcher to the requirements file

    echo "pytest-watcher" >> requirements.txt
    

    the terminal goes back to the command line.

  • I use uv to install pytest-watcher with the requirements file

    uv add --requirement requirements.txt
    

    the terminal shows that it installed pytest-watcher and its dependencies.

  • I add the new files and folder to git for tracking

    git add .
    

    the terminal goes back to the command line.

  • I add a git commit message

    git commit --all --message 'setup project'
    

    the terminal shows

    [main (root-commit) a0b12c3] setup project
     8 files changed, X insertions(+)
     create mode 100644 .gitignore
     create mode 100644 .python-version
     create mode 100644 README.md
     create mode 100644 pyproject.toml
     create mode 100644 requirements.txt
     create mode 100644 tests/__init__.py
     create mode 100644 tests/test_assertion_error.py
     create mode 100644 uv.lock
    

    then goes back to the command line.

  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal is my friend, and shows AssertionError

    ========================== ERRORS ==========================
    ______ ERROR collecting tests/test_assertion_error.py ______
    tests/test_assertion_error.py:2: in <module>
        assert False is True
    E   assert False is True
    ================= short test summary info ==================
    ERROR tests/test_assertion_error.py - assert False is True
    !!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!
    ===================== 1 error in I.JKs =====================
    

    because False is NOT True.

    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_assertion_error.py

    1assert False is True
    2
    3
    4# Exceptions seen
    5# AssertionError
    
  • I change True to False in the assertion

    1# assert False is True
    2assert False is False
    3
    4
    5# Exceptions seen
    6# AssertionError
    

    the test passes.


the assert keyword

We know that the result of 1 + 1 is 2. What if I said that the result of '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 remove the first failing assertion then add the first statement

    11 + 1 == 2
    2
    3
    4# Exceptions seen
    5# AssertionError
    
    • the test is still passing

    • == is two equal signs on the keyboard (=+=) and means is equal which makes this statement read as 1 + 1 is equal to 2

  • I change the result of the equation

    1# 1 + 1 == 2
    21 + 1 == 11
    3
    4
    5# Exceptions seen
    6# AssertionError
    

    the test is still passing. Why is it still passing? 1 + 1 cannot be both 2 and 11, is Python broken?

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

    1# 1 + 1 == 2
    2# 1 + 1 == 11
    3assert 1 + 1 == 11
    4
    5
    6# Exceptions seen
    7# AssertionError
    

    the terminal is my friend, and shows

    E   assert (1 + 1) == 11
    

    because Python does not care if the statement is False or True as long as it follows the rules. Once I make it an assertion with the assert keyword it tells the computer DO NOT CONTINUE, if "1 + 1 == 11" is False


GREEN: make it pass


I change the result back to make the statement True

1# 1 + 1 == 2
2# 1 + 1 == 11
3# assert 1 + 1 == 11
4assert 1 + 1 == 2
5
6
7# Exceptions seen

the test passes.


REFACTOR: make it better


  • I add the other statement ('1' + '1' == '11')

    1# 1 + 1 == 2
    2# 1 + 1 == 11
    3# assert 1 + 1 == 11
    4assert 1 + 1 == 2
    5
    6'1' + '1' == '2'
    7
    8
    9# Exceptions seen
    

    the terminal still shows no tests ran because the statement is not an assertion.

  • I add assert before the statement to make it an assertion

     1# 1 + 1 == 2
     2# 1 + 1 == 11
     3# assert 1 + 1 == 11
     4assert 1 + 1 == 2
     5
     6# '1' + '1' == '2'
     7assert '1' + '1' == '2'
     8
     9
    10# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E   AssertionError: assert ('1' + '1') == '2'
    

    because the assert before the statement makes it a command to Python - DO NOT CONTINUE if "'1' + '1' == '2'" is False


what causes AssertionError?

AssertionError is raised if the statement after the assert keyword is False. It was in the first failing assertion

assert False is True

With this statement, I tell Python - “DO NOT CONTINUE, if False is the same object as True”, or said a different way “GO TO THE NEXT LINE, ONLY if False is the same object as True”.

  • I change the statement to make it True

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

    the test passes because the statement is now True, '1' + '1' is equal to '11'

  • These two 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 equality symbol (==).

    • '1' + '1' == '11' checks if the result of “adding” 2 strings is equal to the string on the right side of the equality symbol (==). A string is anything inside quotes.

    I add another statement to show how '1' + '1' == '11'

     6# '1' + '1' == '2'
     7# assert '1' + '1' == '2'
     8assert '1' + '1' == '11'
     9
    10'I am' + ' alive' == '11'
    11
    12
    13# Exceptions seen
    

    the test is still green because 'I am' + ' alive' == '11' is just a statement. Python does not care whether it is False or True and the statement follows Python rules.

  • I change the statement to an assertion with the assert keyword

     6# '1' + '1' == '2'
     7# assert '1' + '1' == '2'
     8assert '1' + '1' == '11'
     9
    10# 'I am' + ' alive' == '11'
    11assert 'I am' + ' alive' == '11'
    12
    13
    14# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E   AssertionError: assert ('I am' + ' alive') == '11'
    

    because the assert before the statement makes it a command to Python - DO NOT CONTINUE if "'I am' + ' alive' == '11'" is False

  • I change the statement to make it True

    10# 'I am' + ' alive' == '11'
    11# assert 'I am' + ' alive' == '11'
    12assert 'I am' + ' alive' == 'I am alive'
    13
    14
    15# Exceptions seen
    
  • I remove the commented lines

    1assert 1 + 1 == 2
    2
    3assert '1' + '1' == '11'
    4
    5assert 'I am' + ' alive' == 'I am alive'
    6
    7
    8# Exceptions seen
    9# AssertionError
    
  • I open a new terminal then change directories to assertion_error

    cd assertion_error
    

    the terminal shows I am in the assertion_error folder

    .../pumping_python/assertion_error
    
  • I add a git commit message

    git commit --all --message \
    'test the assert keyword'
    

    the terminal shows a summary of the changes then goes back to the command line.


test AssertionError with None

None is used when there is no value. I can use assertions to test if something is the same object as None, this is useful if I want to check what value I get from a process.

For example, if I have people fill a form and I want a test for when they leave something blank, I can use an assertion to make sure that the value is None.


RED: make it fail


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

  • I add a new statement to test_assertion_error.py

    5assert 'I am' + ' alive' == 'I am alive'
    6
    7None is not None
    8
    9# Exceptions seen
    

    the test is still passing

  • I add the assert keyword

     5assert 'I am' + ' alive' == 'I am alive'
     6
     7# None is not None
     8assert None is not None
     9
    10# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert None is not None
    

    because None is not None is False.


GREEN: make it pass


I change the statement to make it True

 7  # None is not None
 8  # assert None is not None
 9  assert None is None
10
11  # Exceptions seen

the test passes.


REFACTOR: make it better


  • I add a note with what I learned from the experiment

     7# None is not None
     8# assert None is not None
     9assert None is None
    10
    11
    12# NOTES
    13# None is None
    14
    15# Exceptions seen
    16# AssertionError
    

  • I add a new assertion to compare None with False.

     7# None is not None
     8# assert None is not None
     9assert None is None
    10
    11assert False is None
    12
    13
    14# NOTES
    

    the terminal shows

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

     7# None is not None
     8# assert None is not None
     9assert None is None
    10
    11# assert False is None
    12assert False is not None
    13
    14
    15# NOTES
    

    the test passes.

  • I add a note about False

    11# assert False is None
    12assert False is not None
    13
    14
    15# NOTES
    16# False is not None
    17# None is None
    18
    19# Exceptions seen
    

  • I add an assertion to compare None with True.

    11# assert False is None
    12assert False is not None
    13
    14assert True is None
    15
    16
    17# NOTES
    

    the terminal shows

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

    11# assert False is None
    12assert False is not None
    13
    14# assert True is None
    15assert True is not None
    16
    17
    18# NOTES
    

    the test passes.

  • I add a note about True

    18# NOTES
    19# True is not None
    20# False is not None
    21# None is None
    22
    23# Exceptions seen
    24# AssertionError
    

  • I add an assert statement for an integer (a whole number without decimals)

    14# assert True is None
    15assert True is not None
    16
    17assert 0 is None
    18
    19
    20# NOTES
    

    the terminal shows

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

    14# assert True is None
    15assert True is not None
    16
    17# assert 0 is None
    18assert 0 is not None
    19
    20
    21# NOTES
    

    the test passes.

  • I add a note about integers

    21# NOTES
    22# an integer is not None
    23# True is not None
    24# False is not None
    25# None is None
    26
    27
    28# Exceptions seen
    29# AssertionError
    

  • I add an assert statement for a float (binary floating point decimal number)

    17# assert 0 is None
    18assert 0 is not None
    19
    20assert 0.0 is None
    21
    22
    23# NOTES
    

    the terminal shows

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

    17# assert 0 is None
    18assert 0 is not None
    19
    20# assert 0.0 is None
    21assert 0.0 is not None
    22
    23
    24# NOTES
    

    the test passes.

  • I add a note about floats

    24# NOTES
    25# a float is not None
    26# an integer is not None
    27# True is not None
    28# False is not None
    29# None is None
    30
    31
    32# Exceptions seen
    33# AssertionError
    

  • I add an assert statement for a string (anything in quotes)

    20# assert 0.0 is None
    21assert 0.0 is not None
    22
    23assert '' is None
    24
    25
    26# NOTES
    

    the terminal is my friend, and shows AssertionError

    E   AssertionError: assert '' is None
    

    because a string is not the same object as None.

  • I change the statement to make it True

    20# assert 0.0 is None
    21assert 0.0 is not None
    22
    23# assert '' is None
    24assert '' is not None
    25
    26
    27# NOTES
    

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

  • I add a note about strings

    27# NOTES
    28# a string is not None
    29# a float is not None
    30# an integer is not None
    31# True is not None
    32# False is not None
    33# None is None
    34
    35
    36# Exceptions seen
    37# AssertionError
    

  • I add an assert statement for a tuple (anything in parentheses ( ) separated by a comma)

    26# assert '' is None
    27assert '' is not None
    28
    29assert () is None
    30
    31
    32# NOTES
    

    the terminal shows

    E   assert {} is None
    

    because a tuple is not the same object None.

  • I change the statement to make it True

    26# assert '' is None
    27assert '' is not None
    28
    29# assert () is None
    30assert () is not None
    31
    32
    33# NOTES
    

    the test passes because a tuple is not the same object None.

  • I add a note about tuples

    30# NOTES
    31# a tuple is not None
    32# a string is not None
    33# a float is not None
    34# an integer is not None
    35# True is not None
    36# False is not None
    37# None is None
    38
    39
    40# Exceptions seen
    41# AssertionError
    

  • I add an assert statement for a list (anything in square brackets [ ])

    26# assert () is None
    27assert () is not None
    28
    29assert [] is None
    30
    31
    32# NOTES
    

    the terminal shows

    E   assert [] is None
    

    because a list is not the same object as None.

  • I change the statement to make it True

    26# assert () is None
    27assert () is not None
    28
    29# assert [] is None
    30assert [] is not None
    31
    32
    33# NOTES
    

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

  • I add a note about lists

    33# NOTES
    34# a list is not None
    35# a tuple is not None
    36# a string is not None
    37# a float is not None
    38# an integer is not None
    39# True is not None
    40# False is not None
    41# None is None
    42
    43
    44# Exceptions seen
    45# AssertionError
    

  • I add an assert statement for a set

    29# assert [] is None
    30assert [] is not None
    31
    32assert set() is None
    33
    34
    35# NOTES
    

    the terminal is my friend, and shows AssertionError

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

    29# assert [] is None
    30assert [] is not None
    31
    32# assert set() is None
    33assert set() is not None
    34
    35
    36# NOTES
    

    the test passes.

  • I add a note about sets

    36# NOTES
    37# a set is not None
    38# a list is not None
    39# a tuple is not None
    40# a string is not None
    41# a float is not None
    42# an integer is not None
    43# True is not None
    44# False is not None
    45# None is None
    46
    47
    48# Exceptions seen
    49# AssertionError
    

  • I add an assert statement for a dictionary

    32# assert set() is None
    33assert set() is not None
    34
    35assert {} is None
    36
    37
    38# NOTES
    

    the terminal shows

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

    32# assert set() is None
    33assert set() is not None
    34
    35# assert {} is None
    36assert {} is not None
    37
    38
    39# NOTES
    

    the test passes.

  • I add a note about dictionaries

    51# NOTES
    52# a dictionary is not None
    53# a set is not None
    54# a list is not None
    55# a tuple is not None
    56# a string is not None
    57# a float is not None
    58# an integer is not None
    59# True is not None
    60# False is not None
    61# None is None
    62
    63
    64# Exceptions seen
    65# AssertionError
    
  • I remove the commented lines

     5assert 'I am' + ' alive' == 'I am alive'
     6
     7assert None is None
     8
     9assert False is not None
    10
    11assert True is not None
    12
    13assert 0 is not None
    14
    15assert 0.0 is not None
    16
    17assert '' is not None
    18
    19assert () is not None
    20
    21assert [] is not None
    22
    23assert set() is not None
    24
    25assert {} is not None
    26
    27
    28# NOTES
    
  • I add comments to group the assertions

     1# test assert keyword
     2assert 1 + 1 == 2
     3
     4assert '1' + '1' == '11'
     5
     6assert 'I am' + ' alive' == 'I am alive'
     7
     8
     9# test AssertionError with None
    10assert None is None
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test AssertionError with None'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can use assertions to test if something is None.


test AssertionError with False

False is one of the two booleans and assert False is not None shows that False is NOT None. is None False?

I can use assertions to test if something is the same object as False or not.


RED: make it fail


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

  • I add a new statement to test_assertion_error.py with a comment to group these new statements

    28assert {} is not None
    29
    30
    31# test AssertionError with False
    32None is False
    33
    34
    35# NOTES
    

    the test is still passing.

  • I add the assert keyword

    31# test AssertionError with False
    32# None is False
    33assert None is False
    34
    35
    36# NOTES
    

    the terminal is my friend, and shows

    E       assert None is False
    

    because None is not the same object as False.


GREEN: make it pass


I change the assertion to make it True

31  # test AssertionError with False
32  # None is False
33  # assert None is False
34  assert None is not False
35
36
37  # NOTES

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


REFACTOR: make it better


  • I add a note about None

    37# NOTES
    38# a dictionary is not None
    39# a set is not None
    40# a list is not None
    41# a tuple is not None
    42# a string is not None
    43# a float is not None
    44# an integer is not None
    45# True is not None
    46# False is not None
    47# None is not False
    48# None is None
    

  • I add a failing assert statement about False

    31# test AssertionError with False
    32# None is False
    33# assert None is False
    34assert None is not False
    35
    36assert False is not False
    37
    38
    39# NOTES
    

    the terminal shows

    E       assert False is not False
    

    because False is False.

  • I change the statement to make it True

    31# test AssertionError with False
    32# None is False
    33# assert None is False
    34assert None is not False
    35
    36# assert False is not False
    37assert False is False
    38
    39
    40# NOTES
    

    the test passes because False is False.

  • I add a note about False

    40# NOTES
    41# a dictionary is not None
    42# a set is not None
    43# a list is not None
    44# a tuple is not None
    45# a string is not None
    46# a float is not None
    47# an integer is not None
    48# True is not None
    49# False is False
    50# False is not None
    51# None is not False
    52# None is None
    

  • I add an assert statement to see if True is the same object as False

    36# assert False is not False
    37assert False is False
    38
    39assert True is False
    40
    41
    42# NOTES
    

    the terminal

    E       assert True is False
    

    because True is not the same object as False.

  • I change the statement to make it True

    36# assert False is not False
    37assert False is False
    38
    39# assert True is False
    40assert True is not False
    41
    42
    43# NOTES
    

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

  • I add a note about True

    43# NOTES
    44# a dictionary is not None
    45# a set is not None
    46# a list is not None
    47# a tuple is not None
    48# a string is not None
    49# a float is not None
    50# an integer is not None
    51# True is not False
    52# True is not None
    53# False is False
    54# False is not None
    55# None is not False
    56# None is None
    

  • I add an assert statement to see if an integer (a whole number without decimals) is the same object as False

    39# assert True is False
    40assert True is not False
    41
    42assert 0 is False
    43
    44
    45# NOTES
    

    the terminal shows

    E       assert 0 is False
    

    because an integer is not the same object as False.

  • I change the statement to make it True

    39# assert True is False
    40assert True is not False
    41
    42# assert 0 is False
    43assert 0 is not False
    44
    45
    46# NOTES
    

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

  • I add a note about integers

    46# NOTES
    47# a dictionary is not None
    48# a set is not None
    49# a list is not None
    50# a tuple is not None
    51# a string is not None
    52# a float is not None
    53# an integer is not False
    54# an integer is not None
    55# True is not False
    56# True is not None
    57# False is False
    58# False is not None
    59# None is not False
    60# None is None
    

  • I add an assert statement to see if a float (binary floating point decimal number) is the same object as False

    42# assert 0 is False
    43assert 0 is not False
    44
    45assert 0.0 is False
    46
    47
    48# NOTES
    

    the terminal shows

    E       assert 0.0 is False
    

    because a float is not the same object as False.

  • I change the statement to make it True

    42# assert 0 is False
    43assert 0 is not False
    44
    45# assert 0.0 is False
    46assert 0.0 is not False
    47
    48
    49# NOTES
    

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

  • I add a note about floats

    49# NOTES
    50# a dictionary is not None
    51# a set is not None
    52# a list is not None
    53# a tuple is not None
    54# a string is not None
    55# a float is not False
    56# a float is not None
    57# an integer is not False
    58# an integer is not None
    59# True is not False
    60# True is not None
    61# False is False
    62# False is not None
    63# None is not False
    64# None is None
    

  • I add an assert statement to see if a string (anything in quotes) is the same object as False

    45# assert 0.0 is False
    46assert 0.0 is not False
    47
    48assert '' is False
    49
    50
    51# NOTES
    

    the terminal shows AssertionError

    E       AssertionError: assert '' is False
    

    because a string is not the same object as False.

  • I change the statement to make it True

    45# assert 0.0 is False
    46assert 0.0 is not False
    47
    48# assert '' is False
    49assert '' is not False
    50
    51
    52# NOTES
    

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

  • I add a note about strings

    52# NOTES
    53# a dictionary is not None
    54# a set is not None
    55# a list is not None
    56# a tuple is not None
    57# a string is not False
    58# a string is not None
    59# a float is not False
    60# a float is not None
    61# an integer is not False
    62# an integer is not None
    63# True is not False
    64# True is not None
    65# False is False
    66# False is not None
    67# None is not False
    68# None is None
    

  • I add an assert statement to see if a tuple (anything in parentheses ( ) separated by a comma) is the same object as False

    48# assert '' is False
    49assert '' is not False
    50
    51assert () is False
    52
    53
    54# NOTES
    

    the terminal shows

    E   assert () is False
    

    because a tuple is not the same object as False.

  • I change the statement to make it True

    48# assert '' is False
    49assert '' is not False
    50
    51# assert () is False
    52assert () is not False
    53
    54
    55# NOTES
    

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

  • I add a note about tuples

    55# NOTES
    56# a dictionary is not None
    57# a set is not None
    58# a list is not None
    59# a tuple is not False
    60# a tuple is not None
    61# a string is not False
    62# a string is not None
    63# a float is not False
    64# a float is not None
    65# an integer is not False
    66# an integer is not None
    67# True is not False
    68# True is not None
    69# False is False
    70# False is not None
    71# None is not False
    72# None is None
    

  • I add an assert statement to see if a list (anything in square brackets [ ]) is the same object as False

    51# assert () is False
    52assert () is not False
    53
    54assert [] is False
    55
    56
    57# NOTES
    

    the terminal shows

    E   assert [] is False
    

    because a list is not the same object as False.

  • I change the statement to make it True

    51# assert () is False
    52assert () is not False
    53
    54# assert [] is False
    55assert [] is not False
    56
    57
    58# NOTES
    

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

  • I add a note about lists

    58# NOTES
    59# a dictionary is not None
    60# a set is not None
    61# a list is not False
    62# a list is not None
    63# a tuple is not False
    64# a tuple is not None
    65# a string is not False
    66# a string is not None
    67# a float is not False
    68# a float is not None
    69# an integer is not False
    70# an integer is not None
    71# True is not False
    72# True is not None
    73# False is False
    74# False is not None
    75# None is not False
    76# None is None
    

  • I add an assert statement to see if a set is the same object as False

    54# assert [] is False
    55assert [] is not False
    56
    57assert set() is False
    58
    59
    60# NOTES
    

    the terminal shows

    E   assert set() is False
    

    because a set is not the same object as False.

  • I change the statement to make it True

    54# assert [] is False
    55assert [] is not False
    56
    57# assert set() is False
    58assert set() is not False
    59
    60
    61# NOTES
    

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

  • I add a note about sets

    61# NOTES
    62# a dictionary is not None
    63# a set is not False
    64# a set is not None
    65# a list is not False
    66# a list is not None
    67# a tuple is not False
    68# a tuple is not None
    69# a string is not False
    70# a string is not None
    71# a float is not False
    72# a float is not None
    73# an integer is not False
    74# an integer is not None
    75# True is not False
    76# True is not None
    77# False is False
    78# False is not None
    79# None is not False
    80# None is None
    

  • I add an assert statement to see if a dictionary is the same object as False

    57# assert set() is False
    58assert set() is not False
    59
    60assert {} is False
    61
    62
    63# NOTES
    

    the terminal shows

    E   assert {} is False
    

    because a dictionary is not the same object as False.

  • I change the statement to make it True

    51# assert set() is False
    52assert set() is not False
    53
    54# assert {} is False
    55assert {} is not False
    56
    57
    58# NOTES
    

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

  • I add a note about dictionaries

    64# NOTES
    65# a dictionary is not False
    66# a dictionary is not None
    67# a set is not False
    68# a set is not None
    69# a list is not False
    70# a list is not None
    71# a tuple is not False
    72# a tuple is not None
    73# a string is not False
    74# a string is not None
    75# a float is not False
    76# a float is not None
    77# an integer is not False
    78# an integer is not None
    79# True is not False
    80# True is not None
    81# False is False
    82# False is not None
    83# None is not False
    84# None is None
    85
    86
    87# Exceptions seen
    88# AssertionError
    
  • I remove the commented assertions

    31# test AssertionError with False
    32assert None is not False
    33
    34assert False is False
    35
    36assert True is not False
    37
    38assert 0 is not False
    39
    40assert 0.0 is not False
    41
    42assert '' is not False
    43
    44assert () is not False
    45
    46assert [] is not False
    47
    48assert set() is not False
    49
    50assert {} is not False
    51
    52
    53# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test AssertionError with False'
    

    the terminal shows a summary of the changes then goes back to the command line.


test AssertionError with True

True is the other boolean and is NOT None. I can use assertions to test if something is the same object as True or not.


RED: make it fail


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

  • I add a new statement to test_assertion_error.py with a comment to group these new statements

    50assert {} is not False
    51
    52
    53# test AssertionError with True
    54None is True
    55
    56
    57# NOTES
    

    the test is still passing.

  • I add the assert keyword

    53# test AssertionError with True
    54# None is True
    55assert None is True
    56
    57
    58# NOTES
    

    the terminal is my friend, and shows

    E       assert None is True
    

    because None is not the same object as True.


GREEN: make it pass


I change the statement to make it True

53# test AssertionError with True
54# None is True
55# assert None is True
56assert None is not True
57
58
59# NOTES

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


REFACTOR: make it better


  • I add a note about None

    59# NOTES
    60# a dictionary is not False
    61# a dictionary is not None
    62# a set is not False
    63# a set is not None
    64# a list is not False
    65# a list is not None
    66# a tuple is not False
    67# a tuple is not None
    68# a string is not False
    69# a string is not None
    70# a float is not False
    71# a float is not None
    72# an integer is not False
    73# an integer is not None
    74# True is not False
    75# True is not None
    76# False is False
    77# False is not None
    78# None is not True
    79# None is not False
    80# None is None
    

  • I add an assert statement to see if False is the same object as True

    53# test AssertionError with True
    54# None is True
    55# assert None is True
    56assert None is not True
    57
    58assert False is True
    59
    60
    61# NOTES
    

    the terminal shows

    E       assert False is True
    

    because False is not the same object as True.

  • I change the statement to make it True

    53# test AssertionError with True
    54# None is True
    55# assert None is True
    56assert None is not True
    57
    58# assert False is True
    59assert False is not True
    60
    61
    62# NOTES
    

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

  • I add a note about False

    62# NOTES
    63# a dictionary is not False
    64# a dictionary is not None
    65# a set is not False
    66# a set is not None
    67# a list is not False
    68# a list is not None
    69# a tuple is not False
    70# a tuple is not None
    71# a string is not False
    72# a string is not None
    73# a float is not False
    74# a float is not None
    75# an integer is not False
    76# an integer is not None
    77# True is not False
    78# True is not None
    79# False is not True
    80# False is False
    81# False is not None
    82# None is not True
    83# None is not False
    84# None is None
    

  • I add an assert statement about True, that will fail

    58# assert False is True
    59assert False is not True
    60
    61assert True is not True
    62
    63
    64# NOTES
    

    the terminal shows

    E       assert True is not True
    

    because True is True.

    58# assert False is True
    59assert False is not True
    60
    61# assert True is not True
    62assert True is True
    63
    64
    65# NOTES
    

    the test passes because True is True.

  • I add a note about True

    65# NOTES
    66# a dictionary is not False
    67# a dictionary is not None
    68# a set is not False
    69# a set is not None
    70# a list is not False
    71# a list is not None
    72# a tuple is not False
    73# a tuple is not None
    74# a string is not False
    75# a string is not None
    76# a float is not False
    77# a float is not None
    78# an integer is not False
    79# an integer is not None
    80# True is True
    81# True is not False
    82# True is not None
    83# False is not True
    84# False is False
    85# False is not None
    86# None is not True
    87# None is not False
    88# None is None
    

  • I add an assert statement to see if an integer (a whole number without decimals) is the same object as True

    61# assert True is not True
    62assert True is True
    63
    64assert 0 is True
    65
    66
    67# NOTES
    

    the terminal shows

    E       assert 0 is True
    

    because an integer is not the same object as True.

  • I change the statement to make it True

    61# assert True is not True
    62assert True is True
    63
    64# assert 0 is True
    65assert 0 is not True
    66
    67
    68# NOTES
    

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

  • I add a note about integers

    68# NOTES
    69# a dictionary is not False
    70# a dictionary is not None
    71# a set is not False
    72# a set is not None
    73# a list is not False
    74# a list is not None
    75# a tuple is not False
    76# a tuple is not None
    77# a string is not False
    78# a string is not None
    79# a float is not False
    80# a float is not None
    81# an integer is not True
    82# an integer is not False
    83# an integer is not None
    84# True is True
    85# True is not False
    86# True is not None
    87# False is not True
    88# False is False
    89# False is not None
    90# None is not True
    91# None is not False
    92# None is None
    

  • I add an assert statement to see if a float (binary floating point decimal number) is the same object as True

    64# assert 0 is True
    65assert 0 is not True
    66
    67assert 0.0 is True
    68
    69
    70# NOTES
    

    the terminal shows

    E       assert 0.0 is True
    

    because a float is not the same object as True.

  • I change the statement to make it True

    64# assert 0 is True
    65assert 0 is not True
    66
    67# assert 0.0 is True
    68assert 0.0 is not True
    69
    70
    71# NOTES
    

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

  • I add a note about floats

    71# NOTES
    72# a dictionary is not False
    73# a dictionary is not None
    74# a set is not False
    75# a set is not None
    76# a list is not False
    77# a list is not None
    78# a tuple is not False
    79# a tuple is not None
    80# a string is not False
    81# a string is not None
    82# a float is not True
    83# a float is not False
    84# a float is not None
    85# an integer is not True
    86# an integer is not False
    87# an integer is not None
    88# True is True
    89# True is not False
    90# True is not None
    91# False is not True
    92# False is False
    93# False is not None
    94# None is not True
    95# None is not False
    96# None is None
    

  • I add an assert statement to see if a string (anything in quotes) is the same object as True

    67# assert 0.0 is True
    68assert 0.0 is not True
    69
    70assert '' is True
    71
    72
    73# NOTES
    

    the terminal shows AssertionError

    E       AssertionError: assert '' is True
    

    because a string is not the same object as True.

  • I change the statement to make it True

    67# assert 0.0 is True
    68assert 0.0 is not True
    69
    70# assert '' is True
    71assert '' is not True
    72
    73
    74# NOTES
    

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

  • I add a note about strings

     74# NOTES
     75# a dictionary is not False
     76# a dictionary is not None
     77# a set is not False
     78# a set is not None
     79# a list is not False
     80# a list is not None
     81# a tuple is not False
     82# a tuple is not None
     83# a string is not True
     84# a string is not False
     85# a string is not None
     86# a float is not True
     87# a float is not False
     88# a float is not None
     89# an integer is not True
     90# an integer is not False
     91# an integer is not None
     92# True is True
     93# True is not False
     94# True is not None
     95# False is not True
     96# False is False
     97# False is not None
     98# None is not True
     99# None is not False
    100# None is None
    

  • I add an assert statement to see if a tuple (anything in parentheses ( ) separated by a comma) is the same object as True

    the terminal shows

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

    70# assert '' is True
    71assert '' is not True
    72
    73# assert () is True
    74assert () is not True
    75
    76
    77# NOTES
    

    the test passes.

  • I add a note about tuples

     77# NOTES
     78# a dictionary is not False
     79# a dictionary is not None
     80# a set is not False
     81# a set is not None
     82# a list is not False
     83# a list is not None
     84# a tuple is not True
     85# a tuple is not False
     86# a tuple is not None
     87# a string is not True
     88# a string is not False
     89# a string is not None
     90# a float is not True
     91# a float is not False
     92# a float is not None
     93# an integer is not True
     94# an integer is not False
     95# an integer is not None
     96# True is True
     97# True is not False
     98# True is not None
     99# False is not True
    100# False is False
    101# False is not None
    102# None is not True
    103# None is not False
    104# None is None
    

  • I add an assert statement to see if a list (anything in square brackets [ ]) is the same object as True

    73# assert () is True
    74assert () is not True
    75
    76assert [] is True
    77
    78
    79# NOTES
    

    the terminal shows

    E   assert [] is True
    

    because a list is not the same object as True.

  • I change the statement to make it True

    73# assert () is True
    74assert () is not True
    75
    76# assert [] is True
    77assert [] is not True
    78
    79
    80# NOTES
    

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

  • I add a note about lists

     80# NOTES
     81# a dictionary is not False
     82# a dictionary is not None
     83# a set is not False
     84# a set is not None
     85# a list is not True
     86# a list is not False
     87# a list is not None
     88# a tuple is not True
     89# a tuple is not False
     90# a tuple is not None
     91# a string is not True
     92# a string is not False
     93# a string is not None
     94# a float is not True
     95# a float is not False
     96# a float is not None
     97# an integer is not True
     98# an integer is not False
     99# an integer is not None
    100# True is True
    101# True is not False
    102# True is not None
    103# False is not True
    104# False is False
    105# False is not None
    106# None is not True
    107# None is not False
    108# None is None
    

  • I add an assert statement to see if a set is the same object as True

    76# assert [] is True
    77assert [] is not True
    78
    79assert set() is True
    80
    81
    82# NOTES
    

    the terminal shows

    E   assert set() is True
    

    because a set is not the same object as True.

  • I change the statement to make it True

    76# assert [] is True
    77assert [] is not True
    78
    79# assert set() is True
    80assert set() is not True
    81
    82
    83# NOTES
    

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

  • I add a note about sets

     83# NOTES
     84# a dictionary is not False
     85# a dictionary is not None
     86# a set is not True
     87# a set is not False
     88# a set is not None
     89# a list is not True
     90# a list is not False
     91# a list is not None
     92# a tuple is not True
     93# a tuple is not False
     94# a tuple is not None
     95# a string is not True
     96# a string is not False
     97# a string is not None
     98# a float is not True
     99# a float is not False
    100# a float is not None
    101# an integer is not True
    102# an integer is not False
    103# an integer is not None
    104# True is True
    105# True is not False
    106# True is not None
    107# False is not True
    108# False is False
    109# False is not None
    110# None is not True
    111# None is not False
    112# None is None
    

  • I add an assert statement to see if a dictionary is the same object as True

    79# assert set() is True
    80assert set() is not True
    81
    82assert {} is True
    83
    84
    85# NOTES
    

    the terminal shows

    E   assert {} is True
    

    because a dictionary is not the same object as True.

  • I change the statement to make it True

    79# assert set() is True
    80assert set() is not True
    81
    82# assert {} is True
    83assert {} is not True
    84
    85
    86# NOTES
    

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

  • I add a note about dictionaries

     86# NOTES
     87# a dictionary is not True
     88# a dictionary is not False
     89# a dictionary is not None
     90# a set is not True
     91# a set is not False
     92# a set is not None
     93# a list is not True
     94# a list is not False
     95# a list is not None
     96# a tuple is not True
     97# a tuple is not False
     98# a tuple is not None
     99# a string is not True
    100# a string is not False
    101# a string is not None
    102# a float is not True
    103# a float is not False
    104# a float is not None
    105# an integer is not True
    106# an integer is not False
    107# an integer is not None
    108# True is True
    109# True is not False
    110# True is not None
    111# False is not True
    112# False is False
    113# False is not None
    114# None is not True
    115# None is not False
    116# None is None
    117
    118
    119# Exceptions seen
    120# AssertionError
    
  • I remove the commented assertions

    53# test AssertionError with True
    54assert None is not True
    55
    56assert False is not True
    57
    58assert True is True
    59
    60assert 0 is not True
    61
    62assert 0.0 is not True
    63
    64assert '' is not True
    65
    66assert () is not True
    67
    68assert [] is not True
    69
    70assert set() is not True
    71
    72assert {} is not True
    73
    74
    75# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'test AssertionError with True'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can use assertions to test if something is the same object as True or NOT.


test AssertionError with equality

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.

I can also use assertions to test if two things are equal, like I did when I test the assert keyword.


RED: make it fail


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

  • I add a new statement to test_assertion_error.py with a comment to group these new statements

    72assert {} is not True
    73
    74
    75# test AssertionError with equality
    76None != None
    77
    78
    79# NOTES
    

    the test is still passing.

  • I add the assert keyword

    75# test AssertionError with equality
    76# None != None
    77assert None != None
    78
    79
    80# NOTES
    

    the terminal is my friend, and shows

    E   assert None != None
    
    • because None is equal to None.

    • == is two equal signs on the keyboard (=+=) and means is equal which makes this statement read as None is equal to None


GREEN: make it pass


I change the statement to make it True

75# test AssertionError with equality
76# None != None
77# assert None != None
78assert None == None
79
80
81# NOTES
  • the test passes because None is equal to None

  • == is two equal signs on the keyboard (=+=) and means is equal which makes this statement read as 1 + 1 is equal to 2


REFACTOR: make it better


  • I add to the None is None note

     81# NOTES
     82# a dictionary is not True
     83# a dictionary is not False
     84# a dictionary is not None
     85# a set is not True
     86# a set is not False
     87# a set is not None
     88# a list is not True
     89# a list is not False
     90# a list is not None
     91# a tuple is not True
     92# a tuple is not False
     93# a tuple is not None
     94# a string is not True
     95# a string is not False
     96# a string is not None
     97# a float is not True
     98# a float is not False
     99# a float is not None
    100# an integer is not True
    101# an integer is not False
    102# an integer is not None
    103# True is True
    104# True is not False
    105# True is not None
    106# False is not True
    107# False is False
    108# False is not None
    109# None is not True
    110# None is not False
    111# None is None and equal to None
    
  • I add an assertion to compare False with None

    75# test AssertionError with equality
    76# None != None
    77# assert None != None
    78assert None == None
    79
    80assert False == None
    81
    82
    83# NOTES
    

    the terminal is my friend, and shows

    E    assert False == None
    

    because False is NOT equal to None and None is NOT equal to False.

  • I change the assert statement to make it True

    75# test AssertionError with equality
    76# None != None
    77# assert None != None
    78assert None == None
    79
    80# assert False == None
    81assert False != None
    82
    83
    84# NOTES
    

    the test passes because False is NOT equal to None and None is NOT equal to False.

  • I add to the False is not None and None is not False notes because equality goes both ways

     84# NOTES
     85# a dictionary is not True
     86# a dictionary is not False
     87# a dictionary is not None
     88# a set is not True
     89# a set is not False
     90# a set is not None
     91# a list is not True
     92# a list is not False
     93# a list is not None
     94# a tuple is not True
     95# a tuple is not False
     96# a tuple is not None
     97# a string is not True
     98# a string is not False
     99# a string is not None
    100# a float is not True
    101# a float is not False
    102# a float is not None
    103# an integer is not True
    104# an integer is not False
    105# an integer is not None
    106# True is True
    107# True is not False
    108# True is not None
    109# False is not True
    110# False is False
    111# False is not None and NOT equal to None
    112# None is not True
    113# None is not False and NOT equal to False
    114# None is None and equal to None
    
  • I add an assertion to compare False with True

    80# assert False == None
    81assert False != None
    82
    83assert False == True
    84
    85
    86# NOTES
    

    the terminal is my friend, and shows

    E       assert False == True
    

    because False is NOT equal to True and True is NOT equal to False.

  • I change the assert statement to make it True

    80# assert False == None
    81assert False != None
    82
    83# assert False == True
    84assert False != True
    85
    86
    87# NOTES
    

    the test passes because False is NOT equal to True and True is NOT equal to False.

  • I add to the False is not True and True is not False notes because equality goes both ways

     87# NOTES
     88# a dictionary is not True
     89# a dictionary is not False
     90# a dictionary is not None
     91# a set is not True
     92# a set is not False
     93# a set is not None
     94# a list is not True
     95# a list is not False
     96# a list is not None
     97# a tuple is not True
     98# a tuple is not False
     99# a tuple is not None
    100# a string is not True
    101# a string is not False
    102# a string is not None
    103# a float is not True
    104# a float is not False
    105# a float is not None
    106# an integer is not True
    107# an integer is not False
    108# an integer is not None
    109# True is True
    110# True is not False and NOT equal to False
    111# True is not None
    112# False is not True and NOT equal to True
    113# False is False
    114# False is not None and NOT equal to None
    115# None is not True
    116# None is not False and NOT equal to False
    117# None is None and equal to None
    
  • I add an assertion to compare False with itself

    83# assert False == True
    84assert False != True
    85
    86assert False != False
    87
    88
    89# NOTES
    

    the terminal is my friend, and shows

    E    assert False != False
    

    because False is equal to False.

  • I make the statement True

    83# assert False == True
    84assert False != True
    85
    86# assert False != False
    87assert False == False
    88
    89
    90# NOTES
    

    the test passes because False is equal to False.

  • I add to the False is False note

     90# NOTES
     91# a dictionary is not True
     92# a dictionary is not False
     93# a dictionary is not None
     94# a set is not True
     95# a set is not False
     96# a set is not None
     97# a list is not True
     98# a list is not False
     99# a list is not None
    100# a tuple is not True
    101# a tuple is not False
    102# a tuple is not None
    103# a string is not True
    104# a string is not False
    105# a string is not None
    106# a float is not True
    107# a float is not False
    108# a float is not None
    109# an integer is not True
    110# an integer is not False
    111# an integer is not None
    112# True is True
    113# True is not False and NOT equal to False
    114# True is not None
    115# False is not True and NOT equal to True
    116# False is False and equal to False
    117# False is not None and NOT equal to None
    118# None is not True
    119# None is not False and NOT equal to False
    120# None is None and equal to None
    

  • I add a new failing assertion to compare True with None

    86# assert False != False
    87assert False == False
    88
    89assert True == None
    90
    91
    92# NOTES
    

    the terminal is my friend, and shows

    E    assert True == None
    

    because True is NOT equal None and None is NOT equal to True.

  • I change the assert statement to make it True

    86# assert False != False
    87assert False == False
    88
    89# assert True == None
    90assert True != None
    91
    92
    93# NOTES
    

    the test passes because True is NOT equal None and None is NOT equal to True.

  • I add to the True is not None and None is not True notes because equality goes both ways

     93# NOTES
     94# a dictionary is not True
     95# a dictionary is not False
     96# a dictionary is not None
     97# a set is not True
     98# a set is not False
     99# a set is not None
    100# a list is not True
    101# a list is not False
    102# a list is not None
    103# a tuple is not True
    104# a tuple is not False
    105# a tuple is not None
    106# a string is not True
    107# a string is not False
    108# a string is not None
    109# a float is not True
    110# a float is not False
    111# a float is not None
    112# an integer is not True
    113# an integer is not False
    114# an integer is not None
    115# True is True
    116# True is not False and NOT equal to False
    117# True is not None and NOT equal to None
    118# False is not True and NOT equal to True
    119# False is False and equal to False
    120# False is not None and NOT equal to None
    121# None is not True and NOT equal to True
    122# None is not False and NOT equal to False
    123# None is None and equal to None
    
  • I add an assertion to compare True with itself

    89# assert True == None
    90assert True != None
    91
    92assert True != True
    93
    94
    95# NOTES
    

    the terminal is my friend, and shows

    E    assert True != True
    

    because True is equal to True.

  • I make the statement True

    89# assert True == None
    90assert True != None
    91
    92# assert True != True
    93assert True == True
    94
    95
    96# NOTES
    

    the test passes because True is equal to True.

  • I add to the True is True note

     96# NOTES
     97# a dictionary is not True
     98# a dictionary is not False
     99# a dictionary is not None
    100# a set is not True
    101# a set is not False
    102# a set is not None
    103# a list is not True
    104# a list is not False
    105# a list is not None
    106# a tuple is not True
    107# a tuple is not False
    108# a tuple is not None
    109# a string is not True
    110# a string is not False
    111# a string is not None
    112# a float is not True
    113# a float is not False
    114# a float is not None
    115# an integer is not True
    116# an integer is not False
    117# an integer is not None
    118# True is True and equal to True
    119# True is not False and NOT equal to False
    120# True is not None and NOT equal to None
    121# False is not True and NOT equal to True
    122# False is False and equal to False
    123# False is not None and NOT equal to None
    124# None is not True and NOT equal to True
    125# None is not False and NOT equal to False
    126# None is None and equal to None
    127
    128
    129# Exceptions seen
    130# AssertionError
    
  • I remove the commented assertions

    75# test AssertionError with equality
    76assert None == None
    77
    78assert False != None
    79
    80assert False != True
    81
    82assert False == False
    83
    84assert True != None
    85
    86assert True == True
    87
    88
    89# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test AssertionError with equality'
    

    the terminal shows a summary of the changes then goes back to the command line.

I can use assertions to test if two things are equal.


test AssertionError with is vs equal

Some of the tests I have written use is and some use ==, if they mean the same thing, why do I use different symbols? What is the difference between is and ==?

In Python they do not mean the same thing

  • x is y states that x is the same exact object as y

  • x == y states that x is equal to y according to a rule or instruction programmed in Python

  • x is not y states that x is NOT the same exact object as y

  • x != y states that x is NOT equal to y according to a rule or instruction programmed in Python

This means that things can be equal without being the same exact object. For example, an integer (a whole number without decimals) can be equal to a float (binary floating point decimal numbers) and they are NOT the same object.


RED: make it fail


  • I add statements to show this

    86assert True == True
    87
    88
    89# test AssertionError with is vs equal
    900 is 0.0
    91
    92
    93# NOTES
    

    the test is still passing.

  • I add the assert keyword

    89# test AssertionError with is vs equal
    90# 0 is 0.0
    91assert 0 is 0.0
    92
    93
    94# NOTES
    

    the terminal is my friend, and shows

    E   assert 0 is 0.0
    

    because an integer is not the same object as a float.


GREEN: make it pass


I change the assertion to make the statement True

89  # test AssertionError with is vs equal
90  # 0 is 0.0
91  # assert 0 is 0.0
92  assert 0 is not 0.0
93
94
95  # NOTES

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


REFACTOR: make it better


  • I add another assertion, this time with !=

    89# test AssertionError with is vs equal
    90# 0 is 0.0
    91# assert 0 is 0.0
    92assert 0 is not 0.0
    93
    94assert 0 != 0.0
    95
    96
    97# NOTES
    

    the terminal is my friend, and shows

    E       assert 0 != 0.0
    

    because 0 and 0.0 are equal, they are both zero.

  • I change the assertion to make the statement True

    89# test AssertionError with is vs equal
    90# 0 is 0.0
    91# assert 0 is 0.0
    92assert 0 is not 0.0
    93
    94# assert 0 != 0.0
    95assert 0 == 0.0
    96
    97
    98# NOTES
    

    the test passes because 0 and 0.0 are equal, they are both zero.

  • I remove the commented assertions

    89# test AssertionError with is vs equal
    90assert 0 is not 0.0
    91
    92assert 0 == 0.0
    93
    94
    95# NOTES
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'test AssertionError with is vs equal'
    

    the terminal shows a summary of the changes then goes back to the command line.

The tests show that an integer can be EQUAL to a float but an integer IS NOT a float.


a better way to organize tests

I used comments to group the assertions by what I was testing, and each time I made an assertion True, the terminal showed a confusing message (no tests ran).

Since I am using pytest-watcher which uses pytest to run the test, I can group the assertions with functions instead.

A function is code that is callable, this means I can write code to do something one time, and call the name for it to do that thing at a different time from when I write it.

functions are made with

  • the def keyword

  • a name

  • parentheses and a colon at the end

  • the code that makes up the function (its body) comes after the colon

def name_of_function():
    the body of the function
    ...

test_assertion_error_w_is_vs_equal


  • I add a function for the # test AssertionError with is vs equal group of assertions

    86assert True == True
    87
    88
    89def assertion_error_w_is_vs_equal():
    90# test AssertionError with is vs equal
    91assert 0 is not 0.0
    92
    93assert 0 == 0.0
    

    the terminal is my friend, and shows IndentationError

    IndentationError: expected an indented block
                      after function definition on line 89
    

    because in Python the body of a function is indented under the name.

  • I indent the assertions with four spaces to make them the body of the function

    86assert True == True
    87
    88
    89def assertion_error_w_is_vs_equal():
    90    # test AssertionError with is vs equal
    91    assert 0 is not 0.0
    92
    93    assert 0 == 0.0
    94
    95
    96# NOTES
    

    the test passes and the terminal still shows no tests ran

  • I add test_ to the name of the function

    86assert True == True
    87
    88
    89# def assertion_error_w_is_vs_equal():
    90def test_assertion_error_w_is_vs_equal():
    91    # test AssertionError with is vs equal
    92    assert 0 is not 0.0
    93
    94    assert 0 == 0.0
    95
    96
    97# NOTES
    

    the terminal shows green and the name of the test file with a better message

    ================== test session starts ===================
    platform linux -- Python 3.X.Y, pytest-Z.A.B, pluggy-C.D.E
    rootdir: /.../pumping_python/assertion_error
    configfile: pyproject.toml
    collected 1 item
    
    tests/test_assertion_error.py .                     [100%]
    
    =================== 1 passed in F.GHs ====================
    [pytest-watcher]
    Current runner args: []
    Press w to show menu
    

    fantastic! pytest only calls the function if the name starts with test.

  • I remove the commented lines

    86assert True == True
    87
    88
    89def test_assertion_error_w_is_vs_equal():
    90    assert 0 is not 0.0
    91
    92    assert 0 == 0.0
    93
    94
    95# NOTES
    
  • I add a git commit message

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

    the terminal shows a summary of the changes then goes back to the command line.


test_assertion_error_w_equality


  • I add a function for the # test AssertionError with equality group of assertions

    72assert {} is not True
    73
    74
    75def assertion_error_w_equality():
    76# test AssertionError with equality
    77assert None == None
    

    the terminal is my friend, and shows IndentationError

    IndentationError: expected an indented block
                      after function definition on line 75
    

    because in Python the body of a function is indented under the name.

  • I indent the assertions with four spaces to make them the body of the function

    75assert {} is not True
    76
    77
    78def assertion_error_w_equality():
    79    # test AssertionError with equality
    80    assert None == None
    81
    82    assert False != None
    83
    84    assert False != True
    85
    86    assert False == False
    87
    88    assert True != None
    89
    90    assert True == True
    91
    92
    93def test_assertion_error_w_is_vs_equal():
    

    the terminal shows

    =================== 1 passed in L.MNs ====================
    

    I have two functions with assertions and it only recognizes one.

  • I add test_ to the name of the function

    72assert {} is not True
    73
    74
    75# def assertion_error_w_equality():
    76def test_assertion_error_w_equality():
    77    # test AssertionError with equality
    78    assert None == None
    79
    80    assert False != None
    

    the terminal shows

    =================== 2 passed in O.PQs ====================
    

    because pytest only calls the function if the name starts with test

  • I remove the commented lines

    72assert {} is not True
    73
    74
    75def test_assertion_error_w_equality():
    76    assert None == None
    77
    78    assert False != None
    
  • I add a git commit message

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

    the terminal shows a summary of the changes then goes back to the command line.


test_assertion_error_w_true


  • I add a function for the # test AssertionError with True group of assertions

    50assert {} is not False
    51
    52
    53def assertion_error_w_true():
    54# test AssertionError with True
    55assert None is not True
    

    the terminal is my friend, and shows IndentationError

    IndentationError: expected an indented block
                      after function definition on line 53
    

    because in Python the body of a function is indented under the name.

  • I indent the assertions with four spaces to make them the body of the function

    50assert {} is not False
    51
    52
    53def assertion_error_w_true():
    54    # test AssertionError with True
    55    assert None is not True
    56
    57    assert False is not True
    58
    59    assert True is True
    60
    61    assert 0 is not True
    62
    63    assert 0.0 is not True
    64
    65    assert '' is not True
    66
    67    assert () is not True
    68
    69    assert [] is not True
    70
    71    assert set() is not True
    72
    73    assert {} is not True
    74
    75
    76def test_assertion_error_w_equality():
    

    the terminal shows

    =================== 2 passed in R.STs ====================
    

    I have three functions with assertions and it only recognizes two.

  • I add test_ to the name of the function

    50assert {} is not False
    51
    52
    53# def assertion_error_w_true():
    54def test_assertion_error_w_true():
    55    # test AssertionError with True
    56    assert None is not True
    57
    58    assert False is not True
    

    the terminal shows

    =================== 3 passed in U.VWs ====================
    

    because pytest only calls the function if the name starts with test

  • I remove the commented lines

    50assert {} is not False
    51
    52
    53def test_assertion_error_w_true():
    54    assert None is not True
    55
    56    assert False is not True
    
  • I add a git commit message

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

    the terminal shows a summary of the changes then goes back to the command line.


test_assertion_error_w_false


  • I add a function for the # test AssertionError with False group of assertions

    28assert {} is not None
    29
    30
    31def assertion_error_w_false():
    32# test AssertionError with False
    33assert None is not False
    

    the terminal is my friend, and shows IndentationError

    IndentationError: expected an indented block
                      after function definition on line 31
    

    because in Python the body of a function is indented under the name.

  • I indent the assertions with four spaces to make them the body of the function

    28assert {} is not None
    29
    30
    31def assertion_error_w_false():
    32    # test AssertionError with False
    33    assert None is not False
    34
    35    assert False is False
    36
    37    assert True is not False
    38
    39    assert 0 is not False
    40
    41    assert 0.0 is not False
    42
    43    assert '' is not False
    44
    45    assert () is not False
    46
    47    assert [] is not False
    48
    49    assert set() is not False
    50
    51    assert {} is not False
    52
    53
    54def test_assertion_error_w_true():
    

    the terminal shows

    =================== 3 passed in X.YZs ====================
    

    I have four functions with assertions and it only recognizes three.

  • I add test_ to the name of the function

    28assert {} is not None
    29
    30
    31# def assertion_error_w_false():
    32def test_assertion_error_w_false():
    33    # test AssertionError with False
    34    assert None is not False
    35
    36    assert False is False
    

    the terminal shows

    =================== 4 passed in A.BCs ====================
    

    because pytest only calls the function if the name starts with test

  • I remove the commented lines

    28assert {} is not None
    29
    30
    31def test_assertion_error_w_false():
    32    assert None is not False
    33
    34    assert False is False
    
  • I add a git commit message

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

    the terminal shows a summary of the changes then goes back to the command line.


test_assertion_error_w_none


  • I add a function for the # test AssertionError with None group of assertions

     6assert 'I am' + ' alive' == 'I am alive'
     7
     8
     9def assertion_error_w_none():
    10# test AssertionError with None
    11assert None is None
    

    the terminal is my friend, and shows IndentationError

    IndentationError: expected an indented block
                      after function definition on line 9
    

    because in Python the body of a function is indented under the name.

  • I indent the assertions with four spaces to make them the body of the function

     6assert 'I am' + ' alive' == 'I am alive'
     7
     8
     9def assertion_error_w_none():
    10    # test AssertionError with None
    11    assert None is None
    12
    13    assert False is not None
    14
    15    assert True is not None
    16
    17    assert 0 is not None
    18
    19    assert 0.0 is not None
    20
    21    assert '' is not None
    22
    23    assert () is not None
    24
    25    assert [] is not None
    26
    27    assert set() is not None
    28
    29    assert {} is not None
    30
    31
    32def test_assertion_error_w_false():
    

    the terminal shows

    =================== 4 passed in D.EFs ====================
    

    I have five functions with assertions and it only recognizes four.

  • I add test_ to the name of the function

     6assert 'I am' + ' alive' == 'I am alive'
     7
     8
     9# def assertion_error_w_none():
    10def test_assertion_error_w_none():
    11    # test AssertionError with None
    12    assert None is None
    13
    14    assert False is not None
    

    the terminal shows

    =================== 5 passed in G.HIs ====================
    

    because pytest only calls the function if the name starts with test

  • I remove the commented lines

     6assert 'I am' + ' alive' == 'I am alive'
     7
     8
     9def test_assertion_error_w_none():
    10    assert None is None
    11
    12    assert False is not None
    
  • I add a git commit message

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

    the terminal shows a summary of the changes then goes back to the command line.


test_assert_keyword


  • I add a function for the # test the assert keyword group of assertions

    1def assert_keyword():
    2# test the assert keyword
    3assert 1 + 1 == 2
    

    the terminal is my friend, and shows IndentationError

    IndentationError: expected an indented block
                      after function definition on line 1
    

    because in Python the body of a function is indented under the name.

  • I indent the assertions with four spaces to make them the body of the function

     1def assert_keyword():
     2    # test the assert keyword
     3    assert 1 + 1 == 2
     4
     5    assert '1' + '1' == '11'
     6
     7    assert 'I am' + ' alive' == 'I am alive'
     8
     9
    10def test_assertion_error_w_none():
    

    the terminal shows

    =================== 5 passed in J.KLs ====================
    

    I have six functions with assertions and it only recognizes four.

  • I add test_ to the name of the function

     1# def assert_keyword():
     2def test_assert_keyword():
     3    # test the assert keyword
     4    assert 1 + 1 == 2
     5
     6    assert '1' + '1' == '11'
     7
     8    assert 'I am' + ' alive' == 'I am alive'
     9
    10
    11def test_assertion_error_w_none():
    

    the terminal shows

    =================== 6 passed in M.NOs ====================
    

    because pytest only calls the function if the name starts with test

  • I remove the commented lines

    1def test_assert_keyword():
    2    assert 1 + 1 == 2
    3
    4    assert '1' + '1' == '11'
    5
    6    assert 'I am' + ' alive' == 'I am alive'
    7
    8
    9def test_assertion_error_w_none():
    
  • I add a git commit message

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

    the terminal shows a summary of the changes then goes back to the command line.


pytest only calls the function if the name starts with test


  • I add a function to show that pytest only calls the function if the name starts with test

    89def test_assertion_error_w_is_vs_equal():
    90    assert 0 is not 0.0
    91
    92    assert 0 == 0.0
    93
    94
    95def will_not_run():
    96    assert False == True
    97
    98
    99# NOTES
    

    the terminal still shows

    =================== 6 passed in P.QRs ====================
    
  • I add a function that starts with test with the same assertion

     95def will_not_run():
     96    assert False == True
     97
     98
     99def test_failure():
    100    assert False == True
    101
    102
    103# NOTES
    

    the terminal is my friend, and shows AssertionError

  • I change the assertion to make it True

     99def test_failure():
    100    # assert False == True
    101    assert False == False
    102
    103
    104# NOTES
    

    the test passes. I leave the commented line as a reminder of what will cause the failure.

  • I add a git commit message

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

    the terminal shows a summary of the changes then goes back to the command line.


close the project

  • I close test_assertion_error.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 assertion_error

    cd ..
    

    the terminal is my friend, and shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

The tests show that

How many questions can you answer about AssertionError?


code from the chapter

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


what is next?

Would you like to test functions?


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.