
Python assert statement: How to set the error message in the AssertionError
Python assert
checks a condition and raises the AssertionError if it is false. No error happens in the following code.
assert True
Python raises the AssertionError if the statement is false.
assert False
# AssertionError
Simple Examples
x = 3
assert x == 2
# Traceback (most recent call last):
# File "/Users/serif/python/test/util/tuple.py", line 3, in <module>
# assert x == 2
# AssertionError
Python raises the AssertionError if the condition is false.
a = 1
b = 2
assert a + 1 == b
Error message
a = 1
b = 2
assert a + 2 == b, 'Not Equal'
# AssertionError: Not Equal
You can set an error message that is printed only when a condition is false. There needs a comma between the condition and message.
Comments
Powered by Markdown