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_pythonfolderI open
test_assertion_error.pyI use pytest-watcher to run the tests automatically
the 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_pythonfolderI add a git commit message
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.pythe 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_keywordthe terminal is my friend, and shows AssertionError
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_keywordthe terminal is my friend, and shows AssertionError
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_keyword
REFACTOR: make it better
I remove the commented lines from test_assert_keyword
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.pythe 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
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
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
the test passes.
REFACTOR: make it better
I remove the commented lines from test_assertion_error_w_equality since they are now repetitions
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.pythe 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
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
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
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
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
the test passes.
REFACTOR: make it better
I remove the commented lines from test_assertion_error_w_equality
I use the assert_not_equal function for the assertion in test_failure
the terminal is my friend, and shows AssertionError
E assert True != TrueI change the first True in the parentheses of the assertion to False
the test passes.
I remove the commented lines from test_failure
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
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
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
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_nonethe 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_nonethe 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_nonethe 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_nonethe 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_nonethe 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_nonethe 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_nonethe test passes.
I use the assert_is_not function for the first assertion in test_assertion_error_w_is_vs_equal
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 parenthesesthe test passes.
I remove the commented lines from test_assertion_error_w_is_vs_equal
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
the 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
the test is still green.
I remove the commented lines from test_assertion_error_w_none
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.pyI use the assert_is_not_false function for the first assertion in test_assertion_error_w_false
the 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
the test is still green.
I remove the commented lines from test_assertion_error_w_false
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.pyI use the assert_is_not_true function for the first assertion in test_assertion_error_w_true
the 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
the test is still green.
I remove the commented lines from test_assertion_error_w_true
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_errorthe 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.