Cara menggunakan assert keyword in python

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.

 

Cara menggunakan assert keyword in python

 

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.

 

Cara menggunakan assert keyword in python

 

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.

 

Cara menggunakan assert keyword in python

 

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

  • How to use the Python for loop
  • How to use Python Sets
  • How to use a Python Dictionary
  • How to use Python Classes
  • How to use Python Range
  • How to use Python if-else statements
  • How to use Python RegEx
  • How to use Python Lists
  • How to use Python Enumerate
  • How to use Python Functions
  • How to use Python Split
  • How to use Python Try-Except
  • How to use Python Tuples
  • How to use Python Arrays
  • How to use Python Sort
  • How to use the Python DateTime
  • How to download Python?
  • How to use the Python FileWrite function
  • How to use Python Lambda
  • How to use Python ListAppend
  • How to use Python ListComprehension
  • How to use Python Map
  • How to use Python Operators
  • How to use Python Pandas
  • How to use Python Requests
  • How to use Python Strings
  • How to use Python Count
  • How to use Python Comments
  • How to use the Python File Reader method
  • How to use the Python IDE-s
  • How to use Python logging
  • How to use Python Print
  • How to use the Python Zip
  • How to use Python Append
  • How to use Python Global Variables
  • How to use the Python join method
  • How to use Python list length
  • How to use Python JSON files
  • How to use Python Modulo
  • How to use Python file opening methods
  • How to use Python round
  • How to use Python sleep
  • How to use Python replace
  • How to use Python strip
  • How to use the Python Time module
  • How to use Python unittests
  • How to save data to a text file using Context Manager?
  • How to use Python external modules
  • How to use Python find
  • How to install the Python pip package manager
  • How to delete files in Python
  • Parsing XML files in Python
  • How to make a GUI in Python
  • How to use Python in Command Prompt
  • How to Run a Python Program in VS Code
  • How to run a program in Python IDLE
  • How to run a program in Jupyter Notebook
  • How to read a text file in Python
  • How to add numbers in Python
  • How to ask for user input in Python
  • How to debug in Python
  • How to create a thread in Python
  • How to end a program in Python
  • How to import a library in Python
  • How to use the PIP package manager
  • How to use classes in Python
  • How to reverse strings in Python
  • How to convert a string to int in Python
  • How to print on the same line in Python
  • How to remove items from a list
  • How to add to a dictionary in Python
  • How to raise an exception in Python
  • How to throw an exception in Python
  • How to stop a program in Python
  • How to use Python assert
  • How to use the Python compiler

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