Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python assert, how to print the condition when the assertion failed?

In Python, I can do:

assert result==100, "result should be 100"

but this is redundant since I have to type the condition twice.

Is there a way to tell Python to automatically show the condition when the assertion fails?

like image 244
Erel Segal-Halevi Avatar asked Feb 14 '18 09:02

Erel Segal-Halevi


People also ask

What happens when assert fails in Python?

If an assertion fails, then your program should crash because a condition that was supposed to be true became false. You shouldn't change this intended behavior by catching the exception with a try … except block. A proper use of assertions is to inform developers about unrecoverable errors in a program.

How do you handle assert failed exception in Python?

If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError. AssertionError exceptions can be caught and handled like any other exception using the try-except statement, but if not handled, they will terminate the program and produce a traceback.

What will happen if assert command fails?

Assert command in selenium: When an “assert” command fails, the test execution will be aborted. So when the Assertion fails, all the test steps after that line of code are skipped. The solution to overcoming this issue is to use a try-catch block.

How do you use assert in if condition in Python?

The assert statement is used to validate input when debugging code. If the if statement's condition is False, the code continues from where we want it to. When an assert statement is False, an error is thrown and the code execution stops completely. An if statement is always there.


2 Answers

From Jupyter notebook

This happens with traceback. For example:

x = 2
assert x < 1
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-5-0662b7144a79> in <module>()
      1 x = 2
----> 2 assert x < 1

AssertionError: 

However, it is good practice to humanize (i.e. explain in words) why this error occurs. Often, I use it to feed back useful information. For example:

x = 2
assert x < 1, "Number is not less than 1: {0}".format(x)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-4-bd4b9b15ccc2> in <module>()
      1 x = 2
----> 2 assert x < 1, "Number is not less than 1: {0}".format(x)

AssertionError: Number is not less than 1: 2

From command line

This still happens with traceback. For example:

H:\>python assert.py
Traceback (most recent call last):
  File "assert.py", line 1, in <module>
    assert 2 < 1
AssertionError

Solution for all environments

Use the traceback module. For details, see answer at How to handle AssertionError in Python and find out which line or statement it occurred on?

like image 94
jpp Avatar answered Sep 17 '22 12:09

jpp


With pure Python, you can't reproduce the condition of the assertion automatically easily. The pytest testing framework does exactly what you want, but the implementation for this magic is everything but trivial. In short, pytest rewrites the code of your assertions to complex code to capture the information needed to generate the desired error message.

like image 43
sebasgo Avatar answered Sep 20 '22 12:09

sebasgo