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

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

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)\!'

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.2.zip (13.1 kB view details)

Uploaded Source

Built Distribution

telegram_bot_unittest-0.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: telegram-bot-unittest-0.2.zip
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.16

File hashes

Hashes for telegram-bot-unittest-0.2.zip
Algorithm Hash digest
SHA256 d428822fb046c25909fb46da2d79f10a8c3082ab73d8073d6f7adb0b424543bf
MD5 22f65fab7f48880cceaa3b34a6ab80e1
BLAKE2b-256 9bc1a3f057756171990cc210ee3d89514f19179388c9b6a1d996a1112ece84b2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_bot_unittest-0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.16

File hashes

Hashes for telegram_bot_unittest-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5424a0b481c5089f99b0428feafdf8ca7e2d87fdb14cdb6ffc782fea43c13d17
MD5 6a6a9cbe81e2676f0a3b534e2806f49b
BLAKE2b-256 bcc41edcdab4d2172ceff775391481d5734082725fac6661af364369e8e819bc

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