Skip to main content

test your python-telegram-bot easily

Project description

Unit-tests for python-telegram-bot.

How it works

This is a first version of library

Only long polling mode supported!

Library starts your python-telegram-bot object with custom url (our unit-test server on Flask running under waitress). Now you can communicate in unit-tests with your bot as you do in Telegram.

Features

  1. send text message

  2. send command

  3. send file

  4. receive file

Fixtures

user

User object to send messages and check incoming messages from your bot

bot

Your bot object. See ‘Using’

Echo Bot example

Check echo of Echo Bot from python-telegram-bot example.

def test_echobot_message(bot, user):
    user.send_message('testing message')
    message = user.get_message()
    assert message['text'] == 'testing message'

Check /start command of Echo Bot

def test_echobot_start(bot, user):
    user.send_command('/start')
    message = user.get_message()
    assert message['text'] == 'Hi [FN LN](tg://user?id=1)\!'

File Bot example

Bot renames file you send to him.

def test_echobot_file(bot, user):
    current_dir = os.path.dirname(os.path.abspath(__file__))

    user.send_document(current_dir, 'test.txt')

    document = user.get_document()

    file_io = document.path_or_bytes
    assert file_io.name == 'echo_test.txt'

    content = io.TextIOWrapper(file_io, encoding='utf-8').read()
    assert content == 'Hello world!\nHello world!'

Installing

You can install or upgrade telegram-bot-unittest with:

$ pip install telegram-bot-unittest --upgrade

Or you can install from source with:

$ git clone https://github.com/dontsovcmc/telegram-bot-unittest --recursive
$ cd telegram-bot-unittest
$ python setup.py install

Using

1. Create non-bloking function ‘setup_bot’ that runs your bot. We need to separate updater.idle() and creating Updater().

def setup_bot(bot_token: str, base_url: str = None) -> Updater:

    updater = Updater(bot_token, base_url=base_url)

    dispatcher = updater.dispatcher

    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", help_command))
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
    updater.start_polling()

    return updater

def main(base_url: str = None) -> None:

    updater = setup_bot(BOT_TOKEN, base_url)
    updater.idle()
  1. Add fixture ‘bot’ to you fixture.py file. Example:

import pytest
from <your module> import <start_bot_function>
from telegram_bot_unittest.routes import TELEGRAM_URL
from telegram_bot_unittest.user import BOT_TOKEN

@pytest.fixture(scope='session')
def bot(telegram_server):
    updater = start_bot_function(BOT_TOKEN, TELEGRAM_URL)
    yield updater.bot
    updater.stop()
  1. add ‘telegram_bot_unittest.fixtures’ to ‘pytest_plugins’ list in conftest.py

  2. Add fixture ‘bot’ to you test functions.

  3. Enjoy!

Contributing

Contributions of all sizes are welcome.

License

You may copy, distribute and modify the software provided that modifications are described and licensed for free under LGPL-3. Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under LGPL-3, but applications that use the library don’t have to be.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram-bot-unittest-0.4.zip (21.2 kB view details)

Uploaded Source

Built Distribution

telegram_bot_unittest-0.4-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file telegram-bot-unittest-0.4.zip.

File metadata

  • Download URL: telegram-bot-unittest-0.4.zip
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.0

File hashes

Hashes for telegram-bot-unittest-0.4.zip
Algorithm Hash digest
SHA256 28e3e6003d7cf009ab44ed1d580be275c4b09f9b3afb1a916595d6a99ed69278
MD5 862968d81c9fd012aab499433e3c812c
BLAKE2b-256 e08b52f1e80c6d4c91d90eb67bd424190894d03d6f30761bd1f439897682adb7

See more details on using hashes here.

File details

Details for the file telegram_bot_unittest-0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for telegram_bot_unittest-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e47617563dd3d4c1d036f1d1849300c8ddc33b44120a3fa8ef47fb5aac102410
MD5 e8bd4b347fe906fb8baabd0de3033159
BLAKE2b-256 4d89dcad0327bf18487f38321156c5b5d91f84aa3ffd1c15c82674564b7cf4d4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page