Explaning the Assert Keyword as Used in Python Scripts and Not in Unit Tests

The aim of this page📝 is to explain and illustrate the use of "assert" statement in Python

1. EXPLANATION

https://stackoverflow.com/questions/5142418/what-is-the-use-of-assert-in-python

if not condition:
raise AssertionError()
>>> assert True # nothing happens
>>> assert False
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
assert False, "Oh no! This assertion failed!"
python -O script.py

2. EXAMPLE

import os
import slack

CHANNEL = os.environ['SLACK_CHANNEL']
MESSAGES_PER_PAGE = 200
client = slack.WebClient(token=os.environ['SLACK_TOKEN'])
page = 1
print("Retrieving page {}".format(page))
response = client.conversations_history(
channel=CHANNEL,
limit=MESSAGES_PER_PAGE,
)
assert response["ok"]
messages_all = response['messages']

3. LINKS

--

--

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com