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 or to test ideas and see if they work, without worrying about if I will remember the ideas later.
I use them to test how the program behaves, for example when it is 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
1import unittest
2
3
4class TestAssertionError(unittest.TestCase):
5
6 def test_what_is_an_assertion(self):
7 reality = 1 + 1
8 my_expectation = 2
9 assert reality == my_expectation
10 self.assertEqual(
11 reality, my_expectation
12 )
13
14 reality = '1' + '1'
15 my_expectation = '11'
16 assert reality == my_expectation
17 self.assertEqual(
18 reality, my_expectation
19 )
20
21 reality = 'I am' + ' alive'
22 my_expectation = 'I am alive'
23 assert reality == my_expectation
24 self.assertEqual(
25 reality, my_expectation
26 )
27
28 def test_assertion_error_w_none(self):
29 assert None is None
30 assert False is not None
31 assert True is not None
32 assert 0 is not None
33 assert 0.0 is not None
34 assert 'a string' is not None
35 assert (1, 2, 3, 'n') is not None
36 assert [1, 2, 3, 'n'] is not None
37 assert {1, 2, 3, 'n'} is not None
38 assert {'key': 'value'} is not None
39
40 def test_assertion_error_w_false(self):
41 assert None is not False
42 assert False is False
43 assert True is not False
44 assert 0 is not False
45 assert 0.0 is not False
46 assert 'a string' is not False
47 assert (1, 2, 3, 'n') is not False
48 assert [1, 2, 3, 'n'] is not False
49 assert {1, 2, 3, 'n'} is not False
50 assert {'key': 'value'} is not False
51
52 def test_assertion_error_w_true(self):
53 assert None is not True
54 assert False is not True
55 assert True is True
56 assert 0 is not True
57 assert 0.0 is not True
58 assert 'a string' is not True
59 assert (1, 2, 3, 'n') is not True
60 assert [1, 2, 3, 'n'] is not True
61 assert {1, 2, 3, 'n'} is not True
62 assert {'key': 'value'} is not True
63
64 def test_assertion_error_w_equality(self):
65 assert None == None
66 self.assertEqual(None, None)
67
68 assert False != None
69 self.assertNotEqual(False, None)
70
71 assert False != True
72 self.assertNotEqual(False, True)
73
74 assert False == False
75 self.assertEqual(False, False)
76
77 assert True != None
78 self.assertNotEqual(True, None)
79
80 assert True == True
81 self.assertEqual(True, True)
82
83
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 False
89# a set is not None
90# a list is not False
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 and equal to True
107# True is not False and NOT equal to False
108# True is not None and NOT equal to None
109# False is not True and NOT equal to True
110# False is False and equal to False
111# False is not None and NOT equal to None
112# None is not True and NOT equal to True
113# None is not False and NOT equal to False
114# None is None and equal to None
115
116# Exceptions seen
117# AssertionError
questions about AssertionError
Questions to think about as I go through the chapter
start the project
I name this project
assertion_errorI open a terminal
I use uv to make a directory for the project and initialize it
uv init assertion_errorthe 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_errorthe terminal shows I am in the
assertion_errorfolder.../pumping_python/assertion_errorI use rm to remove
main.pybecause I do not use it in this projectrm main.pythe terminal goes back to the command line.
I make a directory for the tests
mkdir teststhe terminal goes back to the command line.
I make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line.
I make a Python file for the tests in the
testsdirectorytouch tests/test_assertion_error.pyNew-Item tests/test_assertion_error.pythe terminal goes back to the command line.
I open
test_assertion_error.pyin the editor of the Integrated Development Environment (IDE)Tip
I can open a file from the terminal in the Integrated Development Environment (IDE) with the name of the program and the name of the file. That means if I type this in the terminal
code tests/test_assertion_error.pyVisual Studio Code opens
test_assertion_error.pyin the editorI add the first failing test to
test_assertion_error.py1import unittest 2 3 4class TestAssertionError(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(True)I go back to the terminal to make a requirements file for the Python packages I need
echo "pytest" > requirements.txtthe terminal goes back to the command line.
I add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line.
I install the Python packages that I wrote in the requirements file
uv add --requirement requirements.txtthe terminal shows that it installed the Python packages
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, 142 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.lockthen goes back to the command line.
I use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows AssertionError
======================== 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 ====================if the terminal does not show the same error, then check
if your
tests/__init__.pyhas two underscores (__) before and afterinitfor__init__.pynot_init_.pyif you ran
echo "pytest-watcher" >> requirements.txt, to addpytest-watcherto the requirements file
fix those errors and try to run
uv run pytest-watcher . --nowagainI add AssertionError to the list of Exceptions seen in
test_assertion_error.pyin the editor4class TestAssertionError(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(True) 8 9 10# Exceptions seen 11# AssertionErrorthen I change True to False in the assertion
4class TestAssertionError(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(False) 8 9 10# Exceptions seen 11# AssertionErrorthe test passes.
what is a variable?
I can use a name for values when programming. This helps me avoid repetition when I have to change a value.
A variable is a name that is used for values that change. For example, in Mathematics we use x to represent any number.
test_what_is_an_assertion
We know that the result of 1 + 1 is 2. 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_failuretotest_what_is_an_assertionthen add variables with a statement1import unittest 2 3 4class TestAssertionError(unittest.TestCase): 5 6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 reality == my_expectation 10 11 12# Exceptions seen 13# AssertionErrorthe test is still green.
I change
my_expectationto make the statement False6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 11 9 reality == my_expectationwhy is the test still green?
I want the test to fail when I write a statement that is NOT True. I change it to an assert statement
6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 11 9 assert reality == my_expectationthe terminal is my friend, and shows AssertionError
E assert 2 == 11because
1 + 1 == 11is False.2is NOT equal to11and I added assert before the statement, which tells the computerDO NOT CONTINUE, if "1 + 1 == 11" is False.
GREEN: make it pass
I change my_expectation to match reality and make the statement True
6 def test_what_is_an_assertion(self):
7 reality = 1 + 1
8 my_expectation = 2
9 assert reality == my_expectation
the test passes because 1 + 1 == 2 is NOT False, 1 + 1 is equal to 2
REFACTOR: make it better
I add another statement
6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 11 reality = '1' + '1' 12 my_expectation = '2' 13 reality == my_expectation 14 15 16# Exceptions seen 17# AssertionErrorthe test is still green when the statement is not an assertion
I add assert before the statement to make it an assertion
6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 11 reality = '1' + '1' 12 my_expectation = '2' 13 assert reality == my_expectation 14 15 16# Exceptions seenthe terminal is my friend, and shows AssertionError
E AssertionError: assert '11' == '2'because when I added assert before the statement it became a command to Python -
DO NOT CONTINUE if "'1' + '1' == '2'" is FalseI change
my_expectationto matchreality6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 11 reality = '1' + '1' 12 my_expectation = '11' 13 assert reality == my_expectation 14 15 16# Exceptions seenthe test passes because the statement is now True
These 2 statements are NOT the same
1 + 1 == 2checks 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 anything inside quotes
I add another statement to show why
'1' + '1' == '11'6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 11 reality = '1' + '1' 12 my_expectation = '11' 13 assert reality == my_expectation 14 15 reality = 'I am' + ' alive' 16 my_expectation = '11' 17 reality == my_expectation 18 19 20# Exceptions seenthe test is still green because
reality == my_expectationis just a statement. Python does not care whether it is True or False and the statement follows Python rules.I change the statement to an assertion
6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 11 reality = '1' + '1' 12 my_expectation = '11' 13 assert reality == my_expectation 14 15 reality = 'I am' + ' alive' 16 my_expectation = '11' 17 assert reality == my_expectation 18 19 20# Exceptions seenthe terminal is my friend, and shows AssertionError
E AssertionError: assert 'I am alive' == '11'because when I added assert before the statement it became a command to Python -
DO NOT CONTINUE if "'I am' + ' alive' == '11'" is FalseAttention
If your result is different, check that you added a space before
alive, it should be' alive'not'alive'.I change
my_expectationto matchrealityand make the statement True6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 11 reality = '1' + '1' 12 my_expectation = '11' 13 assert reality == my_expectation 14 15 reality = 'I am' + ' alive' 16 my_expectation = 'I am alive' 17 assert reality == my_expectation 18 19 20# Exceptions seenthe test passes.
I open a new terminal then change directories to
assertion_errorcd assertion_errorthe terminal shows I am in the
assertion_errorfolder.../pumping_python/assertion_errorI add a git commit message
git commit --all --message 'add test_what_is_an_assertion'the terminal shows a summary of the changes then goes back to the command line.
what is the difference between = and ==?
=is the symbol for assignment, pointing, defining references. It is how to give a name (variable) to an object in Python, for exampleproject_name = 'assertion_error'is a way to tell Python that
project_nameis the name for the string'assertion_error'. Python will substituteproject_namewith'assertion_error'anytime I use the name after the declaration, because I told it thatproject_nameis a reference for that string (anything in quotes)==is used to check if the thing on the left of==is equal to the thing on the right of==, for exampleassert 'thing on the left' == 'thing on the right'if 'thing on the left' == 'thing on the right': return 'magic'
what causes AssertionError?
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 == False
With these statements, I tell Python - “DO NOT CONTINUE, if True is equal to False”, or said a different way “GO TO THE NEXT LINE, ONLY if True is equal to False”. I expect this line to fail because True is NOT equal to False, they at least have different spellings. 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, this is useful when 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 a value blank, I can use an assertion to make sure that the process returns None.
RED: make it fail
I go back to the terminal where the tests are running
I add a new failing test to
test_assertion_error.pyin the editor6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 11 reality = '1' + '1' 12 my_expectation = '11' 13 assert reality == my_expectation 14 15 reality = 'I am' + ' alive' 16 my_expectation = 'I am alive' 17 assert reality == my_expectation 18 19 def test_assertion_error_w_none(self): 20 assert None is not None 21 22 23# Exceptions seenthe terminal is my friend, and shows AssertionError
E assert None is not Nonebecause
None is not Noneis False
GREEN: make it pass
I change the statement to make it True
19 def test_assertion_error_w_none(self):
20 assert None is None
the test passes.
REFACTOR: make it better
I add a note with what I learned from the experiment
19 def test_assertion_error_w_none(self): 20 assert None is None 21 22 23# NOTES 24# None is None 25 26 27# Exceptions seen 28# AssertionError
I add a new assertion to compare None with False, another simple data structure
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is None 22 23 24# NOTESthe terminal is my friend, and shows AssertionError
E assert False is NoneI change the assert statement to make it True
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 23 24# NOTESthe test passes.
I add a note about False
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 23 24# NOTES 25# False is not None 26# None is None 27 28 29# Exceptions seen 30# AssertionError
I add an assertion to compare None with True, another simple data structure
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is None 23 24 25# NOTESthe terminal is my friend, and shows AssertionError
E assert True is NoneI change the assert statement to make it True
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 24 25# NOTESthe test passes.
I add a note about True
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 24 25# NOTES 26# True is not None 27# False is not None 28# None is None 29 30 31# Exceptions seen 32# AssertionError
I add an assert statement for an integer (a whole number with no decimals)
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is None 24 25 26# NOTESthe terminal is my friend, and shows AssertionError
E assert 0 is NoneI change the statement to make it True
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 25 26# NOTESthe test passes.
I add a note about integers
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 25 26# NOTES 27# an integer is not None 28# True is not None 29# False is not None 30# None is None 31 32 33# Exceptions seen 34# AssertionError
I add an assert statement for a float (binary floating point decimal number)
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is None 25 26 27# NOTESthe terminal is my friend, and shows AssertionError
E assert 0.0 is NoneI change the statement to make it True
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 26 27# NOTESthe test passes.
I add a note about floats
6 def test_assertion_error_w_none(self): 7 assert None is None 8 assert False is not None 9 assert True is not None 10 assert 0 is not None 11 assert 0.0 is not None 12 13 14# NOTES 15# a float is not None 16# an integer is not None 17# True is not None 18# False is not None 19# None is None 20 21 22# Exceptions seen 23# AssertionError
I add an assert statement for a string (anything in quotes)
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 assert 'a string' is None 26 27 28# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: assert 'a string' is NoneI change the statement to make it True
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 assert 'a string' is not None 26 27 28# NOTESthe test passes.
I add a note about strings
28# NOTES 29# a string is not None 30# a float is not None 31# an integer is not None 32# True is not None 33# False is not None 34# None is None 35 36 37# Exceptions seen 38# AssertionError
I add an assert statement for a tuple (anything in parentheses
( )separated by a comma)19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 assert 'a string' is not None 26 assert (1, 2, 3, 'n') is None 27 28 29# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: assert (1, 2, 3, 'n') is NoneI change the statement to make it True
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 assert 'a string' is not None 26 assert (1, 2, 3, 'n') is not None 27 28 29# NOTESthe test passes.
I add a note about tuples
29# NOTES 30# a tuple is not None 31# a string is not None 32# a float is not None 33# an integer is not None 34# True is not None 35# False is not None 36# None is None 37 38 39# Exceptions seen 40# AssertionError
I add an assert statement for a list (anything in square brackets
[ ])19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 assert 'a string' is not None 26 assert (1, 2, 3, 'n') is not None 27 assert [1, 2, 3, 'n'] is None 28 29 30# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: assert [1, 2, 3, 'n'] is NoneI change the statement to make it True
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 assert 'a string' is not None 26 assert (1, 2, 3, 'n') is not None 27 assert [1, 2, 3, 'n'] is not None 28 29 30# NOTESthe test passes.
I add a note about lists
30# NOTES 31# a list is not None 32# a tuple is not None 33# a string is not None 34# a float is not None 35# an integer is not None 36# True is not None 37# False is not None 38# None is None 39 40 41# Exceptions seen 42# AssertionError
I add an assert statement for a set (anything in curly braces
{ }separated by a comma)19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 assert 'a string' is not None 26 assert (1, 2, 3, 'n') is not None 27 assert [1, 2, 3, 'n'] is not None 28 assert {1, 2, 3, 'n'} is None 29 30 31# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: assert {1, 2, 3, 'n'} is NoneI change the statement to make it True
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 assert 'a string' is not None 26 assert (1, 2, 3, 'n') is not None 27 assert [1, 2, 3, 'n'] is not None 28 assert {1, 2, 3, 'n'} is not None 29 30 31# NOTESthe test passes.
I add a note about sets
31# NOTES 32# a set is not None 33# a list is not None 34# a tuple is not None 35# a string is not None 36# a float is not None 37# an integer is not None 38# True is not None 39# False is not None 40# None is None 41 42 43# Exceptions seen 44# AssertionError
I add an assert statement for a dictionary (any key-value pairs in curly braces
{ }separated by a comma)19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 assert 'a string' is not None 26 assert (1, 2, 3, 'n') is not None 27 assert [1, 2, 3, 'n'] is not None 28 assert {1, 2, 3, 'n'} is not None 29 assert {'key': 'value'} is None 30 31 32# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: assert {'key': 'value'} is NoneI change the statement to make it True
19 def test_assertion_error_w_none(self): 20 assert None is None 21 assert False is not None 22 assert True is not None 23 assert 0 is not None 24 assert 0.0 is not None 25 assert 'a string' is not None 26 assert (1, 2, 3, 'n') is not None 27 assert [1, 2, 3, 'n'] is not None 28 assert {1, 2, 3, 'n'} is not None 29 assert {'key': 'value'} is not None 30 31 32# NOTESthe test passes.
I add a note about dictionaries
32# NOTES 33# a dictionary is not None 34# a set is not None 35# a list is not None 36# a tuple is not None 37# a string is not None 38# a float is not None 39# an integer is not None 40# True is not None 41# False is not None 42# None is None 43 44 45# Exceptions seen 46# AssertionErrorI add a git commit message in the other terminal
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_assertion_error_w_false
False is one of the two booleans and is NOT None. I can use assertions to test if something is False or not.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion to see if None is False, in
test_assertion_error.pyin the editor29 assert {'key': 'value'} is not None 30 31 def test_assertion_error_w_false(self): 32 assert None is False 33 34 35# NOTESthe terminal is my friend, and shows AssertionError
E assert None is False
GREEN: make it pass
I change the assertion to make it True
31 def test_assertion_error_w_false(self):
32 assert None is not False
33
34
35 # NOTES
the test passes.
REFACTOR: make it better
I add a note about None
35# NOTES 36# a dictionary is not None 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 not False 46# None is None
I add an assert statement about False, that will fail
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is not False 34 35 36# NOTESthe terminal shows AssertionError
E assert False is not FalseI change the statement to make it True
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 35 36# NOTESthe test passes.
I add a note about False
36# NOTES 37# a dictionary is not None 38# a set is not None 39# a list is not None 40# a tuple is not None 41# a string is not None 42# a float is not None 43# an integer is not None 44# True is not None 45# False is False 46# False is not None 47# None is not False 48# None is None
I add an assert statement to see if True is False
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is False 35 36 37# NOTESthe terminal shows AssertionError
E assert True is FalseI change the statement to make it True
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 36 37# NOTESthe test passes.
I add a note about True
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 False 46# True is not None 47# False is False 48# False is not None 49# None is not False 50# None is None
I add an assert statement to see if an integer (a whole number with no decimals) is False
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is False 36 37 38# NOTESthe terminal shows AssertionError
E assert 0 is FalseI change the statement to make it True
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 37 38# NOTESthe test passes.
I add a note about integers
38# NOTES 39# a dictionary is not None 40# a set is not None 41# a list is not None 42# a tuple is not None 43# a string is not None 44# a float is not None 45# an integer is not False 46# an integer is not None 47# True is not False 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 a float (binary floating point decimal number) is False
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is False 37 38 39# NOTESthe terminal shows AssertionError
E assert 0.0 is FalseI change the statement to make it True
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 38 39# NOTESthe test passes.
I add a note about floats
39# NOTES 40# a dictionary is not None 41# a set is not None 42# a list is not None 43# a tuple is not None 44# a string is not None 45# a float is not False 46# a float is not None 47# an integer is not False 48# an integer is not None 49# True is not False 50# True is not None 51# False is False 52# False is not None 53# None is not False 54# None is None
I add an assert statement to see if a string (anything in quotes) is False
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 assert 'a string' is False 38 39 40# NOTESthe terminal shows AssertionError
E AssertionError: assert 'a string' is FalseI change the statement to make it True
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 assert 'a string' is not False 38 39 40# NOTESthe test passes.
I add a note about strings
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 False 46# a string is not None 47# a float is not False 48# a float is not None 49# an integer is not False 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 a tuple (anything in parentheses
( )separated by a comma) is False31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 assert 'a string' is not False 38 assert (1, 2, 3, 'n') is False 39 40 41# NOTESthe terminal shows AssertionError
E AssertionError: assert (1, 2, 3, 'n') is FalseI change the statement to make it True
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 assert 'a string' is not False 38 assert (1, 2, 3, 'n') is not False 39 40 41# NOTESthe test passes.
I add a note about tuples
41# NOTES 42# a dictionary is not None 43# a set is not None 44# a list is not None 45# a tuple is not False 46# a tuple is not None 47# a string is not False 48# a string is not None 49# a float is not False 50# a float is not None 51# an integer is not False 52# an integer is not None 53# True is not False 54# True is not None 55# False is False 56# False is not None 57# None is not False 58# None is None
I add an assert statement to see if a list (anything in square brackets
[ ]) is False31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 assert 'a string' is not False 38 assert (1, 2, 3, 'n') is not False 39 assert [1, 2, 3, 'n'] is False 40 41 42# NOTESthe terminal shows AssertionError
E AssertionError: assert [1, 2, 3, 'n'] is FalseI change the statement to make it True
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 assert 'a string' is not False 38 assert (1, 2, 3, 'n') is not False 39 assert [1, 2, 3, 'n'] is not False 40 41 42# NOTESthe test passes.
I add a note about lists
42# NOTES 43# a dictionary is not None 44# a set is not None 45# a list is not False 46# a list is not None 47# a tuple is not False 48# a tuple is not None 49# a string is not False 50# a string is not None 51# a float is not False 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 set is False
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 assert 'a string' is not False 38 assert (1, 2, 3, 'n') is not False 39 assert [1, 2, 3, 'n'] is not False 40 assert {1, 2, 3, 'n'} is False 41 42 43# NOTESthe terminal shows AssertionError
E AssertionError: assert {1, 2, 3, 'n'} is FalseI change the statement to make it True
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 assert 'a string' is not False 38 assert (1, 2, 3, 'n') is not False 39 assert [1, 2, 3, 'n'] is not False 40 assert {1, 2, 3, 'n'} is not False 41 42 43# NOTESthe test passes.
I add a note about sets
43# NOTES 44# a dictionary is not None 45# a set is not False 46# a set is not None 47# a list is not False 48# a list is not None 49# a tuple is not False 50# a tuple is not None 51# a string is not False 52# a string is not None 53# a float is not False 54# a float is not None 55# an integer is not False 56# an integer is not None 57# True is not False 58# True is not None 59# False is False 60# False is not None 61# None is not False 62# None is None
I add an assert statement to see if a dictionary (any key-value pairs in curly braces
{ }separated by a comma) is False31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 assert 'a string' is not False 38 assert (1, 2, 3, 'n') is not False 39 assert [1, 2, 3, 'n'] is not False 40 assert {1, 2, 3, 'n'} is not False 41 assert {'key': 'value'} is False 42 43 44# NOTESthe terminal shows AssertionError
E AssertionError: assert {'key': 'value'} is FalseI change the statement to make it True
31 def test_assertion_error_w_false(self): 32 assert None is not False 33 assert False is False 34 assert True is not False 35 assert 0 is not False 36 assert 0.0 is not False 37 assert 'a string' is not False 38 assert (1, 2, 3, 'n') is not False 39 assert [1, 2, 3, 'n'] is not False 40 assert {1, 2, 3, 'n'} is not False 41 assert {'key': 'value'} is not False 42 43 44# NOTESthe test passes.
I add a note about dictionaries
44# NOTES 45# a dictionary is not False 46# a dictionary is not None 47# a set is not False 48# a set is not None 49# a list is not False 50# a list is not None 51# a tuple is not False 52# a tuple is not None 53# a string is not False 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 65 66 67# Exceptions seen 68# AssertionErrorI add a git commit message in the other terminal
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_true
True is the other boolean and is NOT None. I can use assertions to test if something is True or not.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion to see if None is True, in
test_assertion_error.pyin the editor41 assert {'key': 'value'} is not False 42 43 def test_assertion_error_w_true(self): 44 assert None is True 45 46 47# NOTESthe terminal is my friend, and shows AssertionError
E assert None is True
GREEN: make it pass
I change the assertion to make it True
43 def test_assertion_error_w_true(self):
44 assert None is not True
45
46
47 # NOTES
the test passes.
REFACTOR: make it better
I add a note about None
47# NOTES 48# a dictionary is not False 49# a dictionary is not None 50# a set is not False 51# a set is not None 52# a list is not False 53# a list is not None 54# a tuple is not False 55# a tuple is not None 56# a string is not False 57# a string is not None 58# a float is not False 59# a float is not None 60# an integer is not False 61# an integer is not None 62# True is not False 63# True is not None 64# False is False 65# False is not None 66# None is not True 67# None is not False 68# None is None
I add an assert statement to see if False is True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is True 46 47 48# NOTESthe terminal shows AssertionError
E assert False is TrueI change the statement to make it True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 47 48# NOTESthe test passes.
I add a note about False
48# NOTES 49# a dictionary is not False 50# a dictionary is not None 51# a set is not False 52# a set is not None 53# a list is not False 54# a list is not None 55# a tuple is not False 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 not True 66# False is False 67# False is not None 68# None is not True 69# None is not False 70# None is None
I add an assert statement about True, that will fail
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is not True 47 48 49# NOTESthe terminal shows AssertionError
E assert True is not TrueI change the statement to make it True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 48 49# NOTESthe test passes.
I add a note about True
49# NOTES 50# a dictionary is not False 51# a dictionary is not None 52# a set is not False 53# a set is not None 54# a list is not False 55# a list is not None 56# a tuple is not False 57# a tuple is not None 58# a string is not False 59# a string is not None 60# a float is not False 61# a float is not None 62# an integer is not False 63# an integer is not None 64# True is True 65# True is not False 66# True is not None 67# False is not True 68# False is False 69# False is not None 70# None is not True 71# None is not False 72# None is None
I add an assert statement to see if an integer (a whole number with no decimals) is True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is True 48 49 50# NOTESthe terminal shows AssertionError
E assert 0 is TrueI change the statement to make it True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 49 50# NOTESthe test passes.
I add a note about integers
50# NOTES 51# a dictionary is not False 52# a dictionary is not None 53# a set is not False 54# a set is not None 55# a list is not False 56# a list is not None 57# a tuple is not False 58# a tuple is not None 59# a string is not False 60# a string is not None 61# a float is not False 62# a float is not None 63# an integer is not True 64# an integer is not False 65# an integer is not None 66# True is True 67# True is not False 68# True is not None 69# False is not True 70# False is False 71# False is not None 72# None is not True 73# None is not False 74# None is None
I add an assert statement to see if a float (binary floating point decimal number) is True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is True 49 50 51# NOTESthe terminal shows AssertionError
E assert 0.0 is TrueI change the statement to make it True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 50 51# NOTESthe test passes.
I add a note about floats
51# NOTES 52# a dictionary is not False 53# a dictionary is not None 54# a set is not False 55# a set is not None 56# a list is not False 57# a list is not None 58# a tuple is not False 59# a tuple is not None 60# a string is not False 61# a string is not None 62# a float is not True 63# a float is not False 64# a float is not None 65# an integer is not True 66# an integer is not False 67# an integer is not None 68# True is True 69# True is not False 70# True is not None 71# False is not True 72# False is False 73# False is not None 74# None is not True 75# None is not False 76# None is None
I add an assert statement to see if a string (anything in quotes) is True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 assert 'a string' is True 50 51 52# NOTESthe terminal shows AssertionError
E AssertionError: assert 'a string' is TrueI change the statement to make it True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 assert 'a string' is not True 50 51 52# NOTESthe test passes.
I add a note about strings
52# NOTES 53# a dictionary is not False 54# a dictionary is not None 55# a set is not False 56# a set is not None 57# a list is not False 58# a list is not None 59# a tuple is not False 60# a tuple is not None 61# a string is not True 62# a string is not False 63# a string is not None 64# a float is not True 65# a float is not False 66# a float is not None 67# an integer is not True 68# an integer is not False 69# an integer is not None 70# True is True 71# True is not False 72# True is not None 73# False is not True 74# False is False 75# False is not None 76# None is not True 77# None is not False 78# None is None
I add an assert statement to see if a tuple (anything in parentheses
( )separated by a comma) is True43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 assert 'a string' is not True 50 assert (1, 2, 3, 'n') is True 51 52 53# NOTESthe terminal shows AssertionError
E AssertionError: assert (1, 2, 3, 'n') is TrueI change the statement to make it True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 assert 'a string' is not True 50 assert (1, 2, 3, 'n') is not True 51 52 53# NOTESthe test passes.
I add a note about tuples
53# NOTES 54# a dictionary is not False 55# a dictionary is not None 56# a set is not False 57# a set is not None 58# a list is not False 59# a list is not None 60# a tuple is not True 61# a tuple is not False 62# a tuple is not None 63# a string is not True 64# a string is not False 65# a string is not None 66# a float is not True 67# a float is not False 68# a float is not None 69# an integer is not True 70# an integer is not False 71# an integer is not None 72# True is True 73# True is not False 74# True is not None 75# False is not True 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 a list (anything in square brackets
[ ]) is True43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 assert 'a string' is not True 50 assert (1, 2, 3, 'n') is not True 51 assert [1, 2, 3, 'n'] is True 52 53 54# NOTESthe terminal shows AssertionError
E AssertionError: assert [1, 2, 3, 'n'] is TrueI change the statement to make it True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 assert 'a string' is not True 50 assert (1, 2, 3, 'n') is not True 51 assert [1, 2, 3, 'n'] is not True 52 53 54# NOTESthe test passes.
I add a note about lists
54# NOTES 55# a dictionary is not False 56# a dictionary is not None 57# a set is not False 58# a set is not None 59# a list is not True 60# a list is not False 61# a list is not None 62# a tuple is not True 63# a tuple is not False 64# a tuple is not None 65# a string is not True 66# a string is not False 67# a string is not None 68# a float is not True 69# a float is not False 70# a float is not None 71# an integer is not True 72# an integer is not False 73# an integer is not None 74# True is True 75# True is not False 76# True is not None 77# False is not True 78# False is False 79# False is not None 80# None is not True 81# None is not False 82# None is None
I add an assert statement to see if a set is True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 assert 'a string' is not True 50 assert (1, 2, 3, 'n') is not True 51 assert [1, 2, 3, 'n'] is not True 52 assert {1, 2, 3, 'n'} is True 53 54 55# NOTESthe terminal shows AssertionError
E AssertionError: assert {1, 2, 3, 'n'} is TrueI change the statement to make it True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 assert 'a string' is not True 50 assert (1, 2, 3, 'n') is not True 51 assert [1, 2, 3, 'n'] is not True 52 assert {1, 2, 3, 'n'} is not True 53 54 55# NOTESthe test passes.
I add a note about sets
55# NOTES 56# a dictionary is not False 57# a dictionary is not None 58# a set is not True 59# a set is not False 60# a set is not None 61# a list is not True 62# a list is not False 63# a list is not None 64# a tuple is not True 65# a tuple is not False 66# a tuple is not None 67# a string is not True 68# a string is not False 69# a string is not None 70# a float is not True 71# a float is not False 72# a float is not None 73# an integer is not True 74# an integer is not False 75# an integer is not None 76# True is True 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 to see if a dictionary (any key-value pairs in curly braces
{ }separated by a comma) is True43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 assert 'a string' is not True 50 assert (1, 2, 3, 'n') is not True 51 assert [1, 2, 3, 'n'] is not True 52 assert {1, 2, 3, 'n'} is not True 53 assert {'key': 'value'} is True 54 55 56# NOTESthe terminal shows AssertionError
E AssertionError: assert {'key': 'value'} is TrueI change the statement to make it True
43 def test_assertion_error_w_true(self): 44 assert None is not True 45 assert False is not True 46 assert True is True 47 assert 0 is not True 48 assert 0.0 is not True 49 assert 'a string' is not True 50 assert (1, 2, 3, 'n') is not True 51 assert [1, 2, 3, 'n'] is not True 52 assert {1, 2, 3, 'n'} is not True 53 assert {'key': 'value'} is not True 54 55 56# NOTESthe test passes.
I add a note about dictionaries
56# NOTES 57# a dictionary is not True 58# a dictionary is not False 59# a dictionary is not None 60# a set is not True 61# a set is not False 62# a set is not None 63# a list is not True 64# a list is not False 65# a list is not None 66# a tuple is not True 67# a tuple is not False 68# a tuple is not None 69# a string is not True 70# a string is not False 71# a string is not None 72# a float is not True 73# a float is not False 74# a float is not None 75# an integer is not True 76# an integer is not False 77# an integer is not None 78# True is True 79# True is not False 80# True is not None 81# False is not True 82# False is False 83# False is not None 84# None is not True 85# None is not False 86# None is None 87 88 89# Exceptions seen 90# AssertionErrorI add a git commit message in the other terminal
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_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 use assertions to test if 2 things are equal, like I did in test_what_is_an_assertion
RED: make it fail
I go back to the terminal where the tests are running
I add a new test with an assertion to see if None is NOT equal to None, in
test_assertion_error.pyin the editorNote
!=is !+= on the keyboard and is the symbol forNOT equal53 assert {'key': 'value'} is not True 54 55 def test_assertion_error_w_equality(self): 56 assert None != None 57 58 59# NOTESthe terminal is my friend, and shows AssertionError
E assert None != Nonebecause
None is NOT equal to Noneis False
GREEN: make it pass
Note
== is =+= on the keyboard and is the symbol for is equal
I change the assertion to make it True
55 def test_assertion_error_w_equality(self):
56 assert None == None
57
58
59 # NOTES
the test passes because None is equal to None is True
REFACTOR: make it better
I add to the
None is Nonenote59# NOTES 60# a dictionary is not True 61# a dictionary is not False 62# a dictionary is not None 63# a set is not True 64# a set is not False 65# a set is not None 66# a list is not True 67# a list is not False 68# a list is not None 69# a tuple is not True 70# a tuple is not False 71# a tuple is not None 72# a string is not True 73# a string is not False 74# a string is not None 75# a float is not True 76# a float is not False 77# a float is not None 78# an integer is not True 79# an integer is not False 80# an integer is not None 81# True is True 82# True is not False 83# True is not None 84# False is not True 85# False is False 86# False is not None 87# None is not True 88# None is not False 89# None is None and equal to NoneI add an assertion to compare False with None
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 assert False == None 58 59 60# NOTESthe terminal is my friend, and shows AssertionError
E assert False == NoneI change the assert statement to make it True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 assert False != None 58 59 60# NOTESthe test passes.
I add to the
False is not NoneandNone is not Falsenotes because equality goes both ways60# NOTES 61# a dictionary is not True 62# a dictionary is not False 63# a dictionary is not None 64# a set is not True 65# a set is not False 66# a set is not None 67# a list is not True 68# a list is not False 69# a list is not None 70# a tuple is not True 71# a tuple is not False 72# a tuple is not None 73# a string is not True 74# a string is not False 75# a string is not None 76# a float is not True 77# a float is not False 78# a float is not None 79# an integer is not True 80# an integer is not False 81# an integer is not None 82# True is True 83# True is not False 84# True is not None 85# False is not True 86# False is False 87# False is not None and NOT equal to None 88# None is not True 89# None is not False and NOT equal to False 90# None is None and equal to NoneI add an assertion to compare False with True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 assert False != None 58 assert False == True 59 60 61# NOTESthe terminal is my friend, and shows AssertionError
E assert False == TrueI change the assert statement to make it True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 assert False != None 58 assert False != True 59 60 61# NOTESthe test passes.
I add to the
False is not TrueandTrue is not Falsenotes because equality goes both ways61# NOTES 62# a dictionary is not True 63# a dictionary is not False 64# a dictionary is not None 65# a set is not True 66# a set is not False 67# a set is not None 68# a list is not True 69# a list is not False 70# a list is not None 71# a tuple is not True 72# a tuple is not False 73# a tuple is not None 74# a string is not True 75# a string is not False 76# a string is not None 77# a float is not True 78# a float is not False 79# a float is not None 80# an integer is not True 81# an integer is not False 82# an integer is not None 83# True is True 84# True is not False and NOT equal to False 85# True is not None 86# False is not True and NOT equal to True 87# False is False 88# False is not None and NOT equal to None 89# None is not True 90# None is not False and NOT equal to False 91# None is None and equal to NoneI add an assertion to compare False with itself
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 assert False != None 58 assert False != True 59 assert False != False 60 61 62# NOTESthe terminal is my friend, and shows AssertionError
E assert False != FalseI make the statement True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 assert False != None 58 assert False != True 59 assert False == False 60 61 62# NOTESthe test passes.
I add to the
False is Falsenote62# NOTES 63# a dictionary is not True 64# a dictionary is not False 65# a dictionary is not None 66# a set is not True 67# a set is not False 68# a set is not None 69# a list is not True 70# a list is not False 71# a list is not None 72# a tuple is not True 73# a tuple is not False 74# a tuple is not None 75# a string is not True 76# a string is not False 77# a string is not None 78# a float is not True 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 and NOT equal to False 86# True is not None 87# False is not True and NOT equal to True 88# False is False and equal to False 89# False is not None and NOT equal to None 90# None is not True 91# None is not False and NOT equal to False 92# None is None and equal to None
I add a new failing assertion to compare True with None
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 assert False != None 58 assert False != True 59 assert False == False 60 assert True == None 61 62 63# NOTESthe terminal is my friend, and shows AssertionError
E assert True == NoneI change the assert statement to make it True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 assert False != None 58 assert False != True 59 assert False == False 60 assert True != None 61 62 63# NOTESthe test passes.
I add to the
True is not NoneandNone is not Truenotes because equality goes both ways69# NOTES 70# a dictionary is not True 71# a dictionary is not False 72# a dictionary is not None 73# a set is not True 74# a set is not False 75# a set is not None 76# a list is not True 77# a list is not False 78# a list is not None 79# a tuple is not True 80# a tuple is not False 81# a tuple is not None 82# a string is not True 83# a string is not False 84# a string is not None 85# a float is not True 86# a float is not False 87# a float is not None 88# an integer is not True 89# an integer is not False 90# an integer is not None 91# True is True 92# True is not False and NOT equal to False 93# True is not None and NOT equal to None 94# False is not True and NOT equal to True 95# False is False and equal to False 96# False is not None and NOT equal to None 97# None is not True and NOT equal to True 98# None is not False and NOT equal to False 99# None is None and equal to NoneI add an assertion to compare True with itself
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 assert False != None 58 assert False != True 59 assert False == False 60 assert True != None 61 assert True != True 62 63 64# NOTESthe terminal is my friend, and shows AssertionError
E assert True != TrueI make the statement True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 assert False != None 58 assert False != True 59 assert False == False 60 assert True != None 61 assert True == True 62 63 64# NOTESthe test passes.
I add to the
True is Truenote52# NOTES 53# a dictionary is not True 54# a dictionary is not False 55# a dictionary is not None 56# a set is not True 57# a set is not False 58# a set is not None 59# a list is not True 60# a list is not False 61# a list is not None 62# a tuple is not True 63# a tuple is not False 64# a tuple is not None 65# a string is not True 66# a string is not False 67# a string is not None 68# a float is not True 69# a float is not False 70# a float is not None 71# an integer is not True 72# an integer is not False 73# an integer is not None 74# True is True and equal to True 75# True is not False and NOT equal to False 76# True is not None and NOT equal to None 77# False is not True and NOT equal to True 78# False is False and equal to False 79# False is not None and NOT equal to None 80# None is not True and NOT equal to True 81# None is not False and NOT equal to False 82# None is None and equal to NoneI add a git commit message in the other terminal
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.
how I test if two things are NOT Equal
I can also use assert methods from the unittest.TestCase class to test if 2 things are equal or not.
I go back to the terminal where the tests are running
I add an assertion with the assertNotEqual method which checks if the 2 things in the parentheses are NOT equal, in
test_assertion_error.pyin the editor55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertNotEqual(None, None) 58 59 assert False != None 60 assert False != True 61 assert False == False 62 assert True != None 63 assert True == True 64 65 66# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: None == Nonebecause None is equal to None, they are the same object. Compare this error message with the one for
assert None != NoneE assert None != Nonewhich do you like better?
these two statements check the same thing
assert None != None self.assertNotEqual(None, None)they are asking the same question:
is None NOT equal to None?or giving Python the commandDO NOT CONTINUE if "None is NOT Equal to None" is False
how I test if two things are Equal
I change the assertNotEqual method to the assertEqual method which checks if the 2 things in the parentheses are equal
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 assert False != True 61 assert False == False 62 assert True != None 63 assert True == True 64 65 66# NOTESI use assertEqual to compare False with None
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 self.assertEqual(False, None) 61 62 assert False != True 63 assert False == False 64 assert True != None 65 assert True == True 66 67 68# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: False != Nonecompare this error message with the one for
assert False == NoneE assert False == NoneI change assertEqual to assertNotEqual to make the statement True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 self.assertNotEqual(False, None) 61 62 assert False != True 63 assert False == False 64 assert True != None 65 assert True == True 66 67 68# NOTESthe test passes.
I use assertEqual to compare False with True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 self.assertNotEqual(False, None) 61 62 assert False != True 63 self.assertEqual(False, True) 64 65 assert False == False 66 assert True != None 67 assert True == True 68 69 70# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: False != Truecompare this error message with the one for
assert False == TrueE assert False == TrueI change assertEqual to assertNotEqual to make the statement True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 self.assertNotEqual(False, None) 61 62 assert False != True 63 self.assertNotEqual(False, True) 64 65 assert False == False 66 assert True != None 67 assert True == True 68 69 70# NOTESthe test passes.
I use assertNotEqual to compare False with itself
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 self.assertNotEqual(False, None) 61 62 assert False != True 63 self.assertNotEqual(False, True) 64 65 assert False == False 66 self.assertNotEqual(False, False) 67 68 assert True != None 69 assert True == True 70 71 72# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: False == Falsecompare this error message with the one for
assert False != FalseE assert False != FalseI change assertNotEqual to assertEqual to make the statement True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 self.assertNotEqual(False, None) 61 62 assert False != True 63 self.assertNotEqual(False, True) 64 65 assert False == False 66 self.assertEqual(False, False) 67 68 assert True != None 69 assert True == True 70 71 72# NOTESthe test passes.
I use assertEqual to compare True with None
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 self.assertNotEqual(False, None) 61 62 assert False != True 63 self.assertNotEqual(False, True) 64 65 assert False == False 66 self.assertEqual(False, False) 67 68 assert True != None 69 self.assertEqual(True, None) 70 71 assert True == True 72 73 74# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: True != Nonecompare this error message with the one for
assert True == NoneE assert True == NoneI change assertEqual to assertNotEqual to make the statement True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 self.assertNotEqual(False, None) 61 62 assert False != True 63 self.assertNotEqual(False, True) 64 65 assert False == False 66 self.assertEqual(False, False) 67 68 assert True != None 69 self.assertNotEqual(True, None) 70 71 assert True == True 72 73 74# NOTESthe test passes.
I use assertNotEqual to compare True with itself
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 self.assertNotEqual(False, None) 61 62 assert False != True 63 self.assertNotEqual(False, True) 64 65 assert False == False 66 self.assertEqual(False, False) 67 68 assert True != None 69 self.assertNotEqual(True, None) 70 71 assert True == True 72 self.assertNotEqual(True, True) 73 74 75# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: True == Truecompare this error message with the one for
assert True != TrueE assert True != TrueI change assertNotEqual to assertEqual to make the statement True
55 def test_assertion_error_w_equality(self): 56 assert None == None 57 self.assertEqual(None, None) 58 59 assert False != None 60 self.assertNotEqual(False, None) 61 62 assert False != True 63 self.assertNotEqual(False, True) 64 65 assert False == False 66 self.assertEqual(False, False) 67 68 assert True != None 69 self.assertNotEqual(True, None) 70 71 assert True == True 72 self.assertEqual(True, True) 73 74 75# NOTESthe test passes.
I add a git commit message in the other terminal
git commit --all --message 'add assert methods'the terminal shows a summary of the changes then goes back to the command line.
I go back to the terminal where the tests are running
I use the assertNotEqual method in test_what_is_an_assertion, in
test_assertion_error.pyin the editor6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 self.assertNotEqual( 11 reality, my_expectation 12 ) 13 14 reality = '1' + '1' 15 my_expectation = '11' 16 assert reality == my_expectation 17 18 reality = 'I am' + ' alive' 19 my_expectation = 'I am alive' 20 assert reality == my_expectation 21 22 def test_assertion_error_w_none(self):the terminal is my friend, and shows AssertionError
E AssertionError: 2 == 2it shows what the True statement is
I change assertNotEqual to assertEqual to make the statement True
6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 self.assertEqual( 11 reality, my_expectation 12 ) 13 14 reality = '1' + '1' 15 my_expectation = '11' 16 assert reality == my_expectation 17 18 reality = 'I am' + ' alive' 19 my_expectation = 'I am alive' 20 assert reality == my_expectation 21 22 def test_assertion_error_w_none(self):the test passes.
I add a call to assertNotEqual for the next assertion
6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 self.assertEqual( 11 reality, my_expectation 12 ) 13 14 reality = '1' + '1' 15 my_expectation = '11' 16 assert reality == my_expectation 17 self.assertNotEqual( 18 reality, my_expectation 19 ) 20 21 reality = 'I am' + ' alive' 22 my_expectation = 'I am alive' 23 assert reality == my_expectation 24 25 def test_assertion_error_w_none(self):the terminal is my friend, and shows
E AssertionError: '11' == '11'it shows what the True statement is
I change assertNotEqual to assertEqual to make the statement True
6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 self.assertEqual( 11 reality, my_expectation 12 ) 13 14 reality = '1' + '1' 15 my_expectation = '11' 16 assert reality == my_expectation 17 self.assertEqual( 18 reality, my_expectation 19 ) 20 21 reality = 'I am' + ' alive' 22 my_expectation = 'I am alive' 23 assert reality == my_expectation 24 25 def test_assertion_error_w_none(self):the test passes.
I add a call to assertNotEqual for the last assertion in test_what_is_an_assertion
6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 self.assertEqual( 11 reality, my_expectation 12 ) 13 14 reality = '1' + '1' 15 my_expectation = '11' 16 assert reality == my_expectation 17 self.assertEqual( 18 reality, my_expectation 19 ) 20 21 reality = 'I am' + ' alive' 22 my_expectation = 'I am alive' 23 assert reality == my_expectation 24 self.assertNotEqual( 25 reality, my_expectation 26 ) 27 28 def test_assertion_error_w_none(self):the terminal is my friend, and shows AssertionError
E AssertionError: 'I am alive' == 'I am alive'it shows what the True statement is
I change assertNotEqual to assertEqual to make the statement True
6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 assert reality == my_expectation 10 self.assertEqual( 11 reality, my_expectation 12 ) 13 14 reality = '1' + '1' 15 my_expectation = '11' 16 assert reality == my_expectation 17 self.assertEqual( 18 reality, my_expectation 19 ) 20 21 reality = 'I am' + ' alive' 22 my_expectation = 'I am alive' 23 assert reality == my_expectation 24 self.assertEqual( 25 reality, my_expectation 26 ) 27 28 def test_assertion_error_w_none(self):the test passes.
I add a git commit message in the other terminal
git commit --all --message 'add assertEqual'the terminal shows a summary of the changes then goes back to the command line.
close the project
I close
assertion_error.pyin the editorI click in the terminal where the tests are running, then 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_errorcd ..the terminal is my friend, and shows
.../pumping_pythonI am back in the
pumping_pythondirectory
review
I can use assert statements and assert methods to test if something is
and to test if 2 things are
NOT Equal with assertNotEqual
Equal with assertEqual
code from the chapter
what is next?
Congratulations! You now know
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.