test AssertionError with functions
I want to use the assert_equal function and make other functions like it in the AssertionError project.
preview
I have these tests by the end of the chapter
1def assert_is_not(x, y):
2 assert x is not y
3
4
5def assert_is_not_none(x):
6 assert_is_not(x, None)
7
8
9def assert_is_not_false(x):
10 assert_is_not(x, False)
13def assert_is_not_true(x):
14 assert_is_not(x, True)
15
16
17def assert_not_equal(x, y):
18 assert x != y
19
20
21def assert_equal(x, y):
22 assert x == y
25def test_assert_keyword():
26 assert_equal(1+1, 2)
27 assert_equal('1'+'1', '11')
28 assert_equal('I am'+' alive', 'I am alive')
31def test_assertion_error_w_none():
32 assert None is None
33
34 assert_is_not_none(False)
35 assert_is_not_none(True)
36 assert_is_not_none(0)
37 assert_is_not_none(0.0)
38 assert_is_not_none('')
39 assert_is_not_none(())
40 assert_is_not_none([])
41 assert_is_not_none(set())
42 assert_is_not_none({})
45def test_assertion_error_w_false():
46 assert False is False
47
48 assert_is_not_false(None)
49 assert_is_not_false(True)
50 assert_is_not_false(0)
51 assert_is_not_false(0.0)
52 assert_is_not_false('')
53 assert_is_not_false(())
54 assert_is_not_false([])
55 assert_is_not_false(set())
56 assert_is_not_false({})
59def test_assertion_error_w_true():
60 assert True is True
61
62 assert_is_not_true(None)
63 assert_is_not_true(False)
64 assert_is_not_true(0)
65 assert_is_not_true(0.0)
66 assert_is_not_true('')
67 assert_is_not_true(())
68 assert_is_not_true([])
69 assert_is_not_true(set())
70 assert_is_not_true({})
73def test_assertion_error_w_equality():
74 assert_equal(None, None)
75 assert_equal(False, False)
76 assert_equal(True, True)
77
78 assert_not_equal(False, None)
79 assert_not_equal(False, True)
80 assert_not_equal(True, None)
83def test_assertion_error_w_is_vs_equal():
84 assert_is_not(0, 0.0)
85 assert_equal(0, 0.0)
86
87
88def will_not_run():
89 # will not run because
90 # the name does not start with test
91 assert False == True
92
93
94def test_failure():
95 # assert False == True
96 assert_not_equal(False, True)
97
98
99# NOTES
open the project
I open a terminal
I change directory to the assertion_error folder in the
pumping_pythonfoldercd assertion_errorI open
test_assertion_error.pyI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows
tests/test_assertion_error.py ....... [100%] ================== 7 passed in D.EFs ===================I add the assert_equal function to
test_assertion_error.py1def assert_equal(x, y): 2 assert x == y 3 4 5def test_assert_keyword():I open another terminal
I change directory to the assertion_error folder in the
pumping_pythonfoldercd assertion_errorI add a git commit message
git commit -am 'add assert_equal function'
test_assert_keyword with assert_equal
RED: make it fail
I go back to the terminal where the tests are running
I use the assert_equal function for the assertions in test_assert_keyword, in
test_assertion_error.py5def test_assert_keyword(): 6 # assert 1 + 1 == 2 7 assert_equal(1+1, 3) 8 9 # assert '1' + '1' == '11' 10 assert_equal('1'+'1', '2') 11 12 # assert 'I am' + ' alive' == 'I am alive' 13 assert_equal('I am'+' alive', 'I am a ghost') 14 15 16def test_assertion_error_w_none():the terminal is my friend, and shows AssertionError
E assert 2 == 3assert_equal(1+1, 3) -> None └── def assert_equal(x, y): ├── x = 1 + 1 │ = 2 ├── y = 3 └── assert x == y assert 2 == 3
GREEN: make it pass
I change the expectation from
3to2to match reality for the first assertion in test_assert_keyword5def test_assert_keyword(): 6 # assert 1 + 1 == 2 7 # assert_equal(1+1, 3) 8 assert_equal(1+1, 2) 9 10 # assert '1' + '1' == '11' 11 assert_equal('1'+'1', '2')the terminal is my friend, and shows AssertionError
AssertionError: assert '11' == '2'assert_equal('1'+'1', '2') -> None └── def assert_equal(x, y): ├── x = '1' + '1' │ = '11' ├── y = '2' └── assert x == y assert '11' == '2'I change the expectation from
'2'to'11'to match reality for the second assertion in test_assert_keyword10 # assert '1' + '1' == '11' 11 # assert_equal('1'+'1', '2') 12 assert_equal('1'+'1', '11') 13 14 # assert 'I am' + ' alive' == 'I am alive' 15 assert_equal('I am'+' alive', 'I am a ghost')the terminal is my friend, and shows AssertionError
AssertionError: assert 'I am alive' == 'I am a ghost'assert_equal('I am'+' alive', 'I am a ghost') -> None └── def assert_equal(x, y): ├── x = 'I am' + ' alive' │ = 'I am alive' ├── y = 'I am a ghost' └── assert x == y assert 'I am alive' == 'I am a ghost'I change the expectation from
'I am a ghost'to'I am alive'for the third assertion in test_assert_keyword14 # assert 'I am' + ' alive' == 'I am alive' 15 # assert_equal('I am'+' alive', 'I am a ghost') 16 assert_equal('I am'+' alive', 'I am alive') 17 18 19def test_assertion_error_w_none():
REFACTOR: make it better
I remove the commented lines from test_assert_keyword
5def test_assert_keyword(): 6 assert_equal(1+1, 2) 7 assert_equal('1'+'1', '11') 8 assert_equal('I am'+' alive', 'I am alive') 9 10 11def test_assertion_error_w_none():I add a git commit message in the new terminal
git commit -am \ 'test_assert_keyword with assert_equal'
test_assertion_error_w_equality with assert_equal
RED: make it fail
I go back to the terminal where the tests are running
I use the assert_equal function for three assertions in test_assertion_error_w_equality, in
test_assertion_error.py77 def test_assertion_error_w_equality(): 78 # assert None == None 79 assert_equal(False, None) 80 81 assert False != None 82 83 assert False != True 84 85 # assert False == False 86 assert_equal(True, False) 87 88 assert True != None 89 90 # assert True == True 91 assert_equal(False, True) 92 93 94 def test_assertion_error_w_is_vs_equal():the terminal is my friend, and shows AssertionError
E assert False == Noneassert_equal(False, None) -> None └── def assert_equal(x, y): ├── x = False ├── y = None └── assert x == y assert False == None
GREEN: make it pass
I change False to None in the first assertion of test_assertion_error_w_equality
77def test_assertion_error_w_equality(): 78 # assert None == None 79 # assert_equal(False, None) 80 assert_equal(None, None) 81 82 assert False != None 83 84 assert False != True 85 86 # assert False == False 87 assert_equal(True, False)the terminal shows AssertionError
E assert True == Falseassert_equal(True, False) -> None └── def assert_equal(x, y): ├── x = True ├── y = False └── assert x == y assert True == FalseI change True to False in the fourth assertion of test_assertion_error_w_equality
86 # assert False == False 87 # assert_equal(True, False) 88 assert_equal(False, False) 89 90 assert True != None 91 92 # assert True == True 93 assert_equal(False, True) 94 95 96def test_assertion_error_w_is_vs_equal():the terminal is my friend, and shows AssertionError
E assert False == Trueassert_equal(False, True) -> None └── def assert_equal(x, y): ├── x = False ├── y = True └── assert x == y assert False == TrueI change False to True in the last assertion of test_assertion_error_w_equality
92 # assert True == True 93 # assert_equal(False, True) 94 assert_equal(True, True) 95 96 97def test_assertion_error_w_is_vs_equal():the test passes.
REFACTOR: make it better
I remove the commented lines from test_assertion_error_w_equality since they are now repetitions
77def test_assertion_error_w_equality(): 78 assert_equal(None, None) 79 assert_equal(False, False) 80 assert_equal(True, True) 81 82 assert False != None 83 assert False != True 84 assert True != None 85 86 87 88def test_assertion_error_w_is_vs_equal():I add a git commit message in the other terminal
git commit -am \ 'test_assertion_error_w_equality with assert_equal'
test_assertion_error_w_is_vs_equal with assert_equal
RED: make it fail
I go back to the terminal where the tests are running
I use the assert_equal function for the second assertion in test_assertion_error_w_is_vs_equal, in
test_assertion_error.py87 def test_assertion_error_w_is_vs_equal(): 88 assert 0 is not 0.0 89 90 # assert 0 == 0.0 91 assert_equal(1, 0.0) 92 93 94 def will_not_run():the terminal is my friend, and shows AssertionError
E assert 1 == 0.0assert_equal(1, 0.0) -> None └── def assert_equal(x, y): ├── x = 1 ├── y = 0.0 └── assert x == y assert 1 == 0.0
GREEN: make it pass
I change 1 to 0 in the second assertion of test_assertion_error_w_is_vs_equal
87def test_assertion_error_w_is_vs_equal():
88 assert 0 is not 0.0
89
90 # assert 0 == 0.0
91 # assert_equal(1, 0.0)
92 assert_equal(0, 0.0)
93
94
95def will_not_run():
the test passes.
REFACTOR: make it better
I remove the commented line from test_assertion_error_w_is_vs_equal
87def test_assertion_error_w_is_vs_equal(): 88 assert 0 is not 0.0 89 assert_equal(0, 0.0) 90 91 92def will_not_run():I add a git commit message in the other terminal
git commit -am \ 'test_assertion_error_w_is_vs_equal with assert_equal'
extract assert_not_equal function
The remaining assertions in test_assertion_error_w_equality and assertion in test_failure are the same, they check if something is not equal to something else.
assert something != something_else
I can use a function to assert if two things are NOT equal.
RED: make it fail
I go back to the terminal where the tests are running
I add a function named
assert_not_equalthat takes two inputs and asserts that they are NOT equal, intest_assertion_error.py1def assert_not_equal(x, y): 2 assert x != y 3 4 5def assert_equal(x, y):I use the new function for the remaining assertions in test_assertion_error_w_equality
81def test_assertion_error_w_equality(): 82 assert_equal(None, None) 83 assert_equal(False, False) 84 assert_equal(True, True) 85 86 # assert False != None 87 assert_not_equal(None, None) 88 # assert False != True 89 assert_not_equal(True, True) 90 # assert True != None 91 assert_not_equal(None, None) 92 93 94def test_assertion_error_w_is_vs_equal():the terminal is my friend, and shows AssertionError
E assert None != Noneassert_not_equal(None, None) -> None └── def assert_not_equal(x, y): ├── x = None ├── y = None └── assert x != y assert None != None
GREEN: make it pass
I change the first None in the parentheses to False for the first assertion in test_assertion_error_w_equality
86 # assert False != None 87 # assert_not_equal(None, None) 88 assert_not_equal(False, None) 89 # assert False != True 90 assert_not_equal(True, True)the terminal is my friend, and shows AssertionError
E assert True != Trueassert_not_equal(True, True) -> None └── def assert_not_equal(x, y): ├── x = True ├── y = True └── assert x != y assert True != TrueI change the first True in the parentheses to False for the second assertion in test_assertion_error_w_equality
89 # assert False != True 90 # assert_not_equal(True, True) 91 assert_not_equal(False, True) 92 # assert True != None 93 assert_not_equal(None, None) 94 95 96def test_assertion_error_w_is_vs_equal():the terminal is my friend, and shows AssertionError
E assert None != NoneI change the first None in the parentheses to True for the third assertion in test_assertion_error_w_equality
92 # assert True != None 93 # assert_not_equal(None, None) 94 assert_not_equal(True, None) 95 96 97def test_assertion_error_w_is_vs_equal():the test passes.
REFACTOR: make it better
I remove the commented lines from test_assertion_error_w_equality
81def test_assertion_error_w_equality(): 82 assert_equal(None, None) 83 assert_equal(False, False) 84 assert_equal(True, True) 85 86 assert_not_equal(False, None) 87 assert_not_equal(False, True) 88 assert_not_equal(True, None) 89 90 91def test_assertion_error_w_is_vs_equal():I use the assert_not_equal function for the assertion in test_failure
102def test_failure(): 103 # assert False == True 104 # assert False != True 105 assert_not_equal(True, True) 106 107 108# NOTESthe terminal is my friend, and shows AssertionError
E assert True != TrueI change the first True in the parentheses of the assertion to False
102def test_failure(): 103 # assert False == True 104 # assert False != True 105 # assert_not_equal(True, True) 106 assert_not_equal(False, True) 107 108 109# NOTESthe test passes.
I remove the commented lines from test_failure
102def test_failure(): 103 # assert False == True 104 assert_not_equal(False, True) 105 106 107# NOTESI add a git commit message in the other terminal
git commit -am \ 'extract assert_not_equal function'
extract assert_is_not function
The other assertions in test_assertion_error.py are the same, they check if something is NOT the same object as something else
assert something is not something_else
three of the assertions are the same, they check if something is the same object as something else
assert something is something_else
I can use a function to assert if one object is NOT the same object as another.
RED: make it fail
I go back to the terminal where the tests are running
I add a function named
assert_is_notthat takes two inputs and asserts that they are not the same object, intest_assertion_error.py1def assert_is_not(x, y): 2 assert x is not y 3 4 5def assert_not_equal(x, y):I use the assert_is_not function for the assertions in test_assertion_error_w_none
19def test_assertion_error_w_none(): 20 assert None is None 21 22 # assert False is not None 23 assert_is_not(None, None) 24 25 # assert True is not None 26 assert_is_not(None, None) 27 28 # assert 0 is not None 29 assert_is_not(None, None)31 # assert 0.0 is not None 32 assert_is_not(None, None) 33 34 # assert '' is not None 35 assert_is_not(None, None) 36 37 # assert () is not None 38 assert_is_not(None, None)40 # assert [] is not None 41 assert_is_not(None, None) 42 43 # assert set() is not None 44 assert_is_not(None, None) 45 46 # assert {} is not None 47 assert_is_not(None, None) 48 49 50def test_assertion_error_w_false():the terminal is my friend, and shows AssertionError
E assert None is not Noneassert_is_not(None, None) -> None └── def assert_is_not(x, y): ├── x = None ├── y = None └── assert x is not y assert None is not None
GREEN: make it pass
I change the first None in the parentheses to False for the second assertion in test_assertion_error_w_none
19def test_assertion_error_w_none(): 20 assert None is None 21 22 # assert False is not None 23 # assert_is_not(None, None) 24 assert_is_not(False, None) 25 26 # assert True is not None 27 assert_is_not(None, None)the terminal is my friend, and shows AssertionError
E assert None is not NoneI change the first None in the parentheses to True for the third assertion in test_assertion_error_w_none
26 # assert True is not None 27 # assert_is_not(None, None) 28 assert_is_not(True, None) 29 30 # assert 0 is not None 31 assert_is_not(None, None)the terminal is my friend, and shows AssertionError
E assert None is not NoneI change the first None in the parentheses to
0for the fourth assertion in test_assertion_error_w_none30 # assert 0 is not None 31 # assert_is_not(None, None) 32 assert_is_not(0, None) 33 34 # assert 0.0 is not None 35 assert_is_not(None, None)the terminal is my friend, and shows AssertionError
E assert None is not NoneI change the first None in the parentheses to
0.0for the fifth assertion in test_assertion_error_w_none34 # assert 0.0 is not None 35 # assert_is_not(None, None) 36 assert_is_not(0.0, None) 37 38 # assert '' is not None 39 assert_is_not(None, None)the terminal is my friend, and shows AssertionError
E assert None is not NoneI change the first None in the parentheses to
''for the sixth assertion in test_assertion_error_w_none38 # assert '' is not None 39 # assert_is_not(None, None) 40 assert_is_not('', None) 41 42 # assert () is not None 43 assert_is_not(None, None)the terminal is my friend, and shows AssertionError
E assert None is not NoneI change the first None in the parentheses to
()for the seventh assertion in test_assertion_error_w_none42 # assert () is not None 43 # assert_is_not(None, None) 44 assert_is_not((), None) 45 46 # assert [] is not None 47 assert_is_not(None, None)the terminal is my friend, and shows AssertionError
E assert None is not NoneI change the first None in the parentheses to
[]for the eighth assertion in test_assertion_error_w_none46 # assert [] is not None 47 # assert_is_not(None, None) 48 assert_is_not([], None) 49 50 # assert set() is not None 51 assert_is_not(None, None)the terminal is my friend, and shows AssertionError
E assert None is not NoneI change the first None in the parentheses to
set()for the ninth assertion in test_assertion_error_w_none50 # assert set() is not None 51 # assert_is_not(None, None) 52 assert_is_not(set(), None) 53 54 # assert {} is not None 55 assert_is_not(None, None) 56 57 58def test_assertion_error_w_false():the terminal is my friend, and shows AssertionError
E assert None is not NoneI change the first None in the parentheses to
{}for the tenth assertion in test_assertion_error_w_none54 # assert {} is not None 55 # assert_is_not(None, None) 56 assert_is_not({}, None) 57 58 59def test_assertion_error_w_false():the test passes.
I use the assert_is_not function for the first assertion in test_assertion_error_w_is_vs_equal
113def test_assertion_error_w_is_vs_equal(): 114 # assert 0 is not 0.0 115 assert_is_not(0.0, 0.0) 116 assert_equal(0, 0.0) 117 118 119def will_not_run():the terminal is my friend, and shows AssertionError
E assert 0.0 is not 0.0assert_is_not(0.0, 0.0) -> None └── def assert_is_not(x, y): ├── x = 0.0 ├── y = 0.0 └── assert x is not y assert 0.0 is not 0.0I change the first
0.0to0in the parentheses113def test_assertion_error_w_is_vs_equal(): 114 # assert 0 is not 0.0 115 # assert_is_not(0.0, 0.0) 116 assert_is_not(0, 0.0) 117 assert_equal(0, 0.0) 118 119 120def will_not_run():the test passes.
I remove the commented lines from test_assertion_error_w_is_vs_equal
113def test_assertion_error_w_is_vs_equal(): 114 assert_is_not(0, 0.0) 115 assert_equal(0, 0.0) 116 117 118def will_not_run():I add a git commit message in the other terminal
git commit -am \ 'extract assert_is_not function'
extract assert_is_not_none function
All the assertions in test_assertion_error_w_none except the first one, use the assert_is_not function to assert that something is NOT the same object as None
assert something is not None
I can use a function to remove repetition of None from the call to the assert_is_not function to assert that something is NOT the same object as None.
RED: make it fail
I go back to the terminal where the tests are running
I add a function named
assert_is_not_nonethat takes one input and calls the assert_is_not function to assert that the input is NOT the same object as None, intest_assertion_error.py1def assert_is_not(x, y): 2 assert x is not y 3 4 5def assert_is_not_none(x): 6 assert_is_not(x, None) 7 8 9def assert_not_equal(x, y):I use the assert_is_not_none function for the second assertion in test_assertion_error_w_none
23def test_assertion_error_w_none(): 24 assert None is None 25 26 # assert False is not None 27 # assert_is_not(None, None) 28 # assert_is_not(False, None) 29 assert_is_not_none(None) 30 31 # assert True is not Nonethe terminal is my friend, and shows AssertionError
E assert None is not Noneassert_is_not_none(None) -> None └── def assert_is_not_none(x): ├── x = None └── assert_is_not(x, None) └── def assert_is_not(x, y): ├── x = None ├── y = None └── assert x is not y assert None is not None
GREEN: make it pass
I change None to False for the second assertion in test_assertion_error_w_none
23 def test_assertion_error_w_none():
24 assert None is None
25
26 # assert False is not None
27 # assert_is_not(None, None)
28 # assert_is_not(False, None)
29 # assert_is_not_none(None)
30 assert_is_not_none(False)
31
32 # assert True is not None
the test passes.
assert_is_not_none(False) -> None
└── def assert_is_not_none(x):
├── x = False
└── assert_is_not(x, None)
└── def assert_is_not(x, y):
├── x = False
├── y = None
└── assert x is not y
assert False is not None
REFACTOR: make it better
I use the assert_is_not_none function for the remaining assertions in test_assertion_error_w_none
32 # assert True is not None 33 # assert_is_not(None, None) 34 # assert_is_not(True, None) 35 assert_is_not_none(True) 36 37 # assert 0 is not None 38 # assert_is_not(None, None) 39 # assert_is_not(0, None) 40 assert_is_not_none(0) 41 42 # assert 0.0 is not None 43 # assert_is_not(None, None) 44 # assert_is_not(0.0, None) 45 assert_is_not_none(0.0)47 # assert '' is not None 48 # assert_is_not(None, None) 49 # assert_is_not('', None) 50 assert_is_not_none('') 51 52 # assert () is not None 53 # assert_is_not(None, None) 54 # assert_is_not((), None) 55 assert_is_not_none(()) 56 57 # assert [] is not None 58 # assert_is_not(None, None) 59 # assert_is_not([], None) 60 assert_is_not_none([])62 # assert set() is not None 63 # assert_is_not(None, None) 64 # assert_is_not(set(), None) 65 assert_is_not_none(set()) 66 67 # assert {} is not None 68 # assert_is_not(None, None) 69 # assert_is_not({}, None) 70 assert_is_not_none({}) 71 72 73def test_assertion_error_w_false():the test is still green.
I remove the commented lines from test_assertion_error_w_none
23def test_assertion_error_w_none(): 24 assert None is None 25 26 assert_is_not_none(False) 27 assert_is_not_none(True) 28 assert_is_not_none(0) 29 assert_is_not_none(0.0) 30 assert_is_not_none('') 31 assert_is_not_none(()) 32 assert_is_not_none([]) 33 assert_is_not_none(set()) 34 assert_is_not_none({}) 35 36 37def test_assertion_error_w_false():I add a git commit message in the other terminal
git commit -am \ 'extract assert_is_not_none function'
extract assert_is_not_false function
The assertions in test_assertion_error_w_false except assert False is False are the same, they check if something is NOT the same object as False
assert something is not False
I can use a function to assert that something is NOT the same object as False.
RED: make it fail
I go back to the terminal where the tests are running
I add a function named
assert_is_not_falsethat takes one input and calls the assert_is_not function to assert that the input is NOT the same object as False, intest_assertion_error.py5def assert_is_not_none(x): 6 assert_is_not(x, None) 7 8 9def assert_is_not_false(x): 10 assert_is_not(x, False) 11 12 13def assert_not_equal(x, y):I use the assert_is_not_false function for the first assertion in test_assertion_error_w_false
41def test_assertion_error_w_false(): 42 # assert None is not False 43 assert_is_not_false(False) 44 45 assert False is Falsethe terminal is my friend, and shows AssertionError
E assert False is not Falseassert_is_not_false(False) -> None └── def assert_is_not_false(x): ├── x = False └── assert_is_not(x, False) └── def assert_is_not(x, y): ├── x = False ├── y = False └── assert x is not y assert False is not False
GREEN: make it pass
I change False to None for the first assertion in test_assertion_error_w_false
41def test_assertion_error_w_false():
42 # assert None is not False
43 # assert_is_not_false(False)
44 assert_is_not_false(None)
45
46 assert False is False
the test passes.
assert_is_not_false(None) -> None
└── def assert_is_not_false(x):
├── x = None
└── assert_is_not(x, False)
└── def assert_is_not(x, y):
├── x = None
├── y = False
└── assert x is not y
assert None is not False
REFACTOR: make it better
I use the assert_is_not_false function for the remaining assertions in test_assertion_error_w_false
46 assert False is False 47 48 # assert True is not False 49 assert_is_not_false(True) 50 51 # assert 0 is not False 52 assert_is_not_false(0) 53 54 # assert 0.0 is not False 55 assert_is_not_false(0.0)57 # assert '' is not False 58 assert_is_not_false('') 59 60 # assert () is not False 61 assert_is_not_false(()) 62 63 # assert [] is not False 64 assert_is_not_false([])66 # assert set() is not False 67 assert_is_not_false(set()) 68 69 # assert {} is not False 70 assert_is_not_false({}) 71 72 73def test_assertion_error_w_true():the test is still green.
I remove the commented lines from test_assertion_error_w_false
41def test_assertion_error_w_false(): 42 assert False is False 43 44 assert_is_not_false(None) 45 assert_is_not_false(True) 46 assert_is_not_false(0) 47 assert_is_not_false(0.0) 48 assert_is_not_false('') 49 assert_is_not_false(()) 50 assert_is_not_false([]) 51 assert_is_not_false(set()) 52 assert_is_not_false({}) 53 54 55def test_assertion_error_w_true():I add a git commit message in the other terminal
git commit -am \ 'extract assert_is_not_false function'
extract assert_is_not_true function
The assertions in test_assertion_error_w_true except assert True is True are the same, they check if something is NOT the same object as True
assert something is not True
I can use a function to assert that something is NOT the same object as True.
RED: make it fail
I go back to the terminal where the tests are running
I add a function named
assert_is_not_truethat takes one input and calls the assert_is_not function to assert that the input is NOT the same object as True, intest_assertion_error.py9def assert_is_not_false(x): 10 assert_is_not(x, False) 11 12 13def assert_is_not_true(x): 14 assert_is_not(x, True) 15 16 17def assert_not_equal(x, y):I use the assert_is_not_true function for the first assertion in test_assertion_error_w_true
59def test_assertion_error_w_true(): 60 # assert None is not True 61 assert_is_not_true(True) 62 63 assert False is not Truethe terminal is my friend, and shows AssertionError
E assert True is not Trueassert_is_not_true(True) -> None └── def assert_is_not_true(x): ├── x = True └── assert_is_not(x, True) └── def assert_is_not(x, y): ├── x = True ├── y = True └── assert x is not y assert True is not True
GREEN: make it pass
I change True to None for the first assertion in test_assertion_error_w_true
59def test_assertion_error_w_true():
60 # assert None is not True
61 # assert_is_not_true(True)
62 assert_is_not_true(None)
63
64 assert False is not True
the test passes.
assert_is_not_true(None) -> None
└── def assert_is_not_true(x):
├── x = None
└── assert_is_not(x, True)
└── def assert_is_not(x, y):
├── x = None
├── y = True
└── assert x is not y
assert None is not True
REFACTOR: make it better
I use the assert_is_not_true function for the remaining assertions in test_assertion_error_w_true
64 # assert False is not True 65 assert_is_not_true(False) 66 67 assert True is True 68 69 # assert 0 is not True 70 assert_is_not_true(0)72 # assert 0.0 is not True 73 assert_is_not_true(0.0) 74 75 # assert '' is not True 76 assert_is_not_true('') 77 78 # assert () is not True 79 assert_is_not_true(())81 # assert [] is not True 82 assert_is_not_true([]) 83 84 # assert set() is not True 85 assert_is_not_true(set()) 86 87 # assert {} is not True 88 assert_is_not_true({}) 89 90 91def test_assertion_error_w_equality():the test is still green.
I remove the commented lines from test_assertion_error_w_true
59def test_assertion_error_w_true(): 60 assert True is True 61 62 assert_is_not_true(None) 63 assert_is_not_true(False) 64 assert_is_not_true(0) 65 assert_is_not_true(0.0) 66 assert_is_not_true('') 67 assert_is_not_true(()) 68 assert_is_not_true([]) 69 assert_is_not_true(set()) 70 assert_is_not_true({}) 71 72 73def test_assertion_error_w_equality():I add a git commit message in the other terminal
git commit -am \ 'extract assert_is_not_true function'
close the project
I close
test_assertion_error.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests. The terminal goes back to the command line.
I change directory to the parent of
assertion_errorcd ..the terminal is my friend, and shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I can use functions to make assertions.
I have a problem with the functions I used to make assertions - I had to write the assert_equal function in this project, even though I wrote it in the previous project. There has to be a better way where I can write the function once, then use it any time or anywhere I want.
code from the chapter
what is next?
Would you like to test using a function to make a string from input?
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.