12

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?

jpp
  • 159,742
  • 34
  • 281
  • 339
Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183

3 Answers3

8

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?

jpp
  • 159,742
  • 34
  • 281
  • 339
  • 1
    The question seems to be whether this *humanization* can be done automatically. – Ma0 Feb 14 '18 at 09:20
  • @jp_data_analysis Apologies I think you're right, it's only in the interpreter that it fails – Chris_Rands Feb 14 '18 at 09:29
  • @jp_data_analysis Very true. Let's not call it *humanization* then XD – Ma0 Feb 14 '18 at 09:29
  • From your answer it is not very clear to me which snippet corresponds to `assert.py`. Could you clarify? – Ma0 Feb 14 '18 at 09:30
  • @jp_data_analysis The behavior you are describing cannot be trusted though. Running this code in different places produces different results. Running it on a Python shell for example behaves differently; it does not provide any information regarding the assertion that failed. – Ma0 Feb 14 '18 at 09:38
5

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.

sebasgo
  • 3,845
  • 23
  • 28
0

assertpy is a small nice package which prints out useful error message out of the box:

>>> assert_that('foo').is_equal_to('bar')

Expected <foo> to be equal to <bar>, but was not.
gebbissimo
  • 2,137
  • 2
  • 25
  • 35