Skip to main content

aiogram-bot-tester

CI PyPI version Python 3.10–3.15 aiogram 3.x Apache 2.0 license PyPI Downloads

Test aiogram conversations offline. Start the bot, tap a button by its label, and check the reply and the FSM after each step.

import pytest

from my_bot.signup import Signup

pytestmark = pytest.mark.asyncio


async def test_signup(tester):
    welcome = await tester.start()
    welcome.assert_contains("Welcome")
    welcome.assert_button("Sign up")

    prompt = await tester.tap_button("Sign up")
    prompt.assert_contains("What's your name?")
    await tester.assert_state(Signup.name)

    reply = await tester.send_message("Bob")
    reply.assert_contains("Nice to meet you, Bob!")
    await tester.assert_state(Signup.confirm)
    await tester.assert_data(name="Bob")

What you get

  • Actions that read like a chat. Send a command, tap a reply or inline button by its visible text, or send a photo.
  • A reply you can assert on immediately. Check text, keyboard layout, media, and captions on the message returned by a step.
  • FSM assertions at the point that matters. Check transitions and stored data with assert_state() and assert_data().
  • Your real aiogram routing, offline. Run your routers, filters, middleware, and injected dependencies through the dispatcher without a Telegram token or polling.

Try it with your bot

Install the tester and the async pytest plugin.

uv add --dev aiogram-bot-tester pytest pytest-asyncio
uv run pytest

With pip, install in your test environment.

python -m pip install aiogram-bot-tester pytest pytest-asyncio
python -m pytest

Use your bot's existing router and state group in the examples. Put the fixture in conftest.py and the test snippets in a test module with the pytestmark from the first example.

import pytest

from aiogram_bot_tester import BotTester
from my_bot.signup import router


@pytest.fixture
def tester():
    return BotTester.from_routers(router)

The signup example expects your handlers to show a Sign up button on /start, prompt for a name when tapped, and store the name before moving to Signup.confirm. You can also pass an existing dispatcher to BotTester.from_dispatcher(dispatcher).

Write tests that match the conversation

Check a reply on the spot

Each start(), send_message(), send_command(), or tap_button() returns the last captured reply for that step. A missing reply raises NoBotResponseError.

async def test_welcome_keyboard(tester):
    welcome = await tester.send_command("start")
    welcome.assert_matches(r"^Welcome")
    welcome.assert_keyboard([["Sign up"]])

Replay a transcript

chat() replays a linear, text-only conversation and checks that each reply contains the expected text.

async def test_welcome_transcript(tester):
    await tester.chat(
        """
        User: /start
        Bot: Welcome
        """
    )

Send media and check the attachment

Pass media metadata as keyword arguments to send_photo(), send_document(), send_voice(), send_video(), or send_sticker(). The tester builds the Telegram update for you. For a bot that echoes an uploaded avatar by its file ID:

async def test_avatar(tester):
    reply = await tester.send_photo(file_id="avatar", caption="My avatar")

    reply.assert_photo(file_id="avatar")
    reply.assert_caption("Avatar saved")
    reply.assert_caption_contains("saved")

Use assert_document(), assert_voice(), assert_video(), and assert_sticker() for other attachments. Captured attachments are available as media models on the reply. Outgoing URLs and upload metadata are available in tester.api_calls.

Send an album

An album contains 2–10 media items. The tester generates a file ID when you omit one. Mix photos and videos, or send a group of documents. Each item reaches your handler as its own update with a shared media_group_id. The result contains all captured replies in order, including an empty tuple when the bot stays silent.

from aiogram_bot_tester import Photo


async def test_album(tester):
    replies = await tester.send_album(
        [Photo(file_id="front"), Photo(file_id="back")]
    )

    front, back = replies
    front.assert_photo(file_id="front")
    back.assert_photo(file_id="back")

Feed a custom update

Use feed_update() for events such as inline queries. It returns all captured messages and edits from the update. Inspect api_calls when the expected outcome is an API call. Each ApiCall records the Bot API method name and a read-only snapshot of the supplied parameters.

from aiogram.types import InlineQuery, Update, User


async def test_inline_search(tester):
    replies = await tester.feed_update(
        Update(
            update_id=100,
            inline_query=InlineQuery(
                id="search-1",
                from_user=User(id=1, is_bot=False, first_name="Bob"),
                query="cats",
                offset="",
            ),
        )
    )

    assert replies == ()
    assert any(
        call.method == "answerInlineQuery" and call.params["inline_query_id"] == "search-1"
        for call in tester.api_calls
    )

Why another testing library?

Here is how each tool expresses the same check: send /start and verify that the bot replied with a welcome message.

Library What you write When it fits
aiogram-bot-tester reply = await tester.start()
reply.assert_contains("Welcome")
Follow one conversation step by step and assert directly on replies, buttons, and FSM transitions.
aiogram.test await bot_user.send("/start")
assert "Welcome" in bot_chat.messages[-1].text
Model a stateful Telegram environment with multiple users, chats, rights, and an FSM context.
aiogram-testing calls = await tester.send_message("/start")
calls.assert_called(SendMessage, text="Welcome")
Check typed Bot API calls, queue API failures, and read or set FSM state. Its press() also accepts a visible inline-button label.
aiogram-test-framework await user.send_command("start")
assert user.has_received_message_containing("Welcome")
Work with simulated users, Telegram object factories, and a request capture log.

aiogram-bot-tester keeps the common workflow on one object. Create a tester from your router, take a user action, and check the returned reply. Add await tester.assert_state(...) and await tester.assert_data(...) beside the reply assertion when a step also changes the bot's state.

Learn more

Metadata

Release files for aiogram_bot_tester 2.2.1

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_bot_tester 2.2.1
File Size Uploaded Provenance Actions
aiogram_bot_tester-2.2.1.tar.gz 21.3 kB Without provenance Details for aiogram_bot_tester-2.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aiogram_bot_tester 2.2.1
File Interpreter ABI Platform Actions
aiogram_bot_tester-2.2.1-py3-none-any.whl Python 3 none any Details for aiogram_bot_tester-2.2.1-py3-none-any.whl

Total release size: 47.5 kB

Release files / aiogram_bot_tester-2.2.1.tar.gz

Download URL aiogram_bot_tester-2.2.1.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
54e465d59103a603feb80413499d2401dde005be250b06ad147c02fc338e4fec
BLAKE2b-256 checksum
How to use checksums
b2c858c2cf6105319cae78cdd67aa54cdd62699bb8927059fdfd00b5112fc747
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / aiogram_bot_tester-2.2.1-py3-none-any.whl

Download URL aiogram_bot_tester-2.2.1-py3-none-any.whl
Size 26.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
25ee78a5d80a9bf8c2f1f5835627ff5b670ec63086e9fb84ea834c54e8b1b79a
BLAKE2b-256 checksum
How to use checksums
4f06758a9a490dc1a61ee36e24b01ab022627dffe5b81099e14896bb0665dcf0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for aiogram_bot_tester 2.2.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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