vb64 / telemulator3

Mocked Telegram Bot API elements for unit tests of a bot based on the pyTelegramBotAPI library.
MIT License
4 stars 1 forks source link
telegram-bot-api unit-testing

Library telemulator3

GitHub Workflow Status GitHub Workflow Status Codacy Badge Codacy Badge

The free, open-source telemulator3 library designed to simplify automatic testing of Telegram bots created using the pyTelegramBotAPI library.

The telemulator3 library partially emulates the Telegram Bot API in unit tests and allows you to create typical scenarios for the interaction of your bot with the Telegram Bot API.

Installation

pip install telemulator3

Usage

Create TeleBot instance and start emulate Telegram API for bot.

from telebot import TeleBot
from telemulator3 import Telemulator

bot = TeleBot('xxx-yyy-zzz', threaded=False)

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
    bot.reply_to(message, "Howdy, how are you doing?")

telemul = Telemulator()
telemul.set_tested_bot(bot, username='my_bot', name='My Bot')

At start, there are no registered users in emulated API.

assert not telemul.api.users

Make API user, that represent our bot. It's a first registered user.

mybot = telemul.api.get_me()
assert mybot.is_bot
assert mybot.username == 'my_bot'
assert len(telemul.api.users) == 1

New user open private chat with bot and send /start command. Bot must answer as defined and his answer must be in chat history.

from telemulator3 import send_command

user = telemul.api.create_user('User')
chat = user.private()
send_command(chat, '/start', user)
assert chat.history.contain('Howdy, how are you doing?')

User create group and add bot as member.

group = user.create_group('My group')
group.add_members(user, [mybot])
assert group.history.contain('invite new members:')

And so on.

mybot.leave(group)
assert group.history.contain('My Bot (ID 1) left chat')
# group.history.dump()

Development

git clone git@github.com:vb64/telemulator3
cd telemulator3
make setup PYTHON_BIN=/path/to/python3
make tests