Python Assert Keyword – with 3 examples

  • Reading time:15 mins read

What is the Python assert keyword used for

The assert keyword is a built-in statement that is generally used for testing and debugging. Using this keyword we can test whether a certain piece of code meets a given condition or returns a certain result.

This can be values of an argument, the output returned by a function or some python types i.e Numeric. 

 

 

The assert statement takes in two parameters:

  •     The condition that we want to test.
  •      An optional message is printed on the console when an AssertionError is raised.

 

If the condition we are testing for evaluates to False then we throw up an AssertionError and print a message otherwise the program executes uninterrupted. We can also use the assert statement without printing out an error message.

When to use Python assert keywords

The assert keyword makes it easier for developers to detect errors in their programs and handle them.

So we can simply put the assert statement in our code at a certain point and say that at this point we expect the program to meet a certain condition. We expect an AssertError to be raised if the condition is not met.

 

How to use the Python Assert statement

 

In this article, we are going to use a simple example to learn how we can use the assert keyword. In the python script below we have initialized the variable animal to a certain value, using the assert keyword we are testing that the variable animal is indeed equal to the value monkey.

Python Assert Keyword – Example 1

We have added a print statement to mimic that we are doing the testing in the middle of the program.

 

 

In this case, since our condition is met the rest of the program is executed successfully, therefore nothing really happens and the print statement is executed.

Python Assert Keyword – Example 2

On the other hand, suppose the condition is not met. In the program below we have changed the value of the animal variable but left the rest of the program intact.

This means that the statement that we are testing will evaluate as False. In this case, we will get an AssertionError.

 

 

The assert statement is simply a debugging tool and should only be used in development environments. Frameworks such as Pytest offer more robust features for writing small unit tests that can also be scaled to support complex functional tests in production code.

 

How to use the Python assert keyword

 

Python Assert Keyword – Example 3

We can also return a message alongside the AssertionError. In this case, we are going to use an f string to return a message letting us know that we were expecting monkey as the variable.

 

Summary

The Assert statement is appropriate for testing conditions in code and cannot be used in place of the try and except statements or other program errors such as the SyntaxError.

 

How to use the Python assert keyword

 

This is how to use the Python Assert Keyword. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.