Tue. Mar 28th, 2023

How to easily manually raise an AssertionError in Python.

Option 1 (The long way), useful if you then want to do something else if the condition is not met:

x = 10

if x != 10:
    raise AssertionError("ERROR: x is not 10")
else:
    print("OK: x is 10")

Option 2 (The stricter easy way), good for checking 1 condition:

assert x == 10, "ERROR: x is not 10"

By admin