Table of Contents
In case you want to practice test-driven development, or ensure your bot still works after consecutive changes (regression bugs are rather common for chatbots), you should write test cases.
Unit Tests
Unit tests are performed on a logically encapsulated component of the system. The definition of unit tests in contrast to integration tests is that they have no external dependencies.
Thanks to PTBs modular structure, integration with unit tests can already be achieved for many use cases without additional tooling. For example, you can
- manually enqueue updates in the
Application.update_queue
or directly callApplication.process_update
. This allows you to check that verify that updates are being processed as desiderd without fetching any data from Telegram. Note that for this approach, you'll probably want to set the updater toNone
- mock the networking connection to Telegram, i.e. instead of sending the API requests to Telegram you send them to a fictional backend that you control yourself. That way, your tests can not be corrupted by networking issues and do not need an internet connection. This can be achieved e.g. by subclassing the
Bot
class and overriding all methods or by implementing a subclass ofBaseRequest
.
Moreover, @Eldinnie has written an initial POC of a unit test framework for python-telegram-bot, but as the library grew it was not maintained. Perhaps you might be able to help us out here and help in completing the project ;) https://github.com/Eldinnie/ptbtest
Integration Tests
In contrast to unit tests, integration tests may test the system in its eventual environment together with service integrations, such as the Bot API.
In order to test your bot in a real environment, you can make use of a userbot library that will send messages to your bot and evaluate whether it responds in the way it should. Telethon or Pyrogram should be the choices in a Python environment. The currently unmaintained TgIntegration library was written for exactly this purpose, so maybe someone could come along with a pull request to update it to the latest version of Pyrogram.
Must read
Concepts & Important Elements
- Architecture Overview
- Builder Pattern for
Application
- Types of Handlers
- Working with Files and Media
- Exceptions, Warnings and Logging
- Concurrency in PTB
Notable Features
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Job Queue
- Arbitrary
callback_data
- Avoiding flood limits
- Webhooks
- Bot API Forward Compatiblity
Code Resources
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests
Examples explained
Networking
Other resources
- Where to host Telegram Bots
- How to host your bot
- Local API Server
- Type Checking with PTB
- Press
- Notes on GAE
- Related Projects
- Emoji
Transition Guides
Administration
- Wiki of
python-telegram-bot
© Copyright 2015-2024 – Licensed by Creative Commons