Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assert true vs assert is not None

I am working on some automated tests with a colleague and we wondered whether there were any differences in our approaches.

We have a method that returns a HTTP response object. Occasionally, it might return None, but normally an object.

The original code is:

self.assertTrue(response)

But I felt it might be more readable to have:

assert response is not None

I looked into the differences in the docs on truthyness, so I'm aware of things that would return true that aren't necessarily None. However, for the most part, both would work. And a lot of the existing tests are asserting true rather than asserting not none.

Is there any reason why one technique would work better than the other?

like image 346
Craig Brett Avatar asked Nov 27 '19 10:11

Craig Brett


People also ask

What is assert true in Python?

assertTrue() in Python is a unittest library function that is used in unit testing to compare test value with true. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is true then assertTrue() will return true else return false.

What does the assert statement do and what other statement is it like?

assert statement takes an expression and optional message. assert statement is used to check types, values of argument and the output of the function. assert statement is used as debugging tool as it halts the program at the point where an error occurs.

What happens when Python assert fails?

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 fix an assertion error 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.


1 Answers

tldr;

There is a functional difference in general, though in your particular case the execution result will be the same for both techniques. It now depends only on your willingness to be either explicit or concise.

Full answer

Assuming that a is a variable in Python, there is a difference between:

assert a

and

assert a is not None

In the fist case, you are evaluating the return of the call to the bool() buit-in on the object, which follows multiple strategies to get a result, as it was already discussed here.

This evaluates only according to the object's class implementation. Did you have a look on the class of your response object? It will tell you how bool(response) behaves.

In the second case, your are evaluating a as not being None. This is completely different. For example, an empty list will evaluate to False but is not None:

>>> a = []
>>> a is not None
True
>>> assert a is not None
>>> assert a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

If your response object is a sub-class derived from list, then it will probably behave the same way, so evaluating the emptiness of response is not equivalent to evaluating response as None.

In your case, I guess that your response object is provided by the request library. If so, then yes, in your particular case, both techniques are functionally equivalent, though you should notice a tiny performance gain evaluating against None.

like image 138
Tim Avatar answered Oct 03 '22 13:10

Tim