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.
Metadata
Release files for telegram-bot-unittest 0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| telegram-bot-unittest-0.4.zip | 21.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| telegram_bot_unittest-0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 35.8 kB
Release files / telegram-bot-unittest-0.4.zip
| Download URL | telegram-bot-unittest-0.4.zip |
|---|---|
| Size | 21.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
28e3e6003d7cf009ab44ed1d580be275c4b09f9b3afb1a916595d6a99ed69278
|
|
BLAKE2b-256 checksum How to use checksums |
e08b52f1e80c6d4c91d90eb67bd424190894d03d6f30761bd1f439897682adb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.8.0
|
Release files / telegram_bot_unittest-0.4-py3-none-any.whl
| Download URL | telegram_bot_unittest-0.4-py3-none-any.whl |
|---|---|
| Size | 14.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e47617563dd3d4c1d036f1d1849300c8ddc33b44120a3fa8ef47fb5aac102410
|
|
BLAKE2b-256 checksum How to use checksums |
4d89dcad0327bf18487f38321156c5b5d91f84aa3ffd1c15c82674564b7cf4d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.8.0
|