aiogram_bot_tester 2.2.0__tar.gz → 2.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,210 @@
1
+ Metadata-Version: 2.4
2
+ Name: aiogram_bot_tester
3
+ Version: 2.2.1
4
+ Summary: Your missing test toolkit for Telegram bots.
5
+ Keywords: aiogram,telegram,testing,pytest,bot
6
+ Author: samedit66
7
+ Author-email: samedit66 <samedit66@yandex.ru>
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Topic :: Software Development :: Testing
19
+ Classifier: Framework :: Pytest
20
+ Classifier: Typing :: Typed
21
+ Requires-Dist: aiogram>=3.28.2,<4
22
+ Requires-Python: >=3.10
23
+ Project-URL: Homepage, https://github.com/samedit66/aiogram-bot-tester
24
+ Project-URL: Documentation, https://github.com/samedit66/aiogram-bot-tester#readme
25
+ Project-URL: Repository, https://github.com/samedit66/aiogram-bot-tester
26
+ Project-URL: Issues, https://github.com/samedit66/aiogram-bot-tester/issues
27
+ Project-URL: Changelog, https://github.com/samedit66/aiogram-bot-tester/releases
28
+ Description-Content-Type: text/markdown
29
+
30
+ # aiogram-bot-tester
31
+
32
+ [![CI](https://github.com/samedit66/aiogram-bot-tester/actions/workflows/ci-and-publish.yml/badge.svg?branch=main)](https://github.com/samedit66/aiogram-bot-tester/actions/workflows/ci-and-publish.yml)
33
+ [![PyPI version](https://img.shields.io/pypi/v/aiogram-bot-tester?logo=pypi)](https://pypi.org/project/aiogram-bot-tester/)
34
+ [![Python 3.10–3.15](https://img.shields.io/badge/Python-3.10%E2%80%933.15-blue?logo=python)](https://github.com/samedit66/aiogram-bot-tester/actions/workflows/ci-and-publish.yml)
35
+ [![aiogram 3.x](https://img.shields.io/badge/aiogram-3.x-blue?logo=telegram)](https://docs.aiogram.dev/)
36
+ [![Apache 2.0 license](https://img.shields.io/badge/license-Apache%202.0-blue)](https://github.com/samedit66/aiogram-bot-tester/blob/main/LICENSE)
37
+ [![PyPI Downloads](https://static.pepy.tech/personalized-badge/aiogram-bot-tester?period=total&units=INTERNATIONAL_SYSTEM&left_color=GRAY&right_color=BLUE&left_text=downloads)](https://pepy.tech/projects/aiogram-bot-tester)
38
+
39
+ Test aiogram conversations offline. Start the bot, tap a button by its label, and check the reply and the FSM after each step.
40
+
41
+ ```python
42
+ import pytest
43
+
44
+ from my_bot.signup import Signup
45
+
46
+ pytestmark = pytest.mark.asyncio
47
+
48
+
49
+ async def test_signup(tester):
50
+ welcome = await tester.start()
51
+ welcome.assert_contains("Welcome")
52
+ welcome.assert_button("Sign up")
53
+
54
+ prompt = await tester.tap_button("Sign up")
55
+ prompt.assert_contains("What's your name?")
56
+ await tester.assert_state(Signup.name)
57
+
58
+ reply = await tester.send_message("Bob")
59
+ reply.assert_contains("Nice to meet you, Bob!")
60
+ await tester.assert_state(Signup.confirm)
61
+ await tester.assert_data(name="Bob")
62
+ ```
63
+
64
+ ## What you get
65
+
66
+ - **Actions that read like a chat.** Send a command, tap a reply or inline button by its visible text, or send a photo.
67
+ - **A reply you can assert on immediately.** Check text, keyboard layout, media, and captions on the message returned by a step.
68
+ - **FSM assertions at the point that matters.** Check transitions and stored data with `assert_state()` and `assert_data()`.
69
+ - **Your real aiogram routing, offline.** Run your routers, filters, middleware, and injected dependencies through the dispatcher without a Telegram token or polling.
70
+
71
+ ## Try it with your bot
72
+
73
+ Install the tester and the async pytest plugin.
74
+
75
+ ```sh
76
+ uv add --dev aiogram-bot-tester pytest pytest-asyncio
77
+ uv run pytest
78
+ ```
79
+
80
+ With pip, install in your test environment.
81
+
82
+ ```sh
83
+ python -m pip install aiogram-bot-tester pytest pytest-asyncio
84
+ python -m pytest
85
+ ```
86
+
87
+ 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.
88
+
89
+ ```python
90
+ import pytest
91
+
92
+ from aiogram_bot_tester import BotTester
93
+ from my_bot.signup import router
94
+
95
+
96
+ @pytest.fixture
97
+ def tester():
98
+ return BotTester.from_routers(router)
99
+ ```
100
+
101
+ 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)`.
102
+
103
+ ## Write tests that match the conversation
104
+
105
+ ### Check a reply on the spot
106
+
107
+ Each `start()`, `send_message()`, `send_command()`, or `tap_button()` returns the last captured reply for that step. A missing reply raises `NoBotResponseError`.
108
+
109
+ ```python
110
+ async def test_welcome_keyboard(tester):
111
+ welcome = await tester.send_command("start")
112
+ welcome.assert_matches(r"^Welcome")
113
+ welcome.assert_keyboard([["Sign up"]])
114
+ ```
115
+
116
+ ### Replay a transcript
117
+
118
+ `chat()` replays a linear, text-only conversation and checks that each reply contains the expected text.
119
+
120
+ ```python
121
+ async def test_welcome_transcript(tester):
122
+ await tester.chat(
123
+ """
124
+ User: /start
125
+ Bot: Welcome
126
+ """
127
+ )
128
+ ```
129
+
130
+ ### Send media and check the attachment
131
+
132
+ 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:
133
+
134
+ ```python
135
+ async def test_avatar(tester):
136
+ reply = await tester.send_photo(file_id="avatar", caption="My avatar")
137
+
138
+ reply.assert_photo(file_id="avatar")
139
+ reply.assert_caption("Avatar saved")
140
+ reply.assert_caption_contains("saved")
141
+ ```
142
+
143
+ 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`.
144
+
145
+ ### Send an album
146
+
147
+ 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.
148
+
149
+ ```python
150
+ from aiogram_bot_tester import Photo
151
+
152
+
153
+ async def test_album(tester):
154
+ replies = await tester.send_album(
155
+ [Photo(file_id="front"), Photo(file_id="back")]
156
+ )
157
+
158
+ front, back = replies
159
+ front.assert_photo(file_id="front")
160
+ back.assert_photo(file_id="back")
161
+ ```
162
+
163
+ ### Feed a custom update
164
+
165
+ 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.
166
+
167
+ ```python
168
+ from aiogram.types import InlineQuery, Update, User
169
+
170
+
171
+ async def test_inline_search(tester):
172
+ replies = await tester.feed_update(
173
+ Update(
174
+ update_id=100,
175
+ inline_query=InlineQuery(
176
+ id="search-1",
177
+ from_user=User(id=1, is_bot=False, first_name="Bob"),
178
+ query="cats",
179
+ offset="",
180
+ ),
181
+ )
182
+ )
183
+
184
+ assert replies == ()
185
+ assert any(
186
+ call.method == "answerInlineQuery" and call.params["inline_query_id"] == "search-1"
187
+ for call in tester.api_calls
188
+ )
189
+ ```
190
+
191
+ ## Why another testing library?
192
+
193
+ Here is how each tool expresses the same check: send `/start` and verify that the bot replied with a welcome message.
194
+
195
+ | Library | What you write | When it fits |
196
+ | --- | --- | --- |
197
+ | **aiogram-bot-tester** | `reply = await tester.start()`<br>`reply.assert_contains("Welcome")` | Follow one conversation step by step and assert directly on replies, buttons, and FSM transitions. |
198
+ | [aiogram.test](https://docs.aiogram.dev/en/v3.33.0/dispatcher/testing.html) | `await bot_user.send("/start")`<br>`assert "Welcome" in bot_chat.messages[-1].text` | Model a stateful Telegram environment with multiple users, chats, rights, and an FSM context. |
199
+ | [aiogram-testing](https://pypi.org/project/aiogram-testing/) | `calls = await tester.send_message("/start")`<br>`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. |
200
+ | [aiogram-test-framework](https://github.com/sgavka/aiogram-test-framework#testuser) | `await user.send_command("start")`<br>`assert user.has_received_message_containing("Welcome")` | Work with simulated users, Telegram object factories, and a request capture log. |
201
+
202
+ 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.
203
+
204
+ ## Learn more
205
+
206
+ - [English tutorial](https://github.com/samedit66/aiogram-bot-tester/blob/main/docs/TUTORIAL_EN.MD)
207
+ - [Русский туториал](https://github.com/samedit66/aiogram-bot-tester/blob/main/docs/TUTORIAL_RU.md)
208
+
209
+ > [!WARNING]
210
+ > This library is under active development. Its API may change between releases.
@@ -0,0 +1,181 @@
1
+ # aiogram-bot-tester
2
+
3
+ [![CI](https://github.com/samedit66/aiogram-bot-tester/actions/workflows/ci-and-publish.yml/badge.svg?branch=main)](https://github.com/samedit66/aiogram-bot-tester/actions/workflows/ci-and-publish.yml)
4
+ [![PyPI version](https://img.shields.io/pypi/v/aiogram-bot-tester?logo=pypi)](https://pypi.org/project/aiogram-bot-tester/)
5
+ [![Python 3.10–3.15](https://img.shields.io/badge/Python-3.10%E2%80%933.15-blue?logo=python)](https://github.com/samedit66/aiogram-bot-tester/actions/workflows/ci-and-publish.yml)
6
+ [![aiogram 3.x](https://img.shields.io/badge/aiogram-3.x-blue?logo=telegram)](https://docs.aiogram.dev/)
7
+ [![Apache 2.0 license](https://img.shields.io/badge/license-Apache%202.0-blue)](https://github.com/samedit66/aiogram-bot-tester/blob/main/LICENSE)
8
+ [![PyPI Downloads](https://static.pepy.tech/personalized-badge/aiogram-bot-tester?period=total&units=INTERNATIONAL_SYSTEM&left_color=GRAY&right_color=BLUE&left_text=downloads)](https://pepy.tech/projects/aiogram-bot-tester)
9
+
10
+ Test aiogram conversations offline. Start the bot, tap a button by its label, and check the reply and the FSM after each step.
11
+
12
+ ```python
13
+ import pytest
14
+
15
+ from my_bot.signup import Signup
16
+
17
+ pytestmark = pytest.mark.asyncio
18
+
19
+
20
+ async def test_signup(tester):
21
+ welcome = await tester.start()
22
+ welcome.assert_contains("Welcome")
23
+ welcome.assert_button("Sign up")
24
+
25
+ prompt = await tester.tap_button("Sign up")
26
+ prompt.assert_contains("What's your name?")
27
+ await tester.assert_state(Signup.name)
28
+
29
+ reply = await tester.send_message("Bob")
30
+ reply.assert_contains("Nice to meet you, Bob!")
31
+ await tester.assert_state(Signup.confirm)
32
+ await tester.assert_data(name="Bob")
33
+ ```
34
+
35
+ ## What you get
36
+
37
+ - **Actions that read like a chat.** Send a command, tap a reply or inline button by its visible text, or send a photo.
38
+ - **A reply you can assert on immediately.** Check text, keyboard layout, media, and captions on the message returned by a step.
39
+ - **FSM assertions at the point that matters.** Check transitions and stored data with `assert_state()` and `assert_data()`.
40
+ - **Your real aiogram routing, offline.** Run your routers, filters, middleware, and injected dependencies through the dispatcher without a Telegram token or polling.
41
+
42
+ ## Try it with your bot
43
+
44
+ Install the tester and the async pytest plugin.
45
+
46
+ ```sh
47
+ uv add --dev aiogram-bot-tester pytest pytest-asyncio
48
+ uv run pytest
49
+ ```
50
+
51
+ With pip, install in your test environment.
52
+
53
+ ```sh
54
+ python -m pip install aiogram-bot-tester pytest pytest-asyncio
55
+ python -m pytest
56
+ ```
57
+
58
+ 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.
59
+
60
+ ```python
61
+ import pytest
62
+
63
+ from aiogram_bot_tester import BotTester
64
+ from my_bot.signup import router
65
+
66
+
67
+ @pytest.fixture
68
+ def tester():
69
+ return BotTester.from_routers(router)
70
+ ```
71
+
72
+ 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)`.
73
+
74
+ ## Write tests that match the conversation
75
+
76
+ ### Check a reply on the spot
77
+
78
+ Each `start()`, `send_message()`, `send_command()`, or `tap_button()` returns the last captured reply for that step. A missing reply raises `NoBotResponseError`.
79
+
80
+ ```python
81
+ async def test_welcome_keyboard(tester):
82
+ welcome = await tester.send_command("start")
83
+ welcome.assert_matches(r"^Welcome")
84
+ welcome.assert_keyboard([["Sign up"]])
85
+ ```
86
+
87
+ ### Replay a transcript
88
+
89
+ `chat()` replays a linear, text-only conversation and checks that each reply contains the expected text.
90
+
91
+ ```python
92
+ async def test_welcome_transcript(tester):
93
+ await tester.chat(
94
+ """
95
+ User: /start
96
+ Bot: Welcome
97
+ """
98
+ )
99
+ ```
100
+
101
+ ### Send media and check the attachment
102
+
103
+ 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:
104
+
105
+ ```python
106
+ async def test_avatar(tester):
107
+ reply = await tester.send_photo(file_id="avatar", caption="My avatar")
108
+
109
+ reply.assert_photo(file_id="avatar")
110
+ reply.assert_caption("Avatar saved")
111
+ reply.assert_caption_contains("saved")
112
+ ```
113
+
114
+ 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`.
115
+
116
+ ### Send an album
117
+
118
+ 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.
119
+
120
+ ```python
121
+ from aiogram_bot_tester import Photo
122
+
123
+
124
+ async def test_album(tester):
125
+ replies = await tester.send_album(
126
+ [Photo(file_id="front"), Photo(file_id="back")]
127
+ )
128
+
129
+ front, back = replies
130
+ front.assert_photo(file_id="front")
131
+ back.assert_photo(file_id="back")
132
+ ```
133
+
134
+ ### Feed a custom update
135
+
136
+ 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.
137
+
138
+ ```python
139
+ from aiogram.types import InlineQuery, Update, User
140
+
141
+
142
+ async def test_inline_search(tester):
143
+ replies = await tester.feed_update(
144
+ Update(
145
+ update_id=100,
146
+ inline_query=InlineQuery(
147
+ id="search-1",
148
+ from_user=User(id=1, is_bot=False, first_name="Bob"),
149
+ query="cats",
150
+ offset="",
151
+ ),
152
+ )
153
+ )
154
+
155
+ assert replies == ()
156
+ assert any(
157
+ call.method == "answerInlineQuery" and call.params["inline_query_id"] == "search-1"
158
+ for call in tester.api_calls
159
+ )
160
+ ```
161
+
162
+ ## Why another testing library?
163
+
164
+ Here is how each tool expresses the same check: send `/start` and verify that the bot replied with a welcome message.
165
+
166
+ | Library | What you write | When it fits |
167
+ | --- | --- | --- |
168
+ | **aiogram-bot-tester** | `reply = await tester.start()`<br>`reply.assert_contains("Welcome")` | Follow one conversation step by step and assert directly on replies, buttons, and FSM transitions. |
169
+ | [aiogram.test](https://docs.aiogram.dev/en/v3.33.0/dispatcher/testing.html) | `await bot_user.send("/start")`<br>`assert "Welcome" in bot_chat.messages[-1].text` | Model a stateful Telegram environment with multiple users, chats, rights, and an FSM context. |
170
+ | [aiogram-testing](https://pypi.org/project/aiogram-testing/) | `calls = await tester.send_message("/start")`<br>`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. |
171
+ | [aiogram-test-framework](https://github.com/sgavka/aiogram-test-framework#testuser) | `await user.send_command("start")`<br>`assert user.has_received_message_containing("Welcome")` | Work with simulated users, Telegram object factories, and a request capture log. |
172
+
173
+ 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.
174
+
175
+ ## Learn more
176
+
177
+ - [English tutorial](https://github.com/samedit66/aiogram-bot-tester/blob/main/docs/TUTORIAL_EN.MD)
178
+ - [Русский туториал](https://github.com/samedit66/aiogram-bot-tester/blob/main/docs/TUTORIAL_RU.md)
179
+
180
+ > [!WARNING]
181
+ > This library is under active development. Its API may change between releases.
@@ -0,0 +1,52 @@
1
+ [project]
2
+ name = "aiogram_bot_tester"
3
+ version = "2.2.1"
4
+ description = "Your missing test toolkit for Telegram bots."
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ license = "Apache-2.0"
8
+ license-files = ["LICENSE"]
9
+ authors = [
10
+ { name = "samedit66", email = "samedit66@yandex.ru" },
11
+ ]
12
+ keywords = ["aiogram", "telegram", "testing", "pytest", "bot"]
13
+ classifiers = [
14
+ "Intended Audience :: Developers",
15
+ "Programming Language :: Python :: 3",
16
+ "Programming Language :: Python :: 3.10",
17
+ "Programming Language :: Python :: 3.11",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: 3.13",
20
+ "Programming Language :: Python :: 3.14",
21
+ "Operating System :: OS Independent",
22
+ "Topic :: Software Development :: Testing",
23
+ "Framework :: Pytest",
24
+ "Typing :: Typed",
25
+ ]
26
+ dependencies = [
27
+ "aiogram>=3.28.2,<4",
28
+ ]
29
+
30
+ [project.urls]
31
+ Homepage = "https://github.com/samedit66/aiogram-bot-tester"
32
+ Documentation = "https://github.com/samedit66/aiogram-bot-tester#readme"
33
+ Repository = "https://github.com/samedit66/aiogram-bot-tester"
34
+ Issues = "https://github.com/samedit66/aiogram-bot-tester/issues"
35
+ Changelog = "https://github.com/samedit66/aiogram-bot-tester/releases"
36
+
37
+ [dependency-groups]
38
+ dev = [
39
+ "mypy>=1.18.2",
40
+ "pytest>=9.0.3",
41
+ "pytest-asyncio>=1.3.0",
42
+ "ruff>=0.15.13",
43
+ ]
44
+
45
+ [tool.mypy]
46
+ python_version = "3.10"
47
+ files = ["src"]
48
+ strict = true
49
+
50
+ [build-system]
51
+ requires = ["uv_build>=0.11.14,<0.12"]
52
+ build-backend = "uv_build"
@@ -0,0 +1,23 @@
1
+ from .api_call import ApiCall, Upload
2
+ from .bot_message import BotMessage
3
+ from .bot_tester import BotTester
4
+ from .buttons import Button, CallbackButton, UnsupportedInlineButton, UrlButton
5
+ from .media import AlbumItem, CaptionEntity, Document, Photo, Sticker, Video, Voice
6
+
7
+ __all__ = [
8
+ "AlbumItem",
9
+ "ApiCall",
10
+ "BotMessage",
11
+ "BotTester",
12
+ "Button",
13
+ "CallbackButton",
14
+ "CaptionEntity",
15
+ "Document",
16
+ "Photo",
17
+ "Sticker",
18
+ "UnsupportedInlineButton",
19
+ "Upload",
20
+ "UrlButton",
21
+ "Video",
22
+ "Voice",
23
+ ]