Skip to main content

Aiogram Tests

aiogram_tests is a testing library for bots written on aiogram

📦 Installation

pip install aiogram-testing

The import name stays aiogram_tests:

from aiogram_tests import MockedRequester

📚 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_tests import MockedRequester
from aiogram_tests.handler import MessageHandler
from aiogram_tests.types.dataset import MESSAGE


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

Checking calls

query() returns the Bot API methods the handler called, as aiogram objects (SendMessage, EditMessageText, ...), so fields such as reply_markup are typed models, not raw dicts.

from aiogram.methods import AnswerCallbackQuery, SendMessage

calls = await request.query(message=MESSAGE.as_object(text="Hello, Bot!"))

calls.send_message.fetchone()            # last SendMessage, by snake_case name
calls.get(SendMessage)                   # all SendMessage calls, typed
calls.last(SendMessage)                  # last one or None
calls.assert_called(SendMessage, text="Hello, Bot!")   # fails with the calls that were made
calls.assert_not_called(AnswerCallbackQuery)

Accessing a method that was not called (calls.edit_message_text) raises MethodIsNotCalledError, an AttributeError, so hasattr(calls, "edit_message_text") works.

Testing a whole router: BotTester

MockedRequester checks one handler function. BotTester runs updates through your real router or dispatcher, with its filters, middlewares, FSM and injected dependencies, just like production:

from aiogram.methods import SendMessage

from mybot.handlers import router


async def test_survey(bot_tester):  # fixture from the bundled pytest plugin
    tester = bot_tester(router, db=fake_db)   # keyword arguments reach handlers like dp.start_polling(**kwargs)

    calls = await tester.send_message("/survey")
    calls.assert_called(SendMessage, text="What is your name?")

    await tester.send_message("Alice")        # FSM state carries over between steps
    calls = await tester.press("Python")      # presses a button from the last inline keyboard

    calls.assert_called(SendMessage, text="Nice to meet you, Alice!")
    assert await tester.get_state() is None
  • send_message(text, user=..., chat=...), click(data_or_callback_data, message=..., user=...), press(button_text) and feed(event_or_update, event_type=...) return the calls made while handling that update.
  • get_state(user), set_state(state, user=..., **data) and state(user) give access to the FSM.
  • A module-level router is detached from the previous test's dispatcher, so each test gets a fresh one.
  • A Dispatcher is used as is; its workflow_data and middlewares stay in place.

The plugin is registered automatically on install and provides the bot_tester (factory), mocked_bot and dataset fixtures. See examples/test_survey.py.

Other update types

Each update type has its handler; pass the event positionally or by its update field name:

from aiogram_tests.handler import InlineQueryHandler, PreCheckoutQueryHandler, UpdateHandler
from aiogram_tests.types.dataset import INLINE_QUERY, PRE_CHECKOUT_QUERY

calls = await MockedRequester(InlineQueryHandler(search)).query(INLINE_QUERY.as_object(query="cats"))
calls = await MockedRequester(PreCheckoutQueryHandler(approve)).query(pre_checkout_query=PRE_CHECKOUT_QUERY.as_object())

# anything else the dispatcher knows, by name
calls = await MockedRequester(UpdateHandler(on_business, event="business_message")).query(message)

Available: MessageHandler, EditedMessageHandler, ChannelPostHandler, EditedChannelPostHandler, CallbackQueryHandler, InlineQueryHandler, ChosenInlineResultHandler, ShippingQueryHandler, PreCheckoutQueryHandler, PollHandler, PollAnswerHandler, MyChatMemberHandler, ChatMemberHandler, ChatJoinRequestHandler, MessageReactionHandler and UpdateHandler.

Middlewares passed as dp_middlewares are registered on every event observer except update and error, whose events are Update and ErrorEvent rather than messages or callbacks.

▶️ More examples

Metadata

Release files for aiogram-testing 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiogram-testing 1.2.0
File Size Uploaded Provenance Actions
aiogram_testing-1.2.0.tar.gz 16.8 kB With provenance Details for aiogram_testing-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aiogram-testing 1.2.0
File Interpreter ABI Platform Actions
aiogram_testing-1.2.0-py3-none-any.whl Python 3 none any Details for aiogram_testing-1.2.0-py3-none-any.whl

Total release size: 39.2 kB

Release files / aiogram_testing-1.2.0.tar.gz

Download URL aiogram_testing-1.2.0.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7ca80ab9284770858533054dcd0e98a31d557e6e79ba5d4ee36aef55c2c389b3
BLAKE2b-256 checksum
How to use checksums
4b917db028586d8e1fe81ded6d6dc9186d14444b93c35501be3ef9ee5bac5e6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.

Transparency log

Release files / aiogram_testing-1.2.0-py3-none-any.whl

Download URL aiogram_testing-1.2.0-py3-none-any.whl
Size 22.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c02ea9942e4af057cb6e62b9665fd0d3f5bd471685fcfae9296397cd5648ab47
BLAKE2b-256 checksum
How to use checksums
b6683f87f44c7f237a9cb51522717fa12b8bd613cf1a5835b04a0119aa8f76e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.

Transparency log

Security for aiogram-testing 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for aiogram_testing-1.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page