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
send text message
send command
send file
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()
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()
add ‘telegram_bot_unittest.fixtures’ to ‘pytest_plugins’ list in conftest.py
Add fixture ‘bot’ to you test functions.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file telegram-bot-unittest-0.3.zip
.
File metadata
- Download URL: telegram-bot-unittest-0.3.zip
- Upload date:
- Size: 21.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7223d041dac00887cd4472fa301c3b018509902753123de694274f3340d63ea |
|
MD5 | a9015f283e58572c76ccf94401a3b466 |
|
BLAKE2b-256 | ee9764a0c212712c92cc452655c36268e29b13d71fba6fb907c0f78f58c8d80a |
File details
Details for the file telegram_bot_unittest-0.3-py3-none-any.whl
.
File metadata
- Download URL: telegram_bot_unittest-0.3-py3-none-any.whl
- Upload date:
- Size: 14.4 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b97621f625308da6642878f5feaca7fcb5c40f176582e3c77d59163d689f4951 |
|
MD5 | 56439389dc41e35650ff1087270b55ee |
|
BLAKE2b-256 | 4f6b284e9dcaa8fc6514c7696104ff0099765b08a2270bd02f6fe6aeca776f2e |