GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Writing Tests

Writing Tests

Jump to bottom Edit New page
Bibo-Joshi edited this page Jan 7, 2024 · 21 revisions

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 call Application.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 to None
  • 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 of BaseRequest.

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.

Wiki of python-telegram-bot © Copyright 2015-2026 – Licensed by Creative Commons

Must read

  1. Introduction to the API
  2. Tutorial: Your first bot
  3. FAQ
  4. How to ask good questions
  5. How to write an MWE

Concepts & Important Elements

  1. Architecture Overview
  2. Builder Pattern for Application
  3. Types of Handlers
  4. Working with Files and Media
  5. Exceptions, Warnings and Logging
  6. Concurrency in PTB

Notable Features

  1. Advanced Filters
  2. Storing data
  3. Making your bot persistent
  4. Adding Defaults
  5. Job Queue
  6. Arbitrary callback_data
  7. Avoiding flood limits
  8. Webhooks
  9. Bot API Forward Compatiblity

Code Resources

  1. Frequently requested design patterns
  2. Code snippets
  3. Performance Optimizations
  4. Telegram Passport
  5. Bots built with PTB
  6. Automated Bot Tests

Examples explained

  1. InlineKeyboard Example

Networking

  1. Working Behind a Proxy
  2. Handling network errors

Other resources

  1. Where to host Telegram Bots
  2. How to host your bot
  3. Local API Server
  4. Type Checking with PTB
  5. Press
  6. Notes on GAE
  7. Related Projects
  8. Emoji

Transition Guides

  • Version 4
  • Version 5
  • Version 12
  • Version 13
  • Version 20

Administration

  • Test Bots
  • How To Release
  • Bots, Groups & Channels
  • PTB tests knowledge base
  • Bot API Backward Compatibility

Clone this wiki locally

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.