I have seen in some programs and on the web that they use the assert function , for example:
Assert True
Does nothing, while:
Assert False
Send an AssertationError...
It is still not clear to me what this function is for.
Any ideas ?
Thank you!
What it does is already explained in other answers. I am going to expand a little more on what it is used for. It has two main uses.
Detect programming errors
The first is to include in your program a kind of "safety net" against possible programmer errors. Imagine that you call a function that you know never returns a negative value, and you use its response as part of another operation:
I insist, the will
otra_funcion()
never return a negative. If it did, it would be wrong. But you don't trust it at all, because perhaps that function hasn't been written by you and perhaps in a later update of the library that includes it, a bug appears and returns a negative.assert
allows you to express a condition that must always be true, since otherwise the program will be interrupted. We could addassert
like this:While it
a
is indeed positive, it is as if we had not added theassert
, since it does nothing. But if it were ever negative, we are calm because itassert
will interrupt the program and dump a trace indicating the point in the program where itassert
failed.We can include a message that would be part of the exception that is raised when the expression is false. So:
This message would be part of the error if the exception occurs, and will help us track down the problem.
An important detail is that the
assert
should be used to catch conditions that should never occur , and if they do occur they should be considered a programming error. If, on the other hand, it is possible that the function returns negatives but in my specific application it does not work for me and I want to consider it an error, the normal thing would be to use aif
to verify it and, if necessary, generate a more appropriate exception. For example:testing
When you are writing unit tests (which consist of calling a function whose result you already know in advance what it should be, but you are checking that it actually returns what you expected), the
assert
is used to verify that the value is the expected one.For example, suppose you have written a function called
raiz_cuadrada()
and want to check if it works fine. You can write some tests like this:Typically there would be more testing, but to get the idea it's enough.
When you run these tests from a test automation environment, that environment captures those
AssertionError
that occur, so that in the end it can give you a report of how many tests have failed (and which ones), so that you can review the function that did not behave as you expectedTesting is very important during development, because if you modify the function
raiz_cuadrada()
to fix a bug that it had, you want to be sure that you haven't introduced another bug by doing so. Having a good battery of tests and that they all continue to pass after having modified the code is essential.The assert is a Python statement that allows you to define conditions that must always be met. In case the boolean expression is True assert does nothing and in case of False it throws an exception.
They can be used as pre and post conditions in methods, functions, code blocks but above all to specify invariants. It is shorter than writing an if statement, much clearer for the reader and as a bonus the statement is not executed in case the interpreter is invoked with -O.
So when to do:
Throw an AssertionError exception that you could catch in a try/except
You can find everything in the official documentation: Official Doc