aiogram-unittest

A library for unittesting your bots on aiogram


Keywords
aiogram, python, python3, telegram-bot, unittest, unittesting
License
MIT
Install
pip install aiogram-unittest==0.0.1

Documentation

aiogram_unittest

aiogram_unittest is a testing library for bots written on aiogram

📚 Simple examples

Simple handler test

Simple bot:

from aiogram import Bot, Dispatcher, types, executor

# Please, keep your bot tokens on environments, this code only example
bot = Bot('123456789:AABBCCDDEEFFaabbccddeeff-1234567890')
dp = Dispatcher(bot)


@dp.message_handler()
async def echo(message: types.Message):
    await message.answer(message.text)


if __name__ == '__main__':
    executor.start_polling(dp)

Test cases:

import unittest

from aiogram import types
from bot import echo

from aiogram_unittest import Request, RequestType
from aiogram_unittest.dataset import MESSAGE
from aiogram_unittest.handler import MessageHandler


class TestBot(unittest.IsolatedAsyncioTestCase):
    async def test_echo(self):
        request = Request(request_handler=MessageHandler(echo))

        message = types.Message(**MESSAGE)
        message.text = 'Hello, Bot!'
        call_args = await request.query(message)

        answer_text = call_args[RequestType.SEND_MESSAGE][0]['text']
        self.assertEqual(answer_text, 'Hello, Bot!')

▶️ More examples