AssertionError 2: use the setUp method
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 if 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 if 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 # my_expectation = 11
10 # reality == my_expectation
11 assert reality == my_expectation
12 # self.assertNotEqual(reality, my_expectation)
13 self.assertEqual(reality, my_expectation)
14
15 reality = '1' + '1'
16 # my_expectation = '2'
17 my_expectation = '11'
18 # reality == my_expectation
19 assert reality == my_expectation
20 # self.assertNotEqual(reality, my_expectation)
21 self.assertEqual(reality, my_expectation)
22
23 reality = 'I am' + ' alive'
24 # my_expectation = '11'
25 my_expectation = 'I am alive'
26 # reality == my_expectation
27 assert reality == my_expectation
28 # self.assertNotEqual(reality, my_expectation)
29 self.assertEqual(reality, my_expectation)
30
31 def test_assertion_error_w_none(self):
32 # assert None is not None
33 assert None is None
34 # self.assertIsNot(None, None)
35 self.assertIs(None, None)
36
37 # assert False is None
38 assert False is not None
39 # self.assertIs(False, None)
40 self.assertIsNot(False, None)
41
42 # assert True is None
43 assert True is not None
44 # self.assertIs(True, None)
45 self.assertIsNot(True, None)
46
47 # assert 0 is None
48 assert 0 is not None
49 # self.assertIs(0, None)
50 self.assertIsNot(0, None)
51
52 # assert 0.0 is None
53 assert 0.0 is not None
54 # self.assertIs(0.0, None)
55 self.assertIsNot(0.0, None)
56
57 # assert 'a string' is None
58 assert 'a string' is not None
59 # self.assertIs('a string', None)
60 self.assertIsNot('a string', None)
61
62 # assert (1, 2, 3, 'n') is None
63 assert (1, 2, 3, 'n') is not None
64 # self.assertIs((1, 2, 3, 'n'), None)
65 self.assertIsNot((1, 2, 3, 'n'), None)
66
67 # assert [1, 2, 3, 'n'] is None
68 assert [1, 2, 3, 'n'] is not None
69 # self.assertIs([1, 2, 3, 'n'], None)
70 self.assertIsNot([1, 2, 3, 'n'], None)
71
72 # assert {1, 2, 3, 'n'} is None
73 assert {1, 2, 3, 'n'} is not None
74 # self.assertIs({1, 2, 3, 'n'}, None)
75 self.assertIsNot({1, 2, 3, 'n'}, None)
76
77 # assert {'key': 'value'} is None
78 assert {'key': 'value'} is not None
79 # self.assertIs({'key': 'value'}, None)
80 self.assertIsNot({'key': 'value'}, None)
81
82 def test_assertion_error_w_false(self):
83 # assert None is False
84 assert None is not False
85 # self.assertIs(None, False)
86 self.assertIsNot(None, False)
87
88 # assert False is not False
89 assert False is False
90 # self.assertIsNot(False, False)
91 self.assertIs(False, False)
92
93 # assert True is False
94 assert True is not False
95 # self.assertIs(True, False)
96 self.assertIsNot(True, False)
97
98 # assert 0 is False
99 assert 0 is not False
100 # self.assertIs(0, False)
101 self.assertIsNot(0, False)
102
103 # assert 0.0 is False
104 assert 0.0 is not False
105 # self.assertIs(0.0, False)
106 self.assertIsNot(0.0, False)
107
108 # assert 'a string' is False
109 assert 'a string' is not False
110 # self.assertIs('a string', False)
111 self.assertIsNot('a string', False)
112
113 # assert (1, 2, 3, 'n') is False
114 assert (1, 2, 3, 'n') is not False
115 # self.assertIs((1, 2, 3, 'n'), False)
116 self.assertIsNot((1, 2, 3, 'n'), False)
117
118 # assert [1, 2, 3, 'n'] is False
119 assert [1, 2, 3, 'n'] is not False
120 # self.assertIs([1, 2, 3, 'n'], False)
121 self.assertIsNot([1, 2, 3, 'n'], False)
122
123 # assert {1, 2, 3, 'n'} is False
124 assert {1, 2, 3, 'n'} is not False
125 # self.assertIs({1, 2, 3, 'n'}, False)
126 self.assertIsNot({1, 2, 3, 'n'}, False)
127
128 # assert {'key': 'value'} is False
129 assert {'key': 'value'} is not False
130 # self.assertIs({'key': 'value'}, False)
131 self.assertIsNot({'key': 'value'}, False)
132
133 def test_assertion_error_w_true(self):
134 # assert None is True
135 assert None is not True
136 # self.assertIs(None, True)
137 self.assertIsNot(None, True)
138
139 # assert False is True
140 assert False is not True
141 # self.assertIs(False, True)
142 self.assertIsNot(False, True)
143
144 # assert True is not True
145 assert True is True
146 # self.assertIsNot(True, True)
147 self.assertIs(True, True)
148
149 # assert 0 is True
150 assert 0 is not True
151 # self.assertIs(0, True)
152 self.assertIsNot(0, True)
153
154 # assert 0.0 is True
155 assert 0.0 is not True
156 # self.assertIs(0.0, True)
157 self.assertIsNot(0.0, True)
158
159 # assert 'a string' is True
160 assert 'a string' is not True
161 # self.assertIs('a string', True)
162 self.assertIsNot('a string', True)
163
164 # assert (1, 2, 3, 'n') is True
165 assert (1, 2, 3, 'n') is not True
166 # self.assertIs((1, 2, 3, 'n'), True)
167 self.assertIsNot((1, 2, 3, 'n'), True)
168
169 # assert [1, 2, 3, 'n'] is True
170 assert [1, 2, 3, 'n'] is not True
171 # self.assertIs([1, 2, 3, 'n'], True)
172 self.assertIsNot([1, 2, 3, 'n'], True)
173
174 # assert {1, 2, 3, 'n'} is True
175 assert {1, 2, 3, 'n'} is not True
176 # self.assertIs({1, 2, 3, 'n'}, True)
177 self.assertIsNot({1, 2, 3, 'n'}, True)
178
179 # assert {'key': 'value'} is True
180 assert {'key': 'value'} is not True
181 # self.assertIs({'key': 'value'}, True)
182 self.assertIsNot({'key': 'value'}, True)
183
184 def test_assertion_error_w_equality(self):
185 # assert None != None
186 assert None == None
187 # self.assertNotEqual(None, None)
188 self.assertEqual(None, None)
189
190 # assert False == None
191 assert False != None
192 # self.assertEqual(False, None)
193 self.assertNotEqual(False, None)
194
195 # assert False == True
196 assert False != True
197 # self.assertEqual(False, True)
198 self.assertNotEqual(False, True)
199
200 # assert False != False
201 assert False == False
202 # self.assertNotEqual(False, False)
203 self.assertEqual(False, False)
204
205 # assert True == None
206 assert True != None
207 # self.assertEqual(True, None)
208 self.assertNotEqual(True, None)
209
210 # assert True != True
211 assert True == True
212 # self.assertNotEqual(True, True)
213 self.assertEqual(True, True)
214
215
216# NOTES
217# a dictionary is not True
218# a dictionary is not False
219# a dictionary is not None
220# a set is not True
221# a set is not False
222# a set is not None
223# a list is not True
224# a list is not False
225# a list is not None
226# a tuple is not True
227# a tuple is not False
228# a tuple is not None
229# a string is not True
230# a string is not False
231# a string is not None
232# a float is not True
233# a float is not False
234# a float is not None
235# an integer is not True
236# an integer is not False
237# an integer is not None
238# True is True and equal to True
239# True is not False and NOT equal to False
240# True is not None and NOT equal to None
241# False is not True and NOT equal to True
242# False is False and equal to False
243# False is not None and NOT equal to None
244# None is not True and NOT equal to True
245# None is not False and NOT equal to False
246# None is None and equal to None
247
248
249# Exceptions seen
250# AssertionError
questions about AssertionError
Questions to think about as I go through the chapter
how can I test if something is NOT the same object as False?
what is another way to test if something is the same object as None?
what is another way to test if something is NOT the same object as None?
what is another way to test if something is the same object as False?
what is another way to test if something is NOT the same object as False?
what is another way to test if something is NOT the same object as True?
what is another way to test if something is NOT the same object as True?
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 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 use the mv program to change the name of
main.pytotest_assertion_error.pyand move it to thetestsfoldermv main.py tests/test_assertion_error.pyMove-Item main.py 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 delete the text then 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?
A variable is a name that is used for values that change. For example, in Mathematics we use x to represent any number.
I can use variables to remove repetition of values that change so that if I have to change a value, I only have to make the change in one place instead of many.
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 = 2 9 my_expectation = 11 10 reality == my_expectationwhy is the test still green?
I want the test to fail if 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 = 2 9 my_expectation = 11 10 # reality == my_expectation 11 assert reality == my_expectation 12 13 14# Exceptions seenthe 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 back, 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 # my_expectation = 11
10 # reality == my_expectation
11 assert reality == my_expectation
12
13
14 # Exceptions seen
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 # my_expectation = 11 10 # reality == my_expectation 11 assert reality == my_expectation 12 13 reality = '1' + '1' 14 my_expectation = '2' 15 reality == my_expectation 16 17 18# Exceptions seenthe test is still green because 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 # my_expectation = 11 10 # reality == my_expectation 11 assert reality == my_expectation 12 13 reality = '1' + '1' 14 my_expectation = '2' 15 # reality == my_expectation 16 assert reality == my_expectation 17 18 19# Exceptions seenthe terminal is my friend, and shows AssertionError
E AssertionError: assert '11' == '2'because the assert before the statement makes it 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 # my_expectation = 11 10 # reality == my_expectation 11 assert reality == my_expectation 12 13 reality = '1' + '1' 14 # my_expectation = '2' 15 my_expectation = '11' 16 # reality == my_expectation 17 assert reality == my_expectation 18 19 20# Exceptions seenthe test passes because the statement is now True,
'1' + '1'is equal to'11'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 # my_expectation = 11 10 # reality == my_expectation 11 assert reality == my_expectation 12 13 reality = '1' + '1' 14 # my_expectation = '2' 15 my_expectation = '11' 16 # reality == my_expectation 17 assert reality == my_expectation 18 19 reality = 'I am' + ' alive' 20 my_expectation = '11' 21 reality == my_expectation 22 23 24# 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 # my_expectation = 11 10 # reality == my_expectation 11 assert reality == my_expectation 12 13 reality = '1' + '1' 14 # my_expectation = '2' 15 my_expectation = '11' 16 # reality == my_expectation 17 assert reality == my_expectation 18 19 reality = 'I am' + ' alive' 20 my_expectation = '11' 21 # reality == my_expectation 22 assert reality == my_expectation 23 24 25# Exceptions seenthe 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 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 # my_expectation = 11 10 # reality == my_expectation 11 assert reality == my_expectation 12 13 reality = '1' + '1' 14 # my_expectation = '2' 15 my_expectation = '11' 16 # reality == my_expectation 17 assert reality == my_expectation 18 19 reality = 'I am' + ' alive' 20 # my_expectation = '11' 21 my_expectation = 'I am alive' 22 # reality == my_expectation 23 assert reality == my_expectation 24 25 26# 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 if the statement after assert is False. It was in 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”.
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 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 failing test to
test_assertion_error.pyin the editor19 reality = 'I am' + ' alive' 20 # my_expectation = '11' 21 my_expectation = 'I am alive' 22 # reality == my_expectation 23 assert reality == my_expectation 24 25 def test_assertion_error_w_none(self): 26 assert None is not None 27 28 29# 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
25 def test_assertion_error_w_none(self):
26 # assert None is not None
27 assert None is None
28
29
30# Exceptions seen
the test passes.
REFACTOR: make it better
I add a note with what I learned from the experiment
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 29 30# NOTES 31# None is None 32 33 34# Exceptions seen 35# AssertionError
I add a new assertion to compare None with False, another simple data structure
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 assert False is None 29 30 31# NOTESthe terminal is my friend, and shows AssertionError
E assert False is NoneI change the assert statement to make it True
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 31 32# NOTESthe test passes.
I add a note about False
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 31 32# NOTES 33# False is not None 34# None is None 35 36 37# Exceptions seen 38# AssertionError
I add an assertion to compare None with True, another simple data structure
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 assert True is None 31 32 33# NOTESthe terminal is my friend, and shows AssertionError
E assert True is NoneI change the assert statement to make it True
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 33 34# NOTESthe test passes.
I add a note about True
34# NOTES 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 an integer (a whole number without decimals)
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 assert 0 is None 33 34 35# NOTESthe terminal is my friend, and shows AssertionError
E assert 0 is NoneI change the statement to make it True
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 35 36# NOTESthe test passes.
I add a note about integers
36# NOTES 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 float (binary floating point decimal number)
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 assert 0.0 is None 35 36 37# NOTESthe terminal is my friend, and shows AssertionError
E assert 0.0 is NoneI change the statement to make it True
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 37 38# NOTESthe test passes.
I add a note about floats
38# NOTES 39# a float is not None 40# an integer is not None 41# True is not None 42# False is not None 43# None is None 44 45 46# Exceptions seen 47# AssertionError
I add an assert statement for a string (anything in quotes)
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 assert 'a string' is None 37 38 39# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: assert 'a string' is NoneI change the statement to make it True
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 # assert 'a string' is None 37 assert 'a string' is not None 38 39 40# NOTESthe test passes.
I add a note about strings
40# NOTES 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 not None 46# None is None 47 48 49# Exceptions seen 50# AssertionError
I add an assert statement for a tuple (anything in parentheses
( )separated by a comma)25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 # assert 'a string' is None 37 assert 'a string' is not None 38 assert (1, 2, 3, 'n') is None 39 40 41# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: assert (1, 2, 3, 'n') is NoneI change the statement to make it True
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 # assert 'a string' is None 37 assert 'a string' is not None 38 # assert (1, 2, 3, 'n') is None 39 assert (1, 2, 3, 'n') is not None 40 41 42# NOTESthe test passes.
I add a note about tuples
42# NOTES 43# a tuple is not None 44# a string is not None 45# a float is not None 46# an integer is not None 47# True is not None 48# False is not None 49# None is None 50 51 52# Exceptions seen 53# AssertionError
I add an assert statement for a list (anything in square brackets
[ ])25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 # assert 'a string' is None 37 assert 'a string' is not None 38 # assert (1, 2, 3, 'n') is None 39 assert (1, 2, 3, 'n') is not None 40 assert [1, 2, 3, 'n'] is None 41 42 43# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: assert [1, 2, 3, 'n'] is NoneI change the statement to make it True
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 # assert 'a string' is None 37 assert 'a string' is not None 38 # assert (1, 2, 3, 'n') is None 39 assert (1, 2, 3, 'n') is not None 40 # assert [1, 2, 3, 'n'] is None 41 assert [1, 2, 3, 'n'] is not None 42 43 44# NOTESthe test passes.
I add a note about lists
44# NOTES 45# a list is not None 46# a tuple is not None 47# a string is not None 48# a float is not None 49# an integer is not None 50# True is not None 51# False is not None 52# None is None 53 54 55# Exceptions seen 56# AssertionError
I add an assert statement for a set (anything in curly braces
{ }separated by a comma)25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 # assert 'a string' is None 37 assert 'a string' is not None 38 # assert (1, 2, 3, 'n') is None 39 assert (1, 2, 3, 'n') is not None 40 # assert [1, 2, 3, 'n'] is None 41 assert [1, 2, 3, 'n'] is not None 42 assert {1, 2, 3, 'n'} is None 43 44 45# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: assert {1, 2, 3, 'n'} is NoneI change the statement to make it True
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 # assert 'a string' is None 37 assert 'a string' is not None 38 # assert (1, 2, 3, 'n') is None 39 assert (1, 2, 3, 'n') is not None 40 # assert [1, 2, 3, 'n'] is None 41 assert [1, 2, 3, 'n'] is not None 42 # assert {1, 2, 3, 'n'} is None 43 assert {1, 2, 3, 'n'} is not None 44 45 46# NOTESthe test passes.
I add a note about sets
46# NOTES 47# a set is not None 48# a list is not None 49# a tuple is not None 50# a string is not None 51# a float is not None 52# an integer is not None 53# True is not None 54# False is not None 55# None is None 56 57 58# Exceptions seen 59# AssertionError
I add an assert statement for a dictionary (any key-value pairs in curly braces
{ }separated by a comma)25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 # assert 'a string' is None 37 assert 'a string' is not None 38 # assert (1, 2, 3, 'n') is None 39 assert (1, 2, 3, 'n') is not None 40 # assert [1, 2, 3, 'n'] is None 41 assert [1, 2, 3, 'n'] is not None 42 # assert {1, 2, 3, 'n'} is None 43 assert {1, 2, 3, 'n'} is not None 44 assert {'key': 'value'} is None 45 46 47# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: assert {'key': 'value'} is NoneI change the statement to make it True
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # assert False is None 29 assert False is not None 30 # assert True is None 31 assert True is not None 32 # assert 0 is None 33 assert 0 is not None 34 # assert 0.0 is None 35 assert 0.0 is not None 36 # assert 'a string' is None 37 assert 'a string' is not None 38 # assert (1, 2, 3, 'n') is None 39 assert (1, 2, 3, 'n') is not None 40 # assert [1, 2, 3, 'n'] is None 41 assert [1, 2, 3, 'n'] is not None 42 # assert {1, 2, 3, 'n'} is None 43 assert {1, 2, 3, 'n'} is not None 44 # assert {'key': 'value'} is None 45 assert {'key': 'value'} is not None 46 47 48# NOTESthe test passes.
I add a note about dictionaries
48# NOTES 49# a dictionary is not None 50# a set is not None 51# a list is not None 52# a tuple is not None 53# a string is not None 54# a float is not None 55# an integer is not None 56# True is not None 57# False is not None 58# None is None 59 60 61# Exceptions seen 62# 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.
another way to test if something is NOT the same object as None
I can also use assert methods from the unittest.TestCase class to test if something is the same object as None or not.
I go back to the terminal where the tests are running
I add an assertion with the assertIsNot method which checks if the object on the left is NOT the same as the object on the right in the parentheses, in
test_assertion_error.py25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 self.assertIsNot(None, None) 29 30 # assert False is None 31 assert False is not None 32 # assert True is None 33 assert True is not None 34 # assert 0 is None 35 assert 0 is not None 36 # assert 0.0 is None 37 assert 0.0 is not None 38 # assert 'a string' is None 39 assert 'a string' is not None 40 # assert (1, 2, 3, 'n') is None 41 assert (1, 2, 3, 'n') is not None 42 # assert [1, 2, 3, 'n'] is None 43 assert [1, 2, 3, 'n'] is not None 44 # assert {1, 2, 3, 'n'} is None 45 assert {1, 2, 3, 'n'} is not None 46 # assert {'key': 'value'} is None 47 assert {'key': 'value'} is not None 48 49 50# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: unexpectedly identical: Nonebecause None is None, they are the same object. Compare this error message with the one for
assert None is not NoneE assert None is not Nonewhich do you like better?
these two statements check the same thing
assert None is not None self.assertIsNot(None, None)they are asking the same question:
is None the same object as None?or giving Python the commandDO NOT CONTINUE if "None is NOT the same object as None" is False
another way to test if something is the same object as None
I change the assertIsNot method to the assertIs method which checks if the object in parentheses on the left is the same as the object on the right
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # self.assertIsNot(None, None) 29 self.assertIs(None, None) 30 31 # assert False is NoneI use the assertIs method to compare False with None
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # self.assertIsNot(None, None) 29 self.assertIs(None, None) 30 31 # assert False is None 32 assert False is not None 33 self.assertIs(False, None) 34 35 # assert True is Nonethe terminal is my friend, and shows AssertionError
E AssertionError: False is not Nonecompare this error message with the one for
assert False is NoneE assert False is NoneI change assertIs to assertIsNot to make the statement True
31 # assert False is None 32 assert False is not None 33 # self.assertIs(False, None) 34 self.assertIsNot(False, None) 35 36 # assert True is Nonethe test passes.
I use assertIs to compare True with None
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # self.assertIsNot(None, None) 29 self.assertIs(None, None) 30 31 # assert False is None 32 assert False is not None 33 # self.assertIs(False, None) 34 self.assertIsNot(False, None) 35 36 # assert True is None 37 assert True is not None 38 self.assertIs(True, None) 39 40 # assert 0 is Nonethe terminal is my friend, and shows AssertionError
E AssertionError: True is not Nonecompare this error message with the one for
assert True is NoneE assert True is NoneI change assertIs to assertIsNot to make the statement True
36 # assert True is None 37 assert True is not None 38 # self.assertIs(True, None) 39 self.assertIsNot(True, None) 40 41 # assert 0 is Nonethe test passes.
I use assertIs to compare an integer (a whole number without decimals) with None
36 # assert True is None 37 assert True is not None 38 # self.assertIs(True, None) 39 self.assertIsNot(True, None) 40 41 # assert 0 is None 42 assert 0 is not None 43 self.assertIs(0, None) 44 45 # assert 0.0 is Nonethe terminal is my friend, and shows AssertionError
AssertionError: 0 is not Nonecompare this error message with the one for
assert 0 is NoneE assert 0 is NoneI change assertIs to assertIsNot to make the statement True
41 # assert 0 is None 42 assert 0 is not None 43 # self.assertIs(0, None) 44 self.assertIsNot(0, None) 45 46 # assert 0.0 is Nonethe test passes.
I use assertIs to compare a float (binary floating point decimal number) with None
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # self.assertIsNot(None, None) 29 self.assertIs(None, None) 30 31 # assert False is None 32 assert False is not None 33 # self.assertIs(False, None) 34 self.assertIsNot(False, None) 35 36 # assert True is None 37 assert True is not None 38 # self.assertIs(True, None) 39 self.assertIsNot(True, None) 40 41 # assert 0 is None 42 assert 0 is not None 43 # self.assertIs(0, None) 44 self.assertIsNot(0, None) 45 46 # assert 0.0 is None 47 assert 0.0 is not None 48 self.assertIs(0.0, None) 49 50 # assert 'a string' is Nonethe terminal is my friend, and shows AssertionError
E AssertionError: 0.0 is not Nonecompare this error message with the one for
assert 0.0 is NoneE assert 0.0 is NoneI change assertIs to assertIsNot to make the statement True
46 # assert 0.0 is None 47 assert 0.0 is not None 48 # self.assertIs(0.0, None) 49 self.assertIsNot(0.0, None) 50 51 # assert 'a string' is Nonethe test passes.
I use assertIs to compare a string (anything in quotes) with None
46 # assert 0.0 is None 47 assert 0.0 is not None 48 # self.assertIs(0.0, None) 49 self.assertIsNot(0.0, None) 50 51 # assert 'a string' is None 52 assert 'a string' is not None 53 self.assertIs('a string', None) 54 55 # assert (1, 2, 3, 'n') is Nonethe terminal is my friend, and shows AssertionError
E AssertionError: 'a string' is not Nonecompare this error message with the one for
assert 'a string' is NoneE AssertionError: assert 'a string' is NoneI change assertIs to assertIsNot to make the statement True
51 # assert 'a string' is None 52 assert 'a string' is not None 53 # self.assertIs('a string', None) 54 self.assertIsNot('a string', None) 55 56 # assert (1, 2, 3, 'n') is Nonethe test passes.
I use assertIs to compare a tuple (anything in parentheses
( )separated by a comma) with None51 # assert 'a string' is None 52 assert 'a string' is not None 53 # self.assertIs('a string', None) 54 self.assertIsNot('a string', None) 55 56 # assert (1, 2, 3, 'n') is None 57 assert (1, 2, 3, 'n') is not None 58 self.assertIs((1, 2, 3, 'n'), None) 59 60 # assert [1, 2, 3, 'n'] is Nonethe terminal is my friend, and shows AssertionError
E AssertionError: (1, 2, 3, 'n') is not Nonecompare this error message with the one for
assert (1, 2, 3, 'n') is NoneE AssertionError: assert (1, 2, 3, 'n') is NoneI change assertIs to assertIsNot to make the statement True
56 # assert (1, 2, 3, 'n') is None 57 assert (1, 2, 3, 'n') is not None 58 # self.assertIs((1, 2, 3, 'n'), None) 59 self.assertIsNot((1, 2, 3, 'n'), None) 60 61 # assert [1, 2, 3, 'n'] is Nonethe test passes.
I use assertIs to compare a list (anything in square brackets
[ ]) with None56 # assert (1, 2, 3, 'n') is None 57 assert (1, 2, 3, 'n') is not None 58 # self.assertIs((1, 2, 3, 'n'), None) 59 self.assertIsNot((1, 2, 3, 'n'), None) 60 61 # assert [1, 2, 3, 'n'] is None 62 assert [1, 2, 3, 'n'] is not None 63 self.assertIs([1, 2, 3, 'n'], None) 64 65 # assert {1, 2, 3, 'n'} is Nonethe terminal is my friend, and shows AssertionError
E AssertionError: [1, 2, 3, 'n'] is not Nonecompare this error message with the one for
assert [1, 2, 3, 'n'] is NoneE AssertionError: assert [1, 2, 3, 'n'] is NoneI change assertIs to assertIsNot to make the statement True
61 # assert [1, 2, 3, 'n'] is None 62 assert [1, 2, 3, 'n'] is not None 63 # self.assertIs([1, 2, 3, 'n'], None) 64 self.assertIsNot([1, 2, 3, 'n'], None) 65 66 # assert {1, 2, 3, 'n'} is Nonethe test passes.
I use assertIs to compare a set (anything in curly braces
{ }separated by a comma) with None61 # assert [1, 2, 3, 'n'] is None 62 assert [1, 2, 3, 'n'] is not None 63 # self.assertIs([1, 2, 3, 'n'], None) 64 self.assertIsNot([1, 2, 3, 'n'], None) 65 66 # assert {1, 2, 3, 'n'} is None 67 assert {1, 2, 3, 'n'} is not None 68 self.assertIs({1, 2, 3, 'n'}, None) 69 70 # assert {'key': 'value'} is Nonethe terminal is my friend, and shows AssertionError
E AssertionError: {1, 2, 3, 'n'} is not Nonecompare this error message with the one for
assert {1, 2, 3, 'n'} is NoneE AssertionError: assert {1, 2, 3, 'n'} is NoneI change assertIs to assertIsNot to make the statement True
66 # assert {1, 2, 3, 'n'} is None 67 assert {1, 2, 3, 'n'} is not None 68 # self.assertIs({1, 2, 3, 'n'}, None) 69 self.assertIsNot({1, 2, 3, 'n'}, None) 70 71 # assert {'key': 'value'} is Nonethe test passes.
I use assertIs to compare a a dictionary (any key-value pairs in curly braces
{ }separated by a comma) with None66 # assert {1, 2, 3, 'n'} is None 67 assert {1, 2, 3, 'n'} is not None 68 # self.assertIs({1, 2, 3, 'n'}, None) 69 self.assertIsNot({1, 2, 3, 'n'}, None) 70 71 # assert {'key': 'value'} is None 72 assert {'key': 'value'} is not None 73 self.assertIs({'key': 'value'}, None) 74 75 76# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: {'key': 'value'} is not Nonecompare this error message with the one for
assert {'key': 'value'} is NoneE AssertionError: assert {'key': 'value'} is NoneI change assertIs to assertIsNot to make the statement True
25 def test_assertion_error_w_none(self): 26 # assert None is not None 27 assert None is None 28 # self.assertIsNot(None, None) 29 self.assertIs(None, None) 30 31 # assert False is None 32 assert False is not None 33 # self.assertIs(False, None) 34 self.assertIsNot(False, None) 35 36 # assert True is None 37 assert True is not None 38 # self.assertIs(True, None) 39 self.assertIsNot(True, None) 40 41 # assert 0 is None 42 assert 0 is not None 43 # self.assertIs(0, None) 44 self.assertIsNot(0, None) 45 46 # assert 0.0 is None 47 assert 0.0 is not None 48 # self.assertIs(0.0, None) 49 self.assertIsNot(0.0, None) 50 51 # assert 'a string' is None 52 assert 'a string' is not None 53 # self.assertIs('a string', None) 54 self.assertIsNot('a string', None) 55 56 # assert (1, 2, 3, 'n') is None 57 assert (1, 2, 3, 'n') is not None 58 # self.assertIs((1, 2, 3, 'n'), None) 59 self.assertIsNot((1, 2, 3, 'n'), None) 60 61 # assert [1, 2, 3, 'n'] is None 62 assert [1, 2, 3, 'n'] is not None 63 # self.assertIs([1, 2, 3, 'n'], None) 64 self.assertIsNot([1, 2, 3, 'n'], None) 65 66 # assert {1, 2, 3, 'n'} is None 67 assert {1, 2, 3, 'n'} is not None 68 # self.assertIs({1, 2, 3, 'n'}, None) 69 self.assertIsNot({1, 2, 3, 'n'}, None) 70 71 # assert {'key': 'value'} is None 72 assert {'key': 'value'} is not None 73 # self.assertIs({'key': 'value'}, None) 74 self.assertIsNot({'key': 'value'}, None) 75 76 77# NOTESthe test passes.
I add a git commit message in the other terminal
git commit --all --message \ 'test_assertion_error_w_none with assert methods'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 and test_assertion_error_w_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 test
71 # assert {'key': 'value'} is None 72 assert {'key': 'value'} is not None 73 # self.assertIs({'key': 'value'}, None) 74 self.assertIsNot({'key': 'value'}, None) 75 76 def test_assertion_error_w_false(self): 77 assert None is False 78 79 80# 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
76 def test_assertion_error_w_false(self):
77 # assert None is False
78 assert None is not False
79
80
81# NOTES
the test passes.
REFACTOR: make it better
I add a note about None
81# NOTES 82# a dictionary is not None 83# a set is not None 84# a list is not None 85# a tuple is not None 86# a string is not None 87# a float is not None 88# an integer is not None 89# True is not None 90# False is not None 91# None is not False 92# None is None
I add a failing assert statement about False
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 assert False is not False 80 81 82# NOTESthe terminal shows AssertionError
E assert False is not FalseI change the statement to make it True
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 82 83# NOTESthe test passes.
I add a note about False
83# NOTES 84# a dictionary is not None 85# a set is not None 86# a list is not None 87# a tuple is not None 88# a string is not None 89# a float is not None 90# an integer is not None 91# True is not None 92# False is False 93# False is not None 94# None is not False 95# None is None
I add an assert statement to see if True is the same object as False
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 assert True is False 82 83 84# NOTESthe terminal shows AssertionError
E assert True is FalseI change the statement to make it True
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 84 85# NOTESthe test passes.
I add a note about True
85# NOTES 86# a dictionary is not None 87# a set is not None 88# a list is not None 89# a tuple is not None 90# a string is not None 91# a float is not None 92# an integer is not None 93# True is not False 94# True is not None 95# False is False 96# False is not None 97# None is not False 98# None is None
I add an assert statement to see if an integer (a whole number without decimals) is the same object as False
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 assert 0 is False 84 85 86# NOTESthe terminal shows AssertionError
E assert 0 is FalseI change the statement to make it True
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 86 87# NOTESthe test passes.
I add a note about integers
87# NOTES 88# a dictionary is not None 89# a set is not None 90# a list is not None 91# a tuple is not None 92# a string is not None 93# a float is not None 94# an integer is not False 95# an integer is not None 96# True is not False 97# True is not None 98# False is False 99# False is not None 100# None is not False 101# None is None
I add an assert statement to see if a float (binary floating point decimal number) is the same object as False
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 assert 0.0 is False 86 87 88# NOTESthe terminal shows AssertionError
E assert 0.0 is FalseI change the statement to make it True
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 88 89# NOTESthe test passes.
I add a note about floats
89# NOTES 90# a dictionary is not None 91# a set is not None 92# a list is not None 93# a tuple is not None 94# a string is not None 95# a float is not False 96# a float is not None 97# an integer is not False 98# an integer is not None 99# True is not False 100# True is not None 101# False is False 102# False is not None 103# None is not False 104# None is None
I add an assert statement to see if a string (anything in quotes) is the same object as False
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 assert 'a string' is False 88 89 90# NOTESthe terminal shows AssertionError
E AssertionError: assert 'a string' is FalseI change the statement to make it True
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 # assert 'a string' is False 88 assert 'a string' is not False 89 90 91# NOTESthe test passes.
I add a note about strings
91# NOTES 92# a dictionary is not None 93# a set is not None 94# a list is not None 95# a tuple is not None 96# a string is not False 97# a string is not None 98# a float is not False 99# a float is not None 100# an integer is not False 101# an integer is not None 102# True is not False 103# True is not None 104# False is False 105# False is not None 106# None is not False 107# 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 False76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 # assert 'a string' is False 88 assert 'a string' is not False 89 assert (1, 2, 3, 'n') is False 90 91 92# NOTESthe terminal shows AssertionError
E AssertionError: assert (1, 2, 3, 'n') is FalseI change the statement to make it True
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 # assert 'a string' is False 88 assert 'a string' is not False 89 # assert (1, 2, 3, 'n') is False 90 assert (1, 2, 3, 'n') is not False 91 92 93# NOTESthe test passes.
I add a note about tuples
93# NOTES 94# a dictionary is not None 95# a set is not None 96# a list is not None 97# a tuple is not False 98# a tuple is not None 99# a string is not False 100# a string is not None 101# a float is not False 102# a float is not None 103# an integer is not False 104# an integer is not None 105# True is not False 106# True is not None 107# False is False 108# False is not None 109# None is not False 110# None is None
I add an assert statement to see if a list (anything in square brackets
[ ]) is the same object as False76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 # assert 'a string' is False 88 assert 'a string' is not False 89 # assert (1, 2, 3, 'n') is False 90 assert (1, 2, 3, 'n') is not False 91 assert [1, 2, 3, 'n'] is False 92 93 94# NOTESthe terminal shows AssertionError
E AssertionError: assert [1, 2, 3, 'n'] is FalseI change the statement to make it True
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 # assert 'a string' is False 88 assert 'a string' is not False 89 # assert (1, 2, 3, 'n') is False 90 assert (1, 2, 3, 'n') is not False 91 # assert [1, 2, 3, 'n'] is False 92 assert [1, 2, 3, 'n'] is not False 93 94 95# NOTESthe test passes.
I add a note about lists
95# NOTES 96# a dictionary is not None 97# a set is not None 98# a list is not False 99# a list is not None 100# a tuple is not False 101# a tuple is not None 102# a string is not False 103# a string is not None 104# a float is not False 105# a float is not None 106# an integer is not False 107# an integer is not None 108# True is not False 109# True is not None 110# False is False 111# False is not None 112# None is not False 113# None is None
I add an assert statement to see if a set (anything in curly braces
{ }separated by a comma) is the same object as False76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 # assert 'a string' is False 88 assert 'a string' is not False 89 # assert (1, 2, 3, 'n') is False 90 assert (1, 2, 3, 'n') is not False 91 # assert [1, 2, 3, 'n'] is False 92 assert [1, 2, 3, 'n'] is not False 93 assert {1, 2, 3, 'n'} is False 94 95 96# NOTESthe terminal shows AssertionError
E AssertionError: assert {1, 2, 3, 'n'} is FalseI change the statement to make it True
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 # assert 'a string' is False 88 assert 'a string' is not False 89 # assert (1, 2, 3, 'n') is False 90 assert (1, 2, 3, 'n') is not False 91 # assert [1, 2, 3, 'n'] is False 92 assert [1, 2, 3, 'n'] is not False 93 # assert {1, 2, 3, 'n'} is False 94 assert {1, 2, 3, 'n'} is not False 95 96 97# NOTESthe test passes.
I add a note about sets
97# NOTES 98# a dictionary is not None 99# a set is not False 100# a set is not None 101# a list is not False 102# a list is not None 103# a tuple is not False 104# a tuple is not None 105# a string is not False 106# a string is not None 107# a float is not False 108# a float is not None 109# an integer is not False 110# an integer is not None 111# True is not False 112# True is not None 113# False is False 114# False is not None 115# None is not False 116# 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 the same object as False76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 # assert 'a string' is False 88 assert 'a string' is not False 89 # assert (1, 2, 3, 'n') is False 90 assert (1, 2, 3, 'n') is not False 91 # assert [1, 2, 3, 'n'] is False 92 assert [1, 2, 3, 'n'] is not False 93 # assert {1, 2, 3, 'n'} is False 94 assert {1, 2, 3, 'n'} is not False 95 assert {'key': 'value'} is False 96 97 98# NOTESthe terminal shows AssertionError
E AssertionError: assert {'key': 'value'} is FalseI change the statement to make it True
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # assert False is not False 80 assert False is False 81 # assert True is False 82 assert True is not False 83 # assert 0 is False 84 assert 0 is not False 85 # assert 0.0 is False 86 assert 0.0 is not False 87 # assert 'a string' is False 88 assert 'a string' is not False 89 # assert (1, 2, 3, 'n') is False 90 assert (1, 2, 3, 'n') is not False 91 # assert [1, 2, 3, 'n'] is False 92 assert [1, 2, 3, 'n'] is not False 93 # assert {1, 2, 3, 'n'} is False 94 assert {1, 2, 3, 'n'} is not False 95 # assert {'key': 'value'} is False 96 assert {'key': 'value'} is not False 97 98 99# NOTESthe test passes.
I add a note about dictionaries
99# NOTES 100# a dictionary is not False 101# a dictionary is not None 102# a set is not False 103# a set is not None 104# a list is not False 105# a list is not None 106# a tuple is not False 107# a tuple is not None 108# a string is not False 109# a string is not None 110# a float is not False 111# a float is not None 112# an integer is not False 113# an integer is not None 114# True is not False 115# True is not None 116# False is False 117# False is not None 118# None is not False 119# None is None 120 121 122# Exceptions seen 123# 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.
another way to test if something is the same object as False
I can also use the assertIs and assertIsNot methods from the unittest.TestCase class to test if something is the same object as False or not.
I go back to the terminal where the tests are running
I add an assertion with the assertIs method (it checks if the two objects in parentheses are the same)
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 self.assertIs(None, False) 80 81 # assert False is not Falsethe terminal is my friend, and shows AssertionError
E AssertionError: None is not Falsebecause None is not the same object as False. Compare this error message with the one for
assert None is FalseE assert None is Falsewhich do you like better?
another way to test if something is NOT the same object as False
I change the assertIs method to the assertIsNot method which checks if the two objects in parentheses are NOT the same
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # self.assertIs(None, False) 80 self.assertIsNot(None, False) 81 82 # assert False is not Falsethe test passes because None is NOT the same object as False
I use the assertIsNot method to compare False with itself
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # self.assertIs(None, False) 80 self.assertIsNot(None, False) 81 82 # assert False is not False 83 assert False is False 84 self.assertIsNot(False, False) 85 86 # assert True is Falsethe terminal is my friend, and shows AssertionError
E AssertionError: unexpectedly identical: Falsecompare this error message with the one for
assert False is not FalseE assert False is not FalseI change assertIsNot to assertIs to make the statement True
82 # assert False is not False 83 assert False is False 84 # self.assertIsNot(False, False) 85 self.assertIs(False, False) 86 87 # assert True is Falsethe test passes.
I use assertIs to compare True with False
82 # assert False is not False 83 assert False is False 84 # self.assertIsNot(False, False) 85 self.assertIs(False, False) 86 87 # assert True is False 88 assert True is not False 89 self.assertIs(True, False) 90 91 # assert 0 is Falsethe terminal is my friend, and shows AssertionError
E AssertionError: True is not Falsecompare this error message with the one for
assert True is FalseE assert True is FalseI change assertIs to assertIsNot to make the statement True
87 # assert True is False 88 assert True is not False 89 # self.assertIs(True, False) 90 self.assertIsNot(True, False) 91 92 # assert 0 is Falsethe test passes.
I use assertIs to compare an integer (a whole number without decimals) with False
87 # assert True is False 88 assert True is not False 89 # self.assertIs(True, False) 90 self.assertIsNot(True, False) 91 92 # assert 0 is False 93 assert 0 is not False 94 self.assertIs(0, False) 95 96 # assert 0.0 is Falsethe terminal is my friend, and shows AssertionError
AssertionError: 0 is not Falsecompare this error message with the one for
assert 0 is FalseE assert 0 is FalseI change assertIs to assertIsNot to make the statement True
92 # assert 0 is False 93 assert 0 is not False 94 # self.assertIs(0, False) 95 self.assertIsNot(0, False) 96 97 # assert 0.0 is Falsethe test passes.
I use assertIs to compare a float (binary floating point decimal number) with False
92 # assert 0 is False 93 assert 0 is not False 94 # self.assertIs(0, False) 95 self.assertIsNot(0, False) 96 97 # assert 0.0 is False 98 assert 0.0 is not False 99 self.assertIs(0.0, False) 100 101 # assert 'a string' is Falsethe terminal is my friend, and shows AssertionError
E AssertionError: 0.0 is not Falsecompare this error message with the one for
assert 0.0 is FalseE assert 0.0 is FalseI change assertIs to assertIsNot to make the statement True
97 # assert 0.0 is False 98 assert 0.0 is not False 99 # self.assertIs(0.0, False) 100 self.assertIsNot(0.0, False) 101 102 # assert 'a string' is Falsethe test passes.
I use assertIs to compare a string (anything in quotes) with False
97 # assert 0.0 is False 98 assert 0.0 is not False 99 # self.assertIs(0.0, False) 100 self.assertIsNot(0.0, False) 101 102 # assert 'a string' is False 103 assert 'a string' is not False 104 self.assertIs('a string', False) 105 106 # assert (1, 2, 3, 'n') is Falsethe terminal is my friend, and shows AssertionError
E AssertionError: 'a string' is not Falsecompare this error message with the one for
assert 'a string' is FalseE AssertionError: assert 'a string' is FalseI change assertIs to assertIsNot to make the statement True
102 # assert 'a string' is False 103 assert 'a string' is not False 104 # self.assertIs('a string', False) 105 self.assertIsNot('a string', False) 106 107 # assert (1, 2, 3, 'n') is Falsethe test passes.
I use assertIs to compare a tuple (anything in parentheses
( )separated by a comma) with False102 # assert 'a string' is False 103 assert 'a string' is not False 104 # self.assertIs('a string', False) 105 self.assertIsNot('a string', False) 106 107 # assert (1, 2, 3, 'n') is False 108 assert (1, 2, 3, 'n') is not False 109 self.assertIs((1, 2, 3, 'n'), False) 110 111 # assert [1, 2, 3, 'n'] is Falsethe terminal is my friend, and shows AssertionError
E AssertionError: (1, 2, 3, 'n') is not Falsecompare this error message with the one for
assert (1, 2, 3, 'n') is FalseE AssertionError: assert (1, 2, 3, 'n') is FalseI change assertIs to assertIsNot to make the statement True
107 # assert (1, 2, 3, 'n') is False 108 assert (1, 2, 3, 'n') is not False 109 # self.assertIs((1, 2, 3, 'n'), False) 110 self.assertIsNot((1, 2, 3, 'n'), False) 111 112 # assert [1, 2, 3, 'n'] is Falsethe test passes.
I use assertIs to compare a list (anything in square brackets
[ ]) with False107 # assert (1, 2, 3, 'n') is False 108 assert (1, 2, 3, 'n') is not False 109 # self.assertIs((1, 2, 3, 'n'), False) 110 self.assertIsNot((1, 2, 3, 'n'), False) 111 112 # assert [1, 2, 3, 'n'] is False 113 assert [1, 2, 3, 'n'] is not False 114 self.assertIs([1, 2, 3, 'n'], False) 115 116 # assert {1, 2, 3, 'n'} is Falsethe terminal is my friend, and shows AssertionError
E AssertionError: [1, 2, 3, 'n'] is not Falsecompare this error message with the one for
assert [1, 2, 3, 'n'] is FalseE AssertionError: assert [1, 2, 3, 'n'] is FalseI change assertIs to assertIsNot to make the statement True
112 # assert [1, 2, 3, 'n'] is False 113 assert [1, 2, 3, 'n'] is not False 114 # self.assertIs([1, 2, 3, 'n'], False) 115 self.assertIsNot([1, 2, 3, 'n'], False) 116 117 # assert {1, 2, 3, 'n'} is Falsethe test passes.
I use assertIs to compare a set (anything in curly braces
{ }separated by a comma) with False112 # assert [1, 2, 3, 'n'] is False 113 assert [1, 2, 3, 'n'] is not False 114 # self.assertIs([1, 2, 3, 'n'], False) 115 self.assertIsNot([1, 2, 3, 'n'], False) 116 117 # assert {1, 2, 3, 'n'} is False 118 assert {1, 2, 3, 'n'} is not False 119 self.assertIs({1, 2, 3, 'n'}, False) 120 121 # assert {'key': 'value'} is Falsethe terminal is my friend, and shows AssertionError
E AssertionError: {1, 2, 3, 'n'} is not Falsecompare this error message with the one for
assert {1, 2, 3, 'n'} is FalseE AssertionError: assert {1, 2, 3, 'n'} is FalseI change assertIs to assertIsNot to make the statement True
117 # assert {1, 2, 3, 'n'} is False 118 assert {1, 2, 3, 'n'} is not False 119 # self.assertIs({1, 2, 3, 'n'}, False) 120 self.assertIsNot({1, 2, 3, 'n'}, False) 121 122 # assert {'key': 'value'} is Falsethe test passes.
I use assertIs to compare a a dictionary (any key-value pairs in curly braces
{ }separated by a comma) with False117 # assert {1, 2, 3, 'n'} is False 118 assert {1, 2, 3, 'n'} is not False 119 # self.assertIs({1, 2, 3, 'n'}, False) 120 self.assertIsNot({1, 2, 3, 'n'}, False) 121 122 # assert {'key': 'value'} is False 123 assert {'key': 'value'} is not False 124 self.assertIs({'key': 'value'}, False) 125 126 127# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: {'key': 'value'} is not Falsecompare this error message with the one for
assert {'key': 'value'} is FalseE AssertionError: assert {'key': 'value'} is FalseI change assertIs to assertIsNot to make the statement True
76 def test_assertion_error_w_false(self): 77 # assert None is False 78 assert None is not False 79 # self.assertIs(None, False) 80 self.assertIsNot(None, False) 81 82 # assert False is not False 83 assert False is False 84 # self.assertIsNot(False, False) 85 self.assertIs(False, False) 86 87 # assert True is False 88 assert True is not False 89 # self.assertIs(True, False) 90 self.assertIsNot(True, False) 91 92 # assert 0 is False 93 assert 0 is not False 94 # self.assertIs(0, False) 95 self.assertIsNot(0, False) 96 97 # assert 0.0 is False 98 assert 0.0 is not False 99 # self.assertIs(0.0, False) 100 self.assertIsNot(0.0, False) 101 102 # assert 'a string' is False 103 assert 'a string' is not False 104 # self.assertIs('a string', False) 105 self.assertIsNot('a string', False) 106 107 # assert (1, 2, 3, 'n') is False 108 assert (1, 2, 3, 'n') is not False 109 # self.assertIs((1, 2, 3, 'n'), False) 110 self.assertIsNot((1, 2, 3, 'n'), False) 111 112 # assert [1, 2, 3, 'n'] is False 113 assert [1, 2, 3, 'n'] is not False 114 # self.assertIs([1, 2, 3, 'n'], False) 115 self.assertIsNot([1, 2, 3, 'n'], False) 116 117 # assert {1, 2, 3, 'n'} is False 118 assert {1, 2, 3, 'n'} is not False 119 # self.assertIs({1, 2, 3, 'n'}, False) 120 self.assertIsNot({1, 2, 3, 'n'}, False) 121 122 # assert {'key': 'value'} is False 123 assert {'key': 'value'} is not False 124 # self.assertIs({'key': 'value'}, False) 125 self.assertIsNot({'key': 'value'}, False) 126 127 128# NOTESthe test passes.
I add a git commit message in the other terminal
git commit --all --message \ 'test_assertion_error_w_false with assert methods'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 False or NOT
test_assertion_error_w_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 test with an assertion to see if None is the same object as True
122 # assert {'key': 'value'} is False 123 assert {'key': 'value'} is not False 124 # self.assertIs({'key': 'value'}, False) 125 self.assertIsNot({'key': 'value'}, False) 126 127 def test_assertion_error_w_true(self): 128 assert None is True 129 130 131# 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
127 def test_assertion_error_w_true(self):
128 # assert None is True
129 assert None is not True
130
131
132# NOTES
the test passes.
REFACTOR: make it better
I add a note about None
132# NOTES 133# a dictionary is not False 134# a dictionary is not None 135# a set is not False 136# a set is not None 137# a list is not False 138# a list is not None 139# a tuple is not False 140# a tuple is not None 141# a string is not False 142# a string is not None 143# a float is not False 144# a float is not None 145# an integer is not False 146# an integer is not None 147# True is not False 148# True is not None 149# False is False 150# False is not None 151# None is not True 152# None is not False 153# None is None
I add an assert statement to see if False is the same object as True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 assert False is True 131 132 133# NOTESthe terminal shows AssertionError
E assert False is TrueI change the statement to make it True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 133 134# NOTESthe test passes.
I add a note about False
134# NOTES 135# a dictionary is not False 136# a dictionary is not None 137# a set is not False 138# a set is not None 139# a list is not False 140# a list is not None 141# a tuple is not False 142# a tuple is not None 143# a string is not False 144# a string is not None 145# a float is not False 146# a float is not None 147# an integer is not False 148# an integer is not None 149# True is not False 150# True is not None 151# False is not True 152# False is False 153# False is not None 154# None is not True 155# None is not False 156# None is None
I add an assert statement about True, that will fail
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 assert True is not True 133 134 135# NOTESthe terminal shows AssertionError
E assert True is not TrueI change the statement to make it True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 135 136# NOTESthe test passes.
I add a note about True
136# NOTES 137# a dictionary is not False 138# a dictionary is not None 139# a set is not False 140# a set is not None 141# a list is not False 142# a list is not None 143# a tuple is not False 144# a tuple is not None 145# a string is not False 146# a string is not None 147# a float is not False 148# a float is not None 149# an integer is not False 150# an integer is not None 151# True is True 152# True is not False 153# True is not None 154# False is not True 155# False is False 156# False is not None 157# None is not True 158# None is not False 159# None is None
I add an assert statement to see if an integer (a whole number without decimals) is the same object as True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 assert 0 is True 135 136 137# NOTESthe terminal shows AssertionError
E assert 0 is TrueI change the statement to make it True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 137 138# NOTESthe test passes.
I add a note about integers
138# NOTES 139# a dictionary is not False 140# a dictionary is not None 141# a set is not False 142# a set is not None 143# a list is not False 144# a list is not None 145# a tuple is not False 146# a tuple is not None 147# a string is not False 148# a string is not None 149# a float is not False 150# a float is not None 151# an integer is not True 152# an integer is not False 153# an integer is not None 154# True is True 155# True is not False 156# True is not None 157# False is not True 158# False is False 159# False is not None 160# None is not True 161# None is not False 162# None is None
I add an assert statement to see if a float (binary floating point decimal number) is the same object as True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 assert 0.0 is True 137 138 139# NOTESthe terminal shows AssertionError
E assert 0.0 is TrueI change the statement to make it True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 139 140# NOTESthe test passes.
I add a note about floats
140# NOTES 141# a dictionary is not False 142# a dictionary is not None 143# a set is not False 144# a set is not None 145# a list is not False 146# a list is not None 147# a tuple is not False 148# a tuple is not None 149# a string is not False 150# a string is not None 151# a float is not True 152# a float is not False 153# a float is not None 154# an integer is not True 155# an integer is not False 156# an integer is not None 157# True is True 158# True is not False 159# True is not None 160# False is not True 161# False is False 162# False is not None 163# None is not True 164# None is not False 165# None is None
I add an assert statement to see if a string (anything in quotes) is the same object as True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 assert 'a string' is True 139 140 141# NOTESthe terminal shows AssertionError
E AssertionError: assert 'a string' is TrueI change the statement to make it True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 # assert 'a string' is True 139 assert 'a string' is not True 140 141 142# NOTESthe test passes.
I add a note about strings
142# NOTES 143# a dictionary is not False 144# a dictionary is not None 145# a set is not False 146# a set is not None 147# a list is not False 148# a list is not None 149# a tuple is not False 150# a tuple is not None 151# a string is not True 152# a string is not False 153# a string is not None 154# a float is not True 155# a float is not False 156# a float is not None 157# an integer is not True 158# an integer is not False 159# an integer is not None 160# True is True 161# True is not False 162# True is not None 163# False is not True 164# False is False 165# False is not None 166# None is not True 167# None is not False 168# 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 True127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 # assert 'a string' is True 139 assert 'a string' is not True 140 assert (1, 2, 3, 'n') is True 141 142 143# NOTESthe terminal shows AssertionError
E AssertionError: assert (1, 2, 3, 'n') is TrueI change the statement to make it True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 # assert 'a string' is True 139 assert 'a string' is not True 140 # assert (1, 2, 3, 'n') is True 141 assert (1, 2, 3, 'n') is not True 142 143 144# NOTESthe test passes.
I add a note about tuples
144# NOTES 145# a dictionary is not False 146# a dictionary is not None 147# a set is not False 148# a set is not None 149# a list is not False 150# a list is not None 151# a tuple is not True 152# a tuple is not False 153# a tuple is not None 154# a string is not True 155# a string is not False 156# a string is not None 157# a float is not True 158# a float is not False 159# a float is not None 160# an integer is not True 161# an integer is not False 162# an integer is not None 163# True is True 164# True is not False 165# True is not None 166# False is not True 167# False is False 168# False is not None 169# None is not True 170# None is not False 171# None is None
I add an assert statement to see if a list (anything in square brackets
[ ]) is the same object as True127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 # assert 'a string' is True 139 assert 'a string' is not True 140 # assert (1, 2, 3, 'n') is True 141 assert (1, 2, 3, 'n') is not True 142 assert [1, 2, 3, 'n'] is True 143 144 145# NOTESthe terminal shows AssertionError
E AssertionError: assert [1, 2, 3, 'n'] is TrueI change the statement to make it True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 # assert 'a string' is True 139 assert 'a string' is not True 140 # assert (1, 2, 3, 'n') is True 141 assert (1, 2, 3, 'n') is not True 142 # assert [1, 2, 3, 'n'] is True 143 assert [1, 2, 3, 'n'] is not True 144 145 146# NOTESthe test passes.
I add a note about lists
146# NOTES 147# a dictionary is not False 148# a dictionary is not None 149# a set is not False 150# a set is not None 151# a list is not True 152# a list is not False 153# a list is not None 154# a tuple is not True 155# a tuple is not False 156# a tuple is not None 157# a string is not True 158# a string is not False 159# a string is not None 160# a float is not True 161# a float is not False 162# a float is not None 163# an integer is not True 164# an integer is not False 165# an integer is not None 166# True is True 167# True is not False 168# True is not None 169# False is not True 170# False is False 171# False is not None 172# None is not True 173# None is not False 174# None is None
I add an assert statement to see if a set (anything in curly braces
{ }separated by a comma) is the same object as True127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 # assert 'a string' is True 139 assert 'a string' is not True 140 # assert (1, 2, 3, 'n') is True 141 assert (1, 2, 3, 'n') is not True 142 # assert [1, 2, 3, 'n'] is True 143 assert [1, 2, 3, 'n'] is not True 144 assert {1, 2, 3, 'n'} is True 145 146 147# NOTESthe terminal shows AssertionError
E AssertionError: assert {1, 2, 3, 'n'} is TrueI change the statement to make it True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 # assert 'a string' is True 139 assert 'a string' is not True 140 # assert (1, 2, 3, 'n') is True 141 assert (1, 2, 3, 'n') is not True 142 # assert [1, 2, 3, 'n'] is True 143 assert [1, 2, 3, 'n'] is not True 144 # assert {1, 2, 3, 'n'} is True 145 assert {1, 2, 3, 'n'} is not True 146 147 148# NOTESthe test passes.
I add a note about sets
148# NOTES 149# a dictionary is not False 150# a dictionary is not None 151# a set is not True 152# a set is not False 153# a set is not None 154# a list is not True 155# a list is not False 156# a list is not None 157# a tuple is not True 158# a tuple is not False 159# a tuple is not None 160# a string is not True 161# a string is not False 162# a string is not None 163# a float is not True 164# a float is not False 165# a float is not None 166# an integer is not True 167# an integer is not False 168# an integer is not None 169# True is True 170# True is not False 171# True is not None 172# False is not True 173# False is False 174# False is not None 175# None is not True 176# None is not False 177# 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 the same object as True127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 # assert 'a string' is True 139 assert 'a string' is not True 140 # assert (1, 2, 3, 'n') is True 141 assert (1, 2, 3, 'n') is not True 142 # assert [1, 2, 3, 'n'] is True 143 assert [1, 2, 3, 'n'] is not True 144 # assert {1, 2, 3, 'n'} is True 145 assert {1, 2, 3, 'n'} is not True 146 assert {'key': 'value'} is True 147 148 149# NOTESthe terminal shows AssertionError
E AssertionError: assert {'key': 'value'} is TrueI change the statement to make it True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # assert False is True 131 assert False is not True 132 # assert True is not True 133 assert True is True 134 # assert 0 is True 135 assert 0 is not True 136 # assert 0.0 is True 137 assert 0.0 is not True 138 # assert 'a string' is True 139 assert 'a string' is not True 140 # assert (1, 2, 3, 'n') is True 141 assert (1, 2, 3, 'n') is not True 142 # assert [1, 2, 3, 'n'] is True 143 assert [1, 2, 3, 'n'] is not True 144 # assert {1, 2, 3, 'n'} is True 145 assert {1, 2, 3, 'n'} is not True 146 # assert {'key': 'value'} is True 147 assert {'key': 'value'} is not True 148 149 150# NOTESthe test passes.
I add a note about dictionaries
150# NOTES 151# a dictionary is not True 152# a dictionary is not False 153# a dictionary is not None 154# a set is not True 155# a set is not False 156# a set is not None 157# a list is not True 158# a list is not False 159# a list is not None 160# a tuple is not True 161# a tuple is not False 162# a tuple is not None 163# a string is not True 164# a string is not False 165# a string is not None 166# a float is not True 167# a float is not False 168# a float is not None 169# an integer is not True 170# an integer is not False 171# an integer is not None 172# True is True 173# True is not False 174# True is not None 175# False is not True 176# False is False 177# False is not None 178# None is not True 179# None is not False 180# None is None 181 182 183# Exceptions seen 184# 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.
another way to test if something is the same object as True
I can also use the assertIs and assertIsNot methods from the unittest.TestCase class to test if something is the same object as True or not.
I go back to the terminal where the tests are running
I add an assertion with the assertIs method (it checks if the two objects in parentheses are the same)
127 def test_assertion_error_w_false(self): 128 # assert None is True 129 assert None is not True 130 self.assertIs(None, True) 131 132 # assert False is Truethe terminal is my friend, and shows AssertionError
E AssertionError: None is not Truebecause None is not the same object as True. Compare this error message with the one for
assert None is TrueE assert None is True
another way to test if something is NOT the same object as True
I change the assertIs method to the assertIsNot method which checks if the two objects in parentheses are NOT the same object
127 def test_assertion_error_w_false(self): 128 # assert None is True 129 assert None is not True 130 # self.assertIs(None, True) 131 self.assertIsNot(None, True) 132 133 # assert False is Truethe test passes because None is NOT the same object as True.
I use the assertIs method to compare False with True
127 # assert False is True 128 assert False is not True 129 self.assertIs(False, True) 130 131 # assert True is not Truethe terminal is my friend, and shows AssertionError
E AssertionError: False is not Truecompare this error message with the one for
assert False is TrueE assert False is TrueI change assertIsNot to assertIs to make the statement True
133 # assert False is True 134 assert False is not True 135 # self.assertIs(False, True) 136 self.assertIsNot(False, True) 137 138 # assert True is not Truethe test passes.
I use assertIsNot to compare True with itself
133 # assert True is not True 134 assert True is True 135 self.assertIsNot(True, True) 136 137 # assert 0 is Truethe terminal is my friend, and shows AssertionError
E AssertionError: unexpectedly identical: Truecompare this error message with the one for
assert True is not TrueE assert True is not TrueI change assertIsNot to assertIs to make the statement True
138 # assert True is not True 139 assert True is True 140 # self.assertIsNot(True, True) 141 self.assertIs(True, True) 142 143 # assert 0 is Truethe test passes.
I use assertIs to compare an integer (a whole number without decimals) with True
143 # assert 0 is True 144 assert 0 is not True 145 self.assertIs(0, True) 146 147 # assert 0.0 is Truethe terminal is my friend, and shows AssertionError
AssertionError: 0 is not Truecompare this error message with the one for
assert 0 is TrueE assert 0 is TrueI change assertIs to assertIsNot to make the statement True
143 # assert 0 is True 144 assert 0 is not True 145 # self.assertIs(0, True) 146 self.assertIsNot(0, True) 147 148 # assert 0.0 is Truethe test passes.
I use assertIs to compare a float (binary floating point decimal number) with True
148 # assert 0.0 is True 149 assert 0.0 is not True 150 self.assertIs(0.0, True) 151 152 # assert 'a string' is Truethe terminal is my friend, and shows AssertionError
E AssertionError: 0.0 is not Truecompare this error message with the one for
assert 0.0 is TrueE assert 0.0 is TrueI change assertIs to assertIsNot to make the statement True
148 # assert 0.0 is True 149 assert 0.0 is not True 150 # self.assertIs(0.0, True) 151 self.assertIsNot(0.0, True) 152 153 # assert 'a string' is Truethe test passes.
I use assertIs to compare a string (anything in quotes) with True
153 # assert 'a string' is True 154 assert 'a string' is not True 155 self.assertIs('a string', True) 156 157 # assert (1, 2, 3, 'n') is Truethe terminal is my friend, and shows AssertionError
E AssertionError: 'a string' is not Truecompare this error message with the one for
assert 'a string' is TrueE AssertionError: assert 'a string' is TrueI change assertIs to assertIsNot to make the statement True
153 # assert 'a string' is True 154 assert 'a string' is not True 155 # self.assertIs('a string', True) 156 self.assertIsNot('a string', True) 157 158 # assert (1, 2, 3, 'n') is Truethe test passes.
I use assertIs to compare a tuple (anything in parentheses
( )separated by a comma) with True158 # assert (1, 2, 3, 'n') is True 159 assert (1, 2, 3, 'n') is not True 160 self.assertIs((1, 2, 3, 'n'), True) 161 162 # assert [1, 2, 3, 'n'] is Truethe terminal is my friend, and shows AssertionError
E AssertionError: (1, 2, 3, 'n') is not Truecompare this error message with the one for
assert (1, 2, 3, 'n') is TrueE AssertionError: assert (1, 2, 3, 'n') is TrueI change assertIs to assertIsNot to make the statement True
158 # assert (1, 2, 3, 'n') is True 159 assert (1, 2, 3, 'n') is not True 160 # self.assertIs((1, 2, 3, 'n'), True) 161 self.assertIsNot((1, 2, 3, 'n'), True) 162 163 # assert [1, 2, 3, 'n'] is Truethe test passes.
I use assertIs to compare a list (anything in square brackets
[ ]) with True163 # assert [1, 2, 3, 'n'] is True 164 assert [1, 2, 3, 'n'] is not True 165 self.assertIs([1, 2, 3, 'n'], True) 166 167 # assert {1, 2, 3, 'n'} is Truethe terminal is my friend, and shows AssertionError
E AssertionError: [1, 2, 3, 'n'] is not Truecompare this error message with the one for
assert [1, 2, 3, 'n'] is TrueE AssertionError: assert [1, 2, 3, 'n'] is TrueI change assertIs to assertIsNot to make the statement True
163 # assert [1, 2, 3, 'n'] is True 164 assert [1, 2, 3, 'n'] is not True 165 # self.assertIs([1, 2, 3, 'n'], True) 166 self.assertIsNot([1, 2, 3, 'n'], True) 167 168 # assert {1, 2, 3, 'n'} is Truethe test passes.
I use assertIs to compare a set (anything in curly braces
{ }separated by a comma) with True168 # assert {1, 2, 3, 'n'} is True 169 assert {1, 2, 3, 'n'} is not True 170 self.assertIs({1, 2, 3, 'n'}, True) 171 172 # assert {'key': 'value'} is Truethe terminal is my friend, and shows AssertionError
E AssertionError: {1, 2, 3, 'n'} is not Truecompare this error message with the one for
assert {1, 2, 3, 'n'} is TrueE AssertionError: assert {1, 2, 3, 'n'} is TrueI change assertIs to assertIsNot to make the statement True
168 # assert {1, 2, 3, 'n'} is True 169 assert {1, 2, 3, 'n'} is not True 170 # self.assertIs({1, 2, 3, 'n'}, True) 171 self.assertIsNot({1, 2, 3, 'n'}, True) 172 173 # assert {'key': 'value'} is Truethe test passes.
I use assertIs to compare a a dictionary (any key-value pairs in curly braces
{ }separated by a comma) with True173 # assert {'key': 'value'} is True 174 assert {'key': 'value'} is not True 175 self.assertIs({'key': 'value'}, True) 176 177 178# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: {'key': 'value'} is not Truecompare this error message with the one for
assert {'key': 'value'} is TrueE AssertionError: assert {'key': 'value'} is TrueI change assertIs to assertIsNot to make the statement True
127 def test_assertion_error_w_true(self): 128 # assert None is True 129 assert None is not True 130 # self.assertIs(None, True) 131 self.assertIsNot(None, True) 132 133 # assert False is True 134 assert False is not True 135 # self.assertIs(False, True) 136 self.assertIsNot(False, True) 137 138 # assert True is not True 139 assert True is True 140 # self.assertIsNot(True, True) 141 self.assertIs(True, True) 142 143 # assert 0 is True 144 assert 0 is not True 145 # self.assertIs(0, True) 146 self.assertIsNot(0, True) 147 148 # assert 0.0 is True 149 assert 0.0 is not True 150 # self.assertIs(0.0, True) 151 self.assertIsNot(0.0, True) 152 153 # assert 'a string' is True 154 assert 'a string' is not True 155 # self.assertIs('a string', True) 156 self.assertIsNot('a string', True) 157 158 # assert (1, 2, 3, 'n') is True 159 assert (1, 2, 3, 'n') is not True 160 # self.assertIs((1, 2, 3, 'n'), True) 161 self.assertIsNot((1, 2, 3, 'n'), True) 162 163 # assert [1, 2, 3, 'n'] is True 164 assert [1, 2, 3, 'n'] is not True 165 # self.assertIs([1, 2, 3, 'n'], True) 166 self.assertIsNot([1, 2, 3, 'n'], True) 167 168 # assert {1, 2, 3, 'n'} is True 169 assert {1, 2, 3, 'n'} is not True 170 # self.assertIs({1, 2, 3, 'n'}, True) 171 self.assertIsNot({1, 2, 3, 'n'}, True) 172 173 # assert {'key': 'value'} is True 174 assert {'key': 'value'} is not True 175 # self.assertIs({'key': 'value'}, True) 176 self.assertIsNot({'key': 'value'}, True) 177 178 179# NOTESthe test passes.
I add a git commit message in the other terminal
git commit --all --message \ 'test_assertion_error_w_true with assert methods'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_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 also 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 equal173 # assert {'key': 'value'} is True 174 assert {'key': 'value'} is not True 175 # self.assertIs({'key': 'value'}, True) 176 self.assertIsNot({'key': 'value'}, True) 177 178 def test_assertion_error_w_equality(self): 179 assert None != None 180 181 182# 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
178 def test_assertion_error_w_equality(self):
179 # assert None != None
180 assert None == None
181
182
183# NOTES
the test passes because None is equal to None is True
REFACTOR: make it better
I add to the
None is Nonenote183# NOTES 184# a dictionary is not True 185# a dictionary is not False 186# a dictionary is not None 187# a set is not True 188# a set is not False 189# a set is not None 190# a list is not True 191# a list is not False 192# a list is not None 193# a tuple is not True 194# a tuple is not False 195# a tuple is not None 196# a string is not True 197# a string is not False 198# a string is not None 199# a float is not True 200# a float is not False 201# a float is not None 202# an integer is not True 203# an integer is not False 204# an integer is not None 205# True is True 206# True is not False 207# True is not None 208# False is not True 209# False is False 210# False is not None 211# None is not True 212# None is not False 213# None is None and equal to NoneI add an assertion to compare False with None
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 assert False == None 182 183 184# NOTESthe terminal is my friend, and shows AssertionError
E assert False == NoneI change the assert statement to make it True
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 # assert False == None 182 assert False != None 183 184 185# NOTESthe test passes.
I add to the
False is not NoneandNone is not Falsenotes because equality goes both ways185# NOTES 186# a dictionary is not True 187# a dictionary is not False 188# a dictionary is not None 189# a set is not True 190# a set is not False 191# a set is not None 192# a list is not True 193# a list is not False 194# a list is not None 195# a tuple is not True 196# a tuple is not False 197# a tuple is not None 198# a string is not True 199# a string is not False 200# a string is not None 201# a float is not True 202# a float is not False 203# a float is not None 204# an integer is not True 205# an integer is not False 206# an integer is not None 207# True is True 208# True is not False 209# True is not None 210# False is not True 211# False is False 212# False is not None and NOT equal to None 213# None is not True 214# None is not False and NOT equal to False 215# None is None and equal to NoneI add an assertion to compare False with True
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 # assert False == None 182 assert False != None 183 assert False == True 184 185 186# NOTESthe terminal is my friend, and shows AssertionError
E assert False == TrueI change the assert statement to make it True
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 # assert False == None 182 assert False != None 183 # assert False == True 184 assert False != True 185 186 187# NOTESthe test passes.
I add to the
False is not TrueandTrue is not Falsenotes because equality goes both ways187# NOTES 188# a dictionary is not True 189# a dictionary is not False 190# a dictionary is not None 191# a set is not True 192# a set is not False 193# a set is not None 194# a list is not True 195# a list is not False 196# a list is not None 197# a tuple is not True 198# a tuple is not False 199# a tuple is not None 200# a string is not True 201# a string is not False 202# a string is not None 203# a float is not True 204# a float is not False 205# a float is not None 206# an integer is not True 207# an integer is not False 208# an integer is not None 209# True is True 210# True is not False and NOT equal to False 211# True is not None 212# False is not True and NOT equal to True 213# False is False 214# False is not None and NOT equal to None 215# None is not True 216# None is not False and NOT equal to False 217# None is None and equal to NoneI add an assertion to compare False with itself
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 # assert False == None 182 assert False != None 183 # assert False == True 184 assert False != True 185 186 187# NOTESthe terminal is my friend, and shows AssertionError
E assert False != FalseI make the statement True
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 # assert False == None 182 assert False != None 183 # assert False == True 184 assert False != True 185 # assert False != False 186 assert False == False 187 188 189# NOTESthe test passes.
I add to the
False is Falsenote189# NOTES 190# a dictionary is not True 191# a dictionary is not False 192# a dictionary is not None 193# a set is not True 194# a set is not False 195# a set is not None 196# a list is not True 197# a list is not False 198# a list is not None 199# a tuple is not True 200# a tuple is not False 201# a tuple is not None 202# a string is not True 203# a string is not False 204# a string is not None 205# a float is not True 206# a float is not False 207# a float is not None 208# an integer is not True 209# an integer is not False 210# an integer is not None 211# True is True 212# True is not False and NOT equal to False 213# True is not None 214# False is not True and NOT equal to True 215# False is False and equal to False 216# False is not None and NOT equal to None 217# None is not True 218# None is not False and NOT equal to False 219# None is None and equal to None
I add a new failing assertion to compare True with None
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 # assert False == None 182 assert False != None 183 # assert False == True 184 assert False != True 185 # assert False != False 186 assert False == False 187 assert True == None 188 189 190# NOTESthe terminal is my friend, and shows AssertionError
E assert True == NoneI change the assert statement to make it True
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 # assert False == None 182 assert False != None 183 # assert False == True 184 assert False != True 185 # assert False != False 186 assert False == False 187 # assert True == None 188 assert True != None 189 190 191# NOTESthe test passes.
I add to the
True is not NoneandNone is not Truenotes because equality goes both ways191# NOTES 192# a dictionary is not True 193# a dictionary is not False 194# a dictionary is not None 195# a set is not True 196# a set is not False 197# a set is not None 198# a list is not True 199# a list is not False 200# a list is not None 201# a tuple is not True 202# a tuple is not False 203# a tuple is not None 204# a string is not True 205# a string is not False 206# a string is not None 207# a float is not True 208# a float is not False 209# a float is not None 210# an integer is not True 211# an integer is not False 212# an integer is not None 213# True is True 214# True is not False and NOT equal to False 215# True is not None and NOT equal to None 216# False is not True and NOT equal to True 217# False is False and equal to False 218# False is not None and NOT equal to None 219# None is not True and NOT equal to True 220# None is not False and NOT equal to False 221# None is None and equal to NoneI add an assertion to compare True with itself
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 # assert False == None 182 assert False != None 183 # assert False == True 184 assert False != True 185 # assert False != False 186 assert False == False 187 # assert True == None 188 assert True != None 189 assert True != True 190 191 192# NOTESthe terminal is my friend, and shows AssertionError
E assert True != TrueI make the statement True
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 # assert False == None 182 assert False != None 183 # assert False == True 184 assert False != True 185 # assert False != False 186 assert False == False 187 # assert True == None 188 assert True != None 189 # assert True != True 190 assert True == True 191 192 193# NOTESthe test passes.
I add to the
True is Truenote193# NOTES 194# a dictionary is not True 195# a dictionary is not False 196# a dictionary is not None 197# a set is not True 198# a set is not False 199# a set is not None 200# a list is not True 201# a list is not False 202# a list is not None 203# a tuple is not True 204# a tuple is not False 205# a tuple is not None 206# a string is not True 207# a string is not False 208# a string is not None 209# a float is not True 210# a float is not False 211# a float is not None 212# an integer is not True 213# an integer is not False 214# an integer is not None 215# True is True and equal to True 216# True is not False and NOT equal to False 217# True is not None and NOT equal to None 218# False is not True and NOT equal to True 219# False is False and equal to False 220# False is not None and NOT equal to None 221# None is not True and NOT equal to True 222# None is not False and NOT equal to False 223# None is None and equal to None 224 225 226# Exceptions seen 227# AssertionErrorI 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.
another way to 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 editor178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 self.assertNotEqual(None, None) 182 183 # assert False == Nonethe terminal is my friend, and shows AssertionError
E AssertionError: None == Nonebecause None is equal to None, they are exactly 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
another way to 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
178 def test_assertion_error_w_equality(self): 179 # assert None != None 180 assert None == None 181 # self.assertNotEqual(None, None) 182 self.assertEqual(None, None) 183 184 # assert False == NoneI use assertEqual to compare False with None
184 # assert False == None 185 assert False != None 186 self.assertEqual(False, None) 187 188 # assert False == Truethe 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
184 # assert False == None 185 assert False != None 186 # self.assertEqual(False, None) 187 self.assertNotEqual(False, None) 188 189 # assert False == Truethe test passes.
I use assertEqual to compare False with True
189 # assert False == True 190 assert False != True 191 self.assertEqual(False, True) 192 193 # assert False != Falsethe 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
189 # assert False == True 190 assert False != True 191 # self.assertEqual(False, True) 192 self.assertNotEqual(False, True) 193 194 # assert False != Falsethe test passes.
I use assertNotEqual to compare False with itself
194 # assert False != False 195 assert False == False 196 self.assertNotEqual(False, False) 197 198 # assert True == Nonethe 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
194 # assert False != False 195 assert False == False 196 # self.assertNotEqual(False, False) 197 self.assertEqual(False, False) 198 199 # assert True == Nonethe test passes.
I use assertEqual to compare True with None
199 # assert True == None 200 assert True != None 201 self.assertEqual(True, None) 202 203 # assert True != Truethe 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
199 # assert True == None 200 assert True != None 201 # self.assertEqual(True, None) 202 self.assertNotEqual(True, None) 203 204 # assert True != Truethe test passes.
I use assertNotEqual to compare True with itself
204 # assert True != True 205 assert True == True 206 self.assertNotEqual(True, True) 207 208 209# 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
127 def test_assertion_error_w_equality(self): 128 # assert None != None 129 assert None == None 130 # self.assertNotEqual(None, None) 131 self.assertEqual(None, None) 132 133 # assert False == None 134 assert False != None 135 # self.assertEqual(False, None) 136 self.assertNotEqual(False, None) 137 138 # assert False == True 139 assert False != True 140 # self.assertEqual(False, True) 141 self.assertNotEqual(False, True) 142 143 # assert False != False 144 assert False == False 145 # self.assertNotEqual(False, False) 146 self.assertEqual(False, False) 147 148 # assert True == None 149 assert True != None 150 # self.assertEqual(True, None) 151 self.assertNotEqual(True, None) 152 153 # assert True != True 154 assert True == True 155 # self.assertNotEqual(True, True) 156 self.assertEqual(True, True) 157 158 159# NOTESthe test passes.
I add a git commit message in the other terminal
git commit --all --message \ 'test_assertion_error_w_equality with 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 teh first assertion of test_what_is_an_assertion
6 def test_what_is_an_assertion(self): 7 reality = 1 + 1 8 my_expectation = 2 9 # my_expectation = 11 10 # reality == my_expectation 11 assert reality == my_expectation 12 self.assertNotEqual(reality, my_expectation) 13 14 reality = '1' + '1'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 # my_expectation = 11 10 # reality == my_expectation 11 assert reality == my_expectation 12 # self.assertNotEqual(reality, my_expectation) 13 self.assertEqual(reality, my_expectation) 14 15 reality = '1' + '1'the test passes.
I add a call to assertNotEqual for the next assertion
15 reality = '1' + '1' 16 # my_expectation = '2' 17 my_expectation = '11' 18 # reality == my_expectation 19 assert reality == my_expectation 20 self.assertNotEqual(reality, my_expectation) 21 22 reality = 'I am' + ' alive'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
15 reality = '1' + '1' 16 # my_expectation = '2' 17 my_expectation = '11' 18 # reality == my_expectation 19 assert reality == my_expectation 20 # self.assertNotEqual(reality, my_expectation) 21 self.assertEqual(reality, my_expectation) 22 23 reality = 'I am' + ' alive'the test passes.
I add a call to assertNotEqual for the last assertion in test_what_is_an_assertion
23 reality = 'I am' + ' alive' 24 # my_expectation = '11' 25 my_expectation = 'I am alive' 26 # reality == my_expectation 27 assert reality == my_expectation 28 self.assertNotEqual(reality, my_expectation) 29 30 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 # my_expectation = 11 10 # reality == my_expectation 11 assert reality == my_expectation 12 # self.assertNotEqual(reality, my_expectation) 13 self.assertEqual(reality, my_expectation) 14 15 reality = '1' + '1' 16 # my_expectation = '2' 17 my_expectation = '11' 18 # reality == my_expectation 19 assert reality == my_expectation 20 # self.assertNotEqual(reality, my_expectation) 21 self.assertEqual(reality, my_expectation) 22 23 reality = 'I am' + ' alive' 24 # my_expectation = '11' 25 my_expectation = 'I am alive' 26 # reality == my_expectation 27 assert reality == my_expectation 28 # self.assertNotEqual(reality, my_expectation) 29 self.assertEqual(reality, my_expectation) 30 31 def test_assertion_error_w_none(self):the test passes.
I add a git commit message in the other terminal
git commit --all --message \ 'test_what_is_an_assertion with assert methods'the terminal shows a summary of the changes then goes back to the command line.
test_assertion_error_w_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 equal?
In Python they do not mean the same thing
x is ystates thatxis the same exact object asyx is not ystates thatxis NOT the same exact object asyx == ystates thatxis equal toyaccording to a rule or instruction programmed in Pythonx != ystates thatxis NOT equal toyaccording to a rule or instruction programmed in Python
This means that things can be equal without being the same exact object. For example, integers (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 a test to show this
210 # assert True != True
211 assert True == True
212 # self.assertNotEqual(True, True)
213 self.assertEqual(True, True)
214
215 def test_assertion_error_w_is_vs_equal(self):
216 assert 0 is 0.0
217
218
219 # NOTES
the terminal is my friend, and shows AssertionError
E assert 0 is 0.0
GREEN: make it pass
I change the assertion to make the statement True
215 def test_assertion_error_w_is_vs_equal(self):
216 # assert 0 is 0.0
217 assert 0 is not 0.0
218
219
220# NOTES
the test passes
REFACTOR: make it better
I add another assertion
215 def test_assertion_error_w_is_vs_equal(self): 216 # assert 0 is 0.0 217 assert 0 is not 0.0 218 assert 0 != 0.0 219 220 221# NOTESthe terminal is my friend, and shows AssertionError
E assert 0 != 0.0I change the assertion to make the statement True
215 def test_assertion_error_w_is_vs_equal(self): 216 # assert 0 is 0.0 217 assert 0 is not 0.0 218 # assert 0 != 0.0 219 assert 0 == 0.0 220 221 222# NOTESthe test passes
I add assertIs for the first assertion
215 def test_assertion_error_w_is_vs_equal(self): 216 # assert 0 is 0.0 217 assert 0 is not 0.0 218 self.assertIs(0, 0.0) 219 220 # assert 0 != 0.0 221 assert 0 == 0.0 222 223 224# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: 0 is not 0.0I change assertIs to assertIsNot
215 def test_assertion_error_w_is_vs_equal(self): 216 # assert 0 is 0.0 217 assert 0 is not 0.0 218 # self.assertIs(0, 0.0) 219 self.assertIsNot(0, 0.0) 220 221 # assert 0 != 0.0 222 assert 0 == 0.0 223 224 225# NOTESthe test passes
I add the assertNotEqual method for the next assertion
215 def test_assertion_error_w_is_vs_equal(self): 216 # assert 0 is 0.0 217 assert 0 is not 0.0 218 # self.assertIs(0, 0.0) 219 self.assertIsNot(0, 0.0) 220 221 # assert 0 != 0.0 222 assert 0 == 0.0 223 self.assertNotEqual(0, 0.0) 224 225 226# NOTESthe terminal is my friend, and shows AssertionError
E AssertionError: 0 == 0.0I change assertNotEqual to assertEqual to make the statement True
215 def test_assertion_error_w_is_vs_equal(self): 216 # assert 0 is 0.0 217 assert 0 is not 0.0 218 # self.assertIs(0, 0.0) 219 self.assertIsNot(0, 0.0) 220 221 # assert 0 != 0.0 222 assert 0 == 0.0 223 # self.assertNotEqual(0, 0.0) 224 self.assertEqual(0, 0.0) 225 226 227# NOTESI add a git commit message in the other terminal
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.q
The tests show that an integer can be EQUAL to a float but an integer IS NOT a float.
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
The tests show that
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.