AssertionError¶
AssertionError is raised to show that a statement is False. It was introduced in how to make a python test driven development environment with the first failing test
self.assertFalse(True)
which is like
assert True is False
I can use assertions when building a program to make sure something is True before it continues. I can also use them to test how the program behaves, for example when it is given inputs.
Assertions can help catch things that break passing tests when they are added. They also help me answer 2 questions
what is the same?
what is different?
The difference between my expectations and reality (what happens when the program runs), tells me what to change to make them match.
requirements¶
I open a terminal to run makePythonTdd.sh with
assertion_erroras the name of the project./makePythonTdd.sh assertion_error
on Windows without Windows Subsystem Linux use makePythonTdd.ps1
./makePythonTdd.ps1 assertion_error
it makes the folders and files that are needed, installs packages, runs the first test, and the terminal shows AssertionError
E AssertionError: True is not false tests/test_assertion_error.py:7: AssertionError
I hold
ctrl(windows/linux) oroption(mac) on the keyboard and use the mouse to click ontests/test_assertion_error.py:7to open it in the editorthen I change
TruetoFalseto make the test pass7 self.assertFalse(False)
I change the name of the class to match the CapWords format
4class TestAssertionError(unittest.TestCase):
test_assertion_error_w_none¶
red: make it fail¶
I change
test_failuretotest_assertion_error_w_none1import unittest 2 3 4class TestAssertionError(unittest.TestCase): 5 6 def test_assertion_error_w_none(self): 7 assert None is not None
the terminal shows AssertionError
E assert None is not None tests/test_assertion_error.py:7: AssertionError
the assert at the beginning of the line makes the statement something like “DO NOT CONTINUE, UNLESS None is NOT None”. The AssertionError is raised to stop the program from running, because this statement is False
green: make it pass¶
I change the failing line to make it True
6 def test_assertion_error_w_none(self):
7 assert None is None
the test passes
refactor: make it better¶
I can also make assertions with some methods from the unittest.TestCase class
I add another failing line using the assertIsNotNone method which checks if something is NOT None
6 def test_assertion_error_w_none(self): 7 assert None is None 8 self.assertIsNotNone(None)
the terminal shows a more descriptive message for the AssertionError
AssertionError: unexpectedly None
I change the statement to use the assertIsNone method which checks if something is None
6 def test_assertion_error_w_none(self): 7 assert None is None 8 self.assertIsNone(None)
the test passes
I add a note
11# NOTES 12# None is None 13 14 15# Exceptions Encountered 16# AssertionError
I add a new failing line
6 def test_assertion_error_w_none(self): 7 assert None is None 8 self.assertIsNone(None) 9 10 assert False is None
the terminal shows AssertionError
E assert False is None
I change the line to make it True
10 assert False is not None
the test passes.
I add another line with the assertIsNone method which checks if something is None
6 def test_assertion_error_w_none(self): 7 assert None is None 8 self.assertIsNone(None) 9 10 assert False is not None 11 self.assertIsNone(False)
the terminal shows AssertionError
AssertionError: False is not None
I change the statement with assertIsNotNone to make it True
10 assert False is None 11 self.assertIsNotNone(False)
the test passes.
I add another note
14# NOTES 15# False is not None 16# None is None
I add another assertion
6 def test_assertion_error_w_none(self): 7 assert None is None 8 self.assertIsNone(None) 9 10 assert False is not None 11 self.assertIsNotNone(False) 12 13 assert True is None
the terminal shows AssertionError
E assert True is None
I change the statement to make it True
13 assert True is not None
the test passes
I use the assertIsNone method
6 def test_assertion_error_w_none(self): 7 assert None is None 8 self.assertIsNone(None) 9 10 assert False is not None 11 self.assertIsNotNone(False) 12 13 assert True is not None 14 self.assertIsNone(True)
the terminal shows AssertionError
AssertionError: True is not None
I make the statement True
13 assert True is not None 14 self.assertIsNotNone(True)
the test passes
I add a note
17# NOTES 18# True is not None 19# False is not None 20# None is None
test_assertion_error_w_false¶
I can use assertions to test if something is False
red: make it fail¶
I add a failing test
13 assert True is not None
14 self.assertIsNotNone(True)
15
16 def test_assertion_error_w_false(self):
17 assert True is False
the terminal shows AssertionError
E assert True is False
green: make it pass¶
I change the line to make it True
16 def test_assertion_error_w_false(self):
17 assert True is not False
the test passes
refactor: make it better¶
There is an assert method to check if something is False, it is the one from the first failing test
16 def test_assertion_error_w_false(self): 17 assert True is not False 18 self.assertFalse(True)
the terminal shows AssertionError
AssertionError: True is not false
I make the statement True
16 def test_assertion_error_w_false(self): 17 assert True is not False 18 self.assertFalse(False)
the test passes
I add notes
21# NOTES 22# False is False 23# True is not False 24# True is not None 25# False is not None 26# None is None
test_assertion_error_w_true¶
I can also use assertions to test if something is True
red: make it fail¶
I add a failing test
16 def test_assertion_error_w_false(self):
17 assert True is not False
18 self.assertFalse(False)
19
20 def test_assertion_error_w_true(self):
21 assert False is True
the terminal shows AssertionError
E assert False is True
green: make it pass¶
I change the statement to make it True
20 def test_assertion_error_w_true(self):
21 assert False is not True
the test passes
refactor: make it better¶
there is an assert method to check if something is True
20 def test_assertion_error_w_true(self): 21 assert False is not True 22 self.assertTrue(False)
the terminal shows AssertionError
AssertionError: False is not true
I change the failing line to make it True
20 def test_assertion_error_w_true(self): 21 assert False is not True 22 self.assertTrue(True)
the test passes
I add more notes
25# NOTES 26# True is True 27# False is not True 28# False is False 29# True is not False 30# True is not None 31# False is not None 32# None is None
All these statements can also be said as - True, False and None are different. They set up a basic expectation of the language because I can compare things with them.
test_assertion_error_w_equality¶
I can use assertions to test if 2 things are equal
red: make it fail¶
I add a new failing test
20 def test_assertion_error_w_true(self):
21 assert False is not True
22 self.assertTrue(True)
23
24 def test_assertion_error_w_equality(self):
25 assert None != None
the terminal shows AssertionError
E assert None != None
!= is the symbol for NOT equal which makes this statement read as assert None is NOT equal to None
green: make it pass¶
I change the statement to make it True
24 def test_assertion_error_w_equality(self):
25 assert None == None
the test passes. == is the symbol for is equal which makes this statement read as assert None is equal to None
refactor: make it better¶
there are assert methods to check for equality. I add assertNotEqual checks if the 2 things in the parentheses are NOT equal
24 def test_assertion_error_w_equality(self): 25 assert None == None 26 self.assertNotEqual(None, None)
the terminal shows AssertionError
AssertionError: None == None
I change the assertion to use the assertEqual method to make the failing line True
24 def test_assertion_error_w_equality(self): 25 assert None == None 26 self.assertEqual(None, None)
the test passes
I add to the notes
29# NOTES 30# True is True 31# False is not True 32# False is False 33# True is not False 34# True is not None 35# False is not None 36# None is None and equal to None
I add a new failing line
24 def test_assertion_error_w_equality(self): 25 assert None == None 26 self.assertEqual(None, None) 27 28 assert False == None
the terminal shows AssertionError
E assert False == None
I change the failing line to make it True
28 assert False != None
the test passes.
I add the assertEqual method
28 assert False != None 29 self.assertEqual(False, None)
the terminal shows AssertionError
AssertionError: False != None
I change the failing line to make it True
28 assert False != None 29 self.assertNotEqual(False, None)
the test passes
I add to the notes
34# NOTES 35# True is True 36# False is not True 37# False is False 38# True is not False 39# True is not None 40# False is not None and not equal to None 41# None is None and equal to None
I add the next failing line
24 def test_assertion_error_w_equality(self): 25 assert None == None 26 self.assertEqual(None, None) 27 28 assert False != None 29 self.assertNotEqual(False, None) 30 31 assert True == None
the terminal shows AssertionError
E assert True == None
I change the statement
31 assert True != None
the test passes
-
31 assert True != None 32 self.assertEqual(True, None)
the terminal shows AssertionError
AssertionError: True != None
I change the method
31 assert True != None 32 self.assertNotEqual(True, None)
and the test passes
I add a note
35# NOTES 36# True is True 37# False is not True 38# False is False 39# True is not False 40# True is not None and not equal to None 41# False is not None and not equal to None 42# None is None and equal to None
I add another failing line
31 def test_assertion_error_w_equality(self): 32 assert None == None 33 self.assertEqual(None, None) 34 35 assert False != None 36 self.assertNotEqual(False, None) 37 38 assert True != None 39 self.assertNotEqual(True, None) 40 41 assert True == False
the terminal shows AssertionError
E assert True == False
I change the failing line
34 assert True != False
the test passes
I add assertEqual
34 assert True != False 35 self.assertEqual(True, False)
the terminal shows AssertionError
AssertionError: True != False
34 assert True != False 35 self.assertNotEqual(True, False)
the test passes
I add a note
38# NOTES 39# True is True 40# False is not True 41# False is False 42# True is not False and not equal to False 43# True is not None and not equal to None 44# False is not None and not equal to None 45# None is None and equal to None
on to the next line
24 def test_assertion_error_w_equality(self): 25 assert None == None 26 self.assertEqual(None, None) 27 28 assert False != None 29 self.assertNotEqual(False, None) 30 31 assert True != None 32 self.assertNotEqual(True, None) 33 34 assert True != False 35 self.assertNotEqual(True, False) 36 37 assert False != False
the terminal shows AssertionError
E assert False != False
I make the line True
37 assert False == False
the test passes
I add another failing line with an assert method
37 assert False == False 38 self.assertNotEqual(False, False)
the terminal shows AssertionError
AssertionError: False == False
I change the method
37 assert False == False 38 self.assertEqual(False, False)
the test passes
I add a note
41# NOTES 42# True is True 43# False is not True 44# False is False and equal to False 45# True is not False and not equal to False 46# True is not None and not equal to None 47# False is not None and not equal to None 48# None is None and equal to None
I add another failing line
24 def test_assertion_error_w_equality(self): 25 assert None == None 26 self.assertEqual(None, None) 27 28 assert False != None 29 self.assertNotEqual(False, None) 30 31 assert True != None 32 self.assertNotEqual(True, None) 33 34 assert True != False 35 self.assertNotEqual(True, False) 36 37 assert False == False 38 self.assertEqual(False, False) 39 40 assert False == True
the terminal shows AssertionError
E assert False == True
I change the line
40 assert False != True
the test passes
I add another failing line
40 assert False != True 41 self.assertEqual(False, True)
the terminal shows AssertionError
AssertionError: False != True
40 assert False != True 41 self.assertNotEqual(False, True)
the test passes
I add another note
44# NOTES 45# True is True 46# False is not True and not equal to True 47# False is False and equal to False 48# True is not False and not equal to False 49# True is not None and not equal to None 50# False is not None and not equal to None 51# None is None and equal to None
time for the last statements
24 def test_assertion_error_w_equality(self): 25 assert None == None 26 self.assertEqual(None, None) 27 28 assert False != None 29 self.assertNotEqual(False, None) 30 31 assert True != None 32 self.assertNotEqual(True, None) 33 34 assert True != False 35 self.assertNotEqual(True, False) 36 37 assert False == False 38 self.assertEqual(False, False) 39 40 assert False != True 41 self.assertNotEqual(False, True) 42 43 assert True != True
the terminal shows AssertionError
E assert True != True
I make the statement True
43 assert True == True
the test passes
I add a line with the assert method
43 assert True == True 44 self.assertNotEqual(True, True)
the terminal shows AssertionError
AssertionError: True == True
I change the method
43 assert True == True 44 self.assertEqual(True, True)
and all tests are passing!
I add a note for the last statement
47# NOTES 48# True is True and equal to True 49# False is not True and not equal to True 50# False is False and equal to False 51# True is not False and not equal to False 52# True is not None and not equal to None 53# False is not None and not equal to None 54# None is None and equal to None
review¶
I can use assert statements and methods to test if something is
Would you like to test what is None?