what is an assertion?
An assertion or assert statement is a way for me to tell Python, “DO NOT CONTINUE, if this statement is False”, or said a different way “GO TO THE NEXT LINE, ONLY if this statement is True”.
I use assertions in tests when making a program to make sure something is True about the program before I continue building. I use them to test ideas, without worrying about if I will remember the ideas later. I use them to test how programs behave, for example when given inputs.
Assertions can help catch things that make tests that were passing, start failing when I add new lines of code. They help me answer 2 questions
what is the same?
what is different?
The difference between my expectations (tests and ideas) and reality (what happens when the program runs), tells me what to change to make them match. The closer my program is to reality, the better.
preview
I have these tests by the end of the chapter
1def test_assert_keyword():
2 assert 1 + 1 == 2
3
4 assert '1' + '1' == '11'
5
6 assert 'I am' + ' alive' == 'I am alive'
9def test_assertion_error_w_none():
10 assert None is None
11
12 assert False is not None
13
14 assert True is not None
15
16 assert 0 is not None
17
18 assert 0.0 is not None
19
20 assert '' is not None
21
22 assert () is not None
23
24 assert [] is not None
25
26 assert set() is not None
27
28 assert {} is not None
31def test_assertion_error_w_false():
32 assert None is not False
33
34 assert False is False
35
36 assert True is not False
37
38 assert 0 is not False
39
40 assert 0.0 is not False
41
42 assert '' is not False
43
44 assert () is not False
45
46 assert [] is not False
47
48 assert set() is not False
49
50 assert {} is not False
53def test_assertion_error_w_true():
54 assert None is not True
55
56 assert False is not True
57
58 assert True is True
59
60 assert 0 is not True
61
62 assert 0.0 is not True
63
64 assert '' is not True
65
66 assert () is not True
67
68 assert [] is not True
69
70 assert set() is not True
71
72 assert {} is not True
75def test_assertion_error_w_equality():
76 assert None == None
77
78 assert False != None
79
80 assert False != True
81
82 assert False == False
83
84 assert True != None
85
86 assert True == True
89def test_assertion_error_w_is_vs_equal():
90 assert 0 is not 0.0
91
92 assert 0 == 0.0
93
94
95def will_not_run():
96 # will not run because
97 # the name does not start with test
98 assert False == True
99
100
101def test_failure():
102 # assert False == True
103 assert False != True
106# NOTES
107# a dictionary is not the same object as True
108# a dictionary is not the same object as False
109# a dictionary is not the same object as None
110# a set is not the same object as True
111# a set is not the same object as False
112# a set is not the same object as None
113# a list is not the same object as True
114# a list is not the same object as False
115# a list is not the same object as None
116# a tuple is not the same object as True
117# a tuple is not the same object as False
118# a tuple is not the same object as None
119# a string is not the same object as True
120# a string is not the same object as False
121# a string is not the same object as None
122# a float is not the same object as True
123# a float is not the same object as False
124# a float is not the same object as None
125# an integer is not the same object as True
126# an integer is not the same object as False
127# an integer is not the same object as None
128# True is True and equal to True
129# True is not equal to False
130# True is not the same object as False
131# True is not equal to None
132# True is not the same object as None
133# False is not equal to True
134# False is not the same object as True
135# False is False and equal to False
136# False is not equal to None
137# False is not the same object as None
138# None is not equal to True
139# None is not the same object as True
140# None is not equal to False
141# None is not the same object as False
142# None is None and equal to None
143
144# Exceptions seen
145# AssertionError
146# IndentationError
questions about AssertionError
start the project
I name this project
assertion_errorI open a terminal
I change directory to the
assertion_errorfolder in thepumping_pythonfolderthe terminal shows
cd: no such file or directory: assertion_errorthere is no folder with the name
assertion_errorin this folder.I use uv to make a directory for the project and initialize it
the terminal shows
Initialized project `assertion-error` at `.../pumping_python/assertion_error`then goes back to the command line.
I change directory to the project
the terminal shows I am in the
assertion_errorfolder.../pumping_python/assertion_errorI make a directory for the tests
the terminal goes back to the command line.
I make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line.
I make an empty file for the tests in the
testsfoldertouch tests/test_assertion_error.pyNew-Item tests/test_assertion_error.pythe terminal goes back to the command line.
I open
test_assertion_error.pyfrom thetestsfolderI add the first failing test to
tests/test_assertion_error.py1assert False is TrueI go back to the terminal
I use echo to add pytest to
requirements.txtecho "pytest" > requirements.txtI add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line.
I use Out-File to add pytest to
requirements.txt"pytest" | Out-File requirements.txt -Encoding UTF8I add pytest-watcher to the requirements file
"pytest-watcher" >> requirements.txt
I use uv to install pytest-watcher with the requirements file
the terminal shows that it installed pytest-watcher and its dependencies.
I add the new files and folder to git for tracking
the terminal goes back to the command line.
I add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
I use pytest-watcher to run the tests automatically
the terminal is my friend, and shows AssertionError
========================== ERRORS ========================== ______ ERROR collecting tests/test_assertion_error.py ______ tests/test_assertion_error.py:2: in <module> assert False is True E assert False is True ================= short test summary info ================== ERROR tests/test_assertion_error.py - assert False is True !!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!! ===================== 1 error in I.JKs =====================if the terminal does not show the same error, then check if
your
tests/__init__.pyhas two underscores (__) before and afterinitfor__init__.pynot_init_.pyyou ran
echo "pytest-watcher" >> requirements.txt, to addpytest-watcherto the requirements file
and try
uv run pytest-watcher . --now --patterns "**/*.py"againI add AssertionError to the list of Exceptions seen, in
test_assertion_error.py1assert False is True 2 3 4# Exceptions seen 5# AssertionErrorI change True to False in the assertion
1# assert False is True 2assert False is False 3 4 5# Exceptions seen 6# AssertionErrorthe test passes, the terminal shows
no tests ran.
the assert keyword
We know that the result of 1 + 1 is 2. What if I said that the result of '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 remove the first failing assertion then add the first of the two statements I am testing
11 + 1 == 2 2 3 4# Exceptions seen 5# AssertionError==is two equal signs on the keyboard (=+=) and meansis equalwhich makes this statement read as1 + 1 is equal to 2I change the result of the equation
1# 1 + 1 == 2 21 + 1 == 11 3 4 5# Exceptions seen 6# AssertionErrornothing happens. Why is it still passing?
1 + 1cannot be both2and11, is Python broken?I want the test to fail if I write a statement that is NOT True. I change it to an assert statement
1# 1 + 1 == 2 2# 1 + 1 == 11 3assert 1 + 1 == 11 4 5 6# Exceptions seen 7# AssertionErrorthe terminal is my friend, and shows
E assert (1 + 1) == 11because if there is no assert before the equation, Python does not care if the statement is False or True as long as it follows Python rules. Once I make the statement an assertion with the assert keyword it tells the computer
DO NOT CONTINUE, if "1 + 1 == 11" is False.
GREEN: make it pass
I change the result back, to make the statement True
1# 1 + 1 == 2
2# 1 + 1 == 11
3# assert 1 + 1 == 11
4assert 1 + 1 == 2
5
6
7# Exceptions seen
the test passes.
REFACTOR: make it better
I add the other statement I am testing (
'1' + '1' == '11')1# 1 + 1 == 2 2# 1 + 1 == 11 3# assert 1 + 1 == 11 4assert 1 + 1 == 2 5 6'1' + '1' == '2' 7 8 9# Exceptions seenthe terminal still shows
no tests ranbecause the statement is not an assertion.I add assert before the statement to make it an assertion
1# 1 + 1 == 2 2# 1 + 1 == 11 3# assert 1 + 1 == 11 4assert 1 + 1 == 2 5 6# '1' + '1' == '2' 7assert '1' + '1' == '2' 8 9 10# Exceptions seenthe terminal is my friend, and shows AssertionError
E AssertionError: assert ('1' + '1') == '2'because the assert before the statement makes it a command to Python -
DO NOT CONTINUE if "'1' + '1' == '2'" is False
what causes AssertionError?
AssertionError is raised if the statement after the assert keyword is False. It was in the first failing assertion
assert False is True
With this statement, I tell Python - “DO NOT CONTINUE, if False is NOT the same object as True”, or said a different way “GO TO THE NEXT LINE, ONLY if False is the same object as True”.
I change the statement to make it True
the test passes because the statement is now True,
'1' + '1'is equal to'11'.These two statements are similar and are NOT the same
assert 1 + 1 == 2checks if the result of adding two numbers (1 + 1) is equal to the number on the right side (2) of the equality symbol (==).assert '1' + '1' == '11'checks if the result of “adding” 2 strings ('1' + '1') is equal to the string on the right side ('11') of the equality symbol (==). A string is anything inside quotes.
I add another statement to show the “addition” of two strings
the test is still green because
'I am' + ' alive' == '11'is just a statement. Python does not care whether it is False or True and the statement follows Python rules.I change the statement to an assertion with the assert keyword
the 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 FalseI change the statement to make it True
the test passes.
the terminal shows
no tests ranwhich is confusing since the only way I know the test passed, is because I saw it fail. There has to be a better way.
I remove the commented lines
1assert 1 + 1 == 2 2 3assert '1' + '1' == '11' 4 5assert 'I am' + ' alive' == 'I am alive' 6 7 8# Exceptions seen 9# AssertionErrorI open a new terminal then change directories to
assertion_errorthe terminal shows I am in the
assertion_errorfolder.../pumping_python/assertion_errorI add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
test AssertionError with None
None is used when there is no value. 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 statement to
test_assertion_error.pythe test is still passing
I add the assert keyword
the 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
7 # None is not None
8 # assert None is not None
9 assert None is None
10
11 # Exceptions seen
the test passes.
REFACTOR: make it better
I add a note with what I learned from the experiment
I add a new assertion to compare None with False.
the terminal shows
E assert False is NoneI change the assert statement to make it True
the test passes.
I add a note about False
I add an assertion to compare None with True.
the terminal shows
E assert True is NoneI change the assert statement to make it True
the test passes.
I add a note about True
I add an assert statement for an integer (a whole number without decimals)
the terminal shows
E assert 0 is NoneI change the statement to make it True
the test passes.
I add a note about integers
I add an assert statement for a float (binary floating point decimal number)
the terminal shows
E assert 0.0 is NoneI change the statement to make it True
the test passes.
I add a note about floats
I add an assert statement for a string (anything in quotes)
the terminal is my friend, and shows AssertionError
E AssertionError: assert '' is NoneI change the statement to make it True
the test passes because a string is not the same object as None.
I add a note about strings
I add an assert statement for a tuple (anything in parentheses
( )separated by a comma)the terminal shows
E assert () is NoneI change the statement to make it True
the test passes because a tuple is not the same object None.
I add a note about tuples
I add an assert statement for a list (anything in square brackets
[ ])the terminal shows
E assert [] is NoneI change the statement to make it True
the test passes because a list is not the same object as None.
I add a note about lists
I add an assert statement for a set
the terminal is my friend, and shows AssertionError
E assert set() is NoneI change the statement to make it True
the test passes.
I add a note about sets
I add an assert statement for a dictionary
the terminal shows
E assert {} is NoneI change the statement to make it True
the test passes.
I add a note about dictionaries
I remove the commented lines
I add comments to group the assertions
1# test assert keyword 2assert 1 + 1 == 2 3 4assert '1' + '1' == '11' 5 6assert 'I am' + ' alive' == 'I am alive' 7 8 9# test AssertionError with None 10assert None is NoneI add a git commit message in the other terminal
the terminal shows a summary of the changes then goes back to the command line.
test AssertionError with False
I can use assertions to test if something is the same object as False or not.
False is one of the two booleans and assert False is not None shows that False is NOT None.
RED: make it fail
I go back to the terminal where the tests are running
False is NOT the same object as None. Is None the same object as False? I add a new statement to
test_assertion_error.pywith a comment to group the new statements that will come afterthe test is still passing.
I add the assert keyword
the terminal is my friend, and shows
E assert None is False
GREEN: make it pass
I change the assertion to make it True
31 # test AssertionError with False
32 # None is False
33 # assert None is False
34 assert None is not False
35
36
37 # NOTES
the test passes because None is not the same object as False.
REFACTOR: make it better
I add a note about None
I add a failing assert statement about False
the terminal shows
E assert False is not Falsebecause False is False.
I change the statement to make it True
the test passes because False is False.
I add a note about False
I add an assert statement to see if True is the same object as False
the terminal
E assert True is FalseI change the statement to make it True
the test passes because True is not the same object as False.
I add a note about True
I add an assert statement to see if an integer (a whole number without decimals) is the same object as False
the terminal shows
E assert 0 is FalseI change the statement to make it True
the test passes because an integer is not the same object as False.
I add a note about integers
I add an assert statement to see if a float (binary floating point decimal number) is the same object as False
the terminal shows
E assert 0.0 is FalseI change the statement to make it True
the test passes because a float is not the same object as False.
I add a note about floats
I add an assert statement to see if a string (anything in quotes) is the same object as False
the terminal shows AssertionError
E AssertionError: assert '' is FalseI change the statement to make it True
the test passes because a string is not the same object as False.
I add a note about strings
I add an assert statement to see if a tuple (anything in parentheses
( )separated by a comma) is the same object as Falsethe terminal shows
E assert () is FalseI change the statement to make it True
the test passes because a tuple is not the same object as False.
I add a note about tuples
I add an assert statement to see if a list (anything in square brackets
[ ]) is the same object as Falsethe terminal shows
E assert [] is FalseI change the statement to make it True
the test passes because a list is not the same object as False.
I add a note about lists
I add an assert statement to see if a set is the same object as False
the terminal shows
E assert set() is FalseI change the statement to make it True
the test passes because a set is not the same object as False.
I add a note about sets
I add an assert statement to see if a dictionary is the same object as False
the terminal shows
E assert {} is Falsebecause a dictionary is not the same object as False.
I change the statement to make it True
the test passes because a dictionary is not the same object as False.
I add a note about dictionaries
I remove the commented assertions
I add a git commit message in the other terminal
the terminal shows a summary of the changes then goes back to the command line.
test AssertionError with 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 new statement to
test_assertion_error.pywith a comment to group these new statementsthe test is still passing.
I add the assert keyword
the terminal is my friend, and shows
E assert None is True
GREEN: make it pass
I change the statement to make it True
53# test AssertionError with True
54# None is True
55# assert None is True
56assert None is not True
57
58
59# NOTES
the test passes because None is not the same object as True.
REFACTOR: make it better
I add a note about None
True is not the same object as False. Is False is the same object as True? I add an assert statement
the terminal shows
E assert False is TrueI change the statement to make it True
the test passes because False is not the same object as True.
I add a note about False
I add an assert statement about True that will fail
the terminal shows
E assert True is not Truebecause True is True.
the test passes because True is True.
I add a note about True
I add an assert statement to see if an integer (a whole number without decimals) is the same object as True
the terminal shows
E assert 0 is TrueI change the statement to make it True
the test passes because an integer is not the same object as True.
I add a note about integers
I add an assert statement to see if a float (binary floating point decimal number) is the same object as True
the terminal shows
E assert 0.0 is TrueI change the statement to make it True
the test passes because a float is not the same object as True.
I add a note about floats
I add an assert statement to see if a string (anything in quotes) is the same object as True
the terminal shows AssertionError
E AssertionError: assert '' is TrueI change the statement to make it True
the test passes because a string is not the same object as True.
I add a note about strings
I add an assert statement to see if a tuple (anything in parentheses
( )separated by a comma) is the same object as Truethe terminal shows
E assert () is TrueI change the statement to make it True
the test passes.
I add a note about tuples
I add an assert statement to see if a list (anything in square brackets
[ ]) is the same object as Truethe terminal shows
E assert [] is TrueI change the statement to make it True
the test passes because a list is not the same object as True.
I add a note about lists
I add an assert statement to see if a set is the same object as True
the terminal shows
E assert set() is TrueI change the statement to make it True
the test passes because a set is not the same object as True.
I add a note about sets
I add an assert statement to see if a dictionary is the same object as True
the terminal shows
E assert {} is Truebecause a dictionary is not the same object as True.
I change the statement to make it True
the test passes because a dictionary is not the same object as True.
I add a note about dictionaries
I remove the commented assertions
I add a git commit message in the other terminal
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 AssertionError with equality
All the assertions I have typed so far show that True, False and None are different objects, which gives me a basic expectation of Python because I can compare things with those three.
I can also use assertions to test if two things are equal, like I did when I test assert keyword.
RED: make it fail
I go back to the terminal where the tests are running
I add a new statement to
test_assertion_error.pywith a comment to group these new statementsthe test is still passing.
!=is !+= on the keyboard and meansis NOT equalwhich makes this statement read asNone is NOT equal to None.
I add the assert keyword
the terminal is my friend, and shows
E assert None != Nonebecause None is equal to None.
GREEN: make it pass
I change the statement to make it True
75# test AssertionError with equality
76# None != None
77# assert None != None
78assert None == None
79
80
81# NOTES
the test passes because None is equal to None
==is two equal signs on the keyboard (=+=) and meansis equalwhich makes this statement read asNone is equal to None.
REFACTOR: make it better
I add to the
None is NonenoteI add an assertion to compare False with None
the terminal is my friend, and shows
E assert False == Nonebecause False is NOT equal to None and None is NOT equal to False.
I change the assert statement to make it True
the test passes because False is NOT equal to None and None is NOT equal to False.
I add notes about False and None because equality goes both ways
I add an assertion to compare False with True
the terminal is my friend, and shows
E assert False == Truebecause False is NOT equal to True and True is NOT equal to False.
I change the assert statement to make it True
the test passes because False is NOT equal to True and True is NOT equal to False.
I add notes about False and True because equality goes both ways
I add an assertion to compare False with itself
the terminal is my friend, and shows
E assert False != Falsebecause False is equal to False.
I make the statement True
the test passes because False is equal to False.
I add to the
False is Falsenote
I add a new failing assertion to compare True with None
the terminal is my friend, and shows
E assert True == Nonebecause True is NOT equal to None and None is NOT equal to True.
I change the assert statement to make it True
the test passes because True is NOT equal None and None is NOT equal to True.
I add notes about True and None because equality goes both ways
I add an assertion to compare True with itself
the terminal is my friend, and shows
E assert True != Truebecause True is equal to True.
I make the statement True
the test passes because True is equal to True.
I add to the
True is TruenoteI remove the commented assertions
I add a git commit message in the other terminal
the terminal shows a summary of the changes then goes back to the command line.
test AssertionError with 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 ==?
In Python they do not mean the same thing
x is ystates thatxis the same exact object asyx == ystates thatxis equal toyx is not ystates thatxis NOT the same exact object asyx != ystates thatxis NOT equal toy
Things can be equal without being the same exact object. For example, an integer (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
GREEN: make it pass
I change the assertion to make the statement True
89 # test AssertionError with is vs equal
90 # 0 is 0.0
91 # assert 0 is 0.0
92 assert 0 is not 0.0
93
94
95 # NOTES
the test passes because an integer is not the same object as a float.
REFACTOR: make it better
I add another assertion, this time with
!=the terminal is my friend, and shows
E assert 0 != 0.0because
0and0.0are equal, they are both zero.I change the assertion to make the statement True
the test passes because
0and0.0are equal, they are both zero.I remove the commented assertions
I add a git commit message in the other terminal
the terminal shows a summary of the changes then goes back to the command line.
The tests show that an integer can be EQUAL to a float but an integer IS NOT a float.
a better way to organize tests
I used comments to group the assertions by what I was testing, and each time I made an assertion True, the terminal showed a confusing message (no tests ran).
Since I am using pytest-watcher which uses pytest to run the test, I can use functions to group the assertions.
A function is code that is callable, this means I can write code to do something one time, and call the name for the function to do that thing at a different time from when I write the code.
functions are made with
the def keyword
a name
parentheses and a colon at the end
the code that makes up the function (its body) comes after the colon
def name_of_function():
the body of the function
...
test_assertion_error_w_is_vs_equal
I go back to the terminal where the tests are running
I add a function for the
# test AssertionError with is vs equalgroup of assertionsthe terminal is my friend, and shows IndentationError
IndentationError: expected an indented block after function definition on line 89because in Python the body of a function is indented under the name.
I add IndentationError to the list of Exceptions seen
I indent the assertions with four spaces to make them the body of the function
the test passes and the terminal still shows
no tests ranI add
test_to the name of the functionthe terminal shows green and the name of the test file with a better message
================== test session starts =================== platform linux -- Python 3.X.Y, pytest-Z.A.B, pluggy-C.D.E rootdir: /.../pumping_python/assertion_error configfile: pyproject.toml collected 1 item tests/test_assertion_error.py . [100%] =================== 1 passed in F.GHs ==================== [pytest-watcher] Current runner args: [] Press w to show menufantastic!
I remove the commented lines
I add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
pytest only calls the function if the name starts with test.
test_assertion_error_w_equality
I go back to the terminal where the tests are running
I add a function for the
# test AssertionError with equalitygroup of assertionsthe terminal is my friend, and shows IndentationError
IndentationError: expected an indented block after function definition on line 75because in Python the body of a function is indented under the name.
I indent the assertions with four spaces to make them the body of the function
the terminal shows
=================== 1 passed in L.MNs ====================I have two functions with assertions and it only shows one.
I add
test_to the name of the functionthe terminal shows
=================== 2 passed in O.PQs ====================because pytest only calls the function if the name starts with
test.I remove the commented lines
I add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
test_assertion_error_w_true
I go back to the terminal where the tests are running
I add a function for the
# test AssertionError with Truegroup of assertionsthe terminal is my friend, and shows IndentationError
IndentationError: expected an indented block after function definition on line 53because in Python the body of a function is indented under the name.
I indent the assertions with four spaces to make them the body of the function
the terminal shows
=================== 2 passed in R.STs ====================I have three functions with assertions and it only shows two.
I add
test_to the name of the functionthe terminal shows
=================== 3 passed in U.VWs ====================because pytest only calls the function if the name starts with
test.I remove the commented lines
I add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
test_assertion_error_w_false
I go back to the terminal where the tests are running
I add a function for the
# test AssertionError with Falsegroup of assertionsthe terminal is my friend, and shows IndentationError
IndentationError: expected an indented block after function definition on line 31because in Python the body of a function is indented under the name.
I indent the assertions with four spaces to make them the body of the function
the terminal shows
=================== 3 passed in X.YZs ====================I have four functions with assertions and it only shows three.
I add
test_to the name of the functionthe terminal shows
=================== 4 passed in A.BCs ====================because pytest only calls the function if the name starts with
test.I remove the commented lines
I add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
test_assertion_error_w_none
I go back to the terminal where the tests are running
I add a function for the
# test AssertionError with Nonegroup of assertionsthe terminal is my friend, and shows IndentationError
IndentationError: expected an indented block after function definition on line 9because in Python the body of a function is indented under the name.
I indent the assertions with four spaces to make them the body of the function
the terminal shows
=================== 4 passed in D.EFs ====================I have five functions with assertions and it only shows four.
I add
test_to the name of the functionthe terminal shows
=================== 5 passed in G.HIs ====================because pytest only calls the function if the name starts with
test.I remove the commented lines
I add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
test_assert_keyword
I go back to the terminal where the tests are running
I add a function for the
# test assert keywordgroup of assertions1def assert_keyword(): 2# test assert keyword 3assert 1 + 1 == 2the terminal is my friend, and shows IndentationError
IndentationError: expected an indented block after function definition on line 1because in Python the body of a function is indented under the name.
I indent the assertions with four spaces to make them the body of the function
1def assert_keyword(): 2 # test assert keyword 3 assert 1 + 1 == 2 4 5 assert '1' + '1' == '11' 6 7 assert 'I am' + ' alive' == 'I am alive' 8 9 10def test_assertion_error_w_none():the terminal shows
=================== 5 passed in J.KLs ====================I have six functions with assertions and it only shows five.
I add
test_to the name of the function1# def assert_keyword(): 2def test_assert_keyword(): 3 # test assert keyword 4 assert 1 + 1 == 2 5 6 assert '1' + '1' == '11' 7 8 assert 'I am' + ' alive' == 'I am alive' 9 10 11def test_assertion_error_w_none():the terminal shows
=================== 6 passed in M.NOs ====================because pytest only calls the function if the name starts with
testI remove the commented lines
1def test_assert_keyword(): 2 assert 1 + 1 == 2 3 4 assert '1' + '1' == '11' 5 6 assert 'I am' + ' alive' == 'I am alive' 7 8 9def test_assertion_error_w_none():I add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
pytest only calls the function if the name starts with test
I go back to the terminal where the tests are running
I add a function to show that pytest only calls the function if the name starts with
testthe terminal still shows
=================== 6 passed in P.QRs ====================I add a function that starts with
testwith the same assertionthe terminal is my friend, and shows AssertionError
================== test session starts =================== platform linux -- Python 3.X.Y, pytest-Z.A.B, pluggy-C.D.E rootdir: /.../pumping_python/assertion_error configfile: pyproject.toml collected 7 items tests/test_assertion_error.py ......F [100%] ======================== FAILURES ======================== ______________________ test_failure ______________________ def test_failure(): > assert False == True E assert False == True tests/test_assertion_error.py:102: AssertionError ============== short test summary info =================== FAILED tests/test_assertion_error.py::test_failure - assert False == True ============== 1 failed, 6 passed in T.UVs =============== [pytest-watcher] Current runner args: [] Press w to show menumore details than when I write the assertions without a function.
I change the assertion to make it True
the test passes. I leave the commented line as a reminder of what will cause the failure.
I add a git commit message
the terminal shows a summary of the changes then goes back to the command line.
pytest only calls the function if the name starts with test.
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 assertions to test if something is None or NOT None.
I can use assertions to test if something is False or NOT False.
I can use assertions to test if something is True or NOT True.
I can use assertions to test if two things are NOT equal or equal.
I can use functions to group assertions and get better error messages from pytest.
The tests show that
code from the chapter
what is next?
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.