Reputation: 3245
I like to see some meaningful description at assertion failure.
Here is my code and its execution:
>cat /tmp/1.py
a="aaa" + "bbb"
print(a)
assert ("hello" + a) and 0
>python /tmp/1.py
aaabbb
Traceback (most recent call last):
File "/tmp/1.py", line 3, in <module>
assert ("hello" + a) and 0
AssertionError
I am using Python 3.7.
You know why "hello" + a
is not evaluated first as a string concatenation? And how can I make it?
Upvotes: 2
Views: 1488
Reputation: 45826
According to the docs, the failure message follows a comma:
assert some_condition, "This is the assert failure message".
This is equivalent to:
if __debug__:
if not some_condition:
raise AssertionError("This is the assert failure message")
And as noted in the comments, assert
is not a function call. Don't add parenthesis, or you may have odd results. assert(condition, message)
will be interpreted as a tuple being used as a condition with no message, and will never fail.
Upvotes: 7
Reputation: 735
You can add your description just after your assert
statement with a comma.
Such as:
assert ("hello" + a) and 0, 'Your description'
The result will be:
aaabbb
Traceback (most recent call last):
File "test.py", line 6, in <module>
assert ("hello" + a) and 0, "Your description"
AssertionError: Your description
Upvotes: 2