🧪 Testing¶
Testing Telegram bot commands in django-telegram-app is designed to be simple, fast, and deterministic.
This guide explains the testing tools provided by the framework and how to use them effectively.
Goals of the Testing Framework¶
The built‑in test utilities help you:
- simulate incoming Telegram messages and callback queries
- assert that your bot responded with specific messages
- test multi‑step commands (including callback flows)
- inspect the last message your bot sent
- avoid real API calls by automatically mocking the Telegram client
These tools ensure your bot's logic can be tested fully without network access.
TelegramBotTestCase¶
All tests should subclass:
This base class provides:
send_text(message)– simulate an incoming text messageclick_on_button(button_label_or_index)– simulate clicking an inline keyboard button- When passing a label, this will be searched for, when passing an int, the index of the button will be searched for.
- Passing index can be useful in case the buttons have dynamic text or you just want to click the last button (-1)
last_bot_message– inspect the most recent bot message- automatic mocking of
bot.post(...) - a helper method for posting raw update payloads
Example: Testing a simple command¶
Assume the /roll command returns “Choose a die to roll:”.
Testing callback flows¶
To simulate pressing an inline button:
click_on_button() automatically:
- inspects the last keyboard sent by the bot
- finds the correct button by text or index
- constructs a callback query update
- posts it to the webhook
Testing “waiting for input”¶
When a step uses:
the next non‑command message will be stored in the callback data.
Example:
Inspecting all messages¶
Cleanup¶
TelegramBotTestCase ensures:
- all mocks are cleaned up
- callback data created during tests is removed
- webhook posting uses your project's
WEBHOOK_TOKEN
Summary¶
The testing tools in django-telegram-app let you:
- write clear, readable tests
- exercise full multi‑step command flows
- avoid network calls
- assert bot output precisely
Testing your bot becomes as simple as testing any other Django component.