/aiogram_pytest

This is a library for testing your bot on aiogram

Primary LanguagePython

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
from aiogram.fsm.context import FSMContext

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


@dp.message()
async def echo(message: types.Message, state: FSMContext) -> None:
    await message.answer(message.text)


if __name__ == '__main__':
    dp.run_polling(bot)

Test cases:

import pytest

from bot import echo

from aiogram_pytest import Requester
from aiogram_pytest.handler import MessageHandler
from aiogram_pytest.types.dataset import MESSAGE


@pytest.mark.asyncio
async def test_echo():
    request = Requester(request_handler=MessageHandler(echo))
    calls = await request.query(message=MESSAGE.as_object(text="Hello, Bot!"))
    answer_message = calls.send_messsage.fetchone()
    assert answer_message.text == "Hello, Bot!"

▶️ More examples