teremock
Telegram · Realistic · Mocking — A fast, ergonomic testing library for teloxide bots.
teremock enables you to write fast, reliable integration tests for your Telegram bots without network access, API tokens, or external services. Run your entire test suite in seconds, not minutes.
use ;
async
Why teremock?
Lightning Fast
Tests run 15-30x faster than traditional approaches. The mock server starts once and persists across all dispatches within a test — no server restart overhead between interactions.
50 sequential dispatches: ~2 seconds (teremock)
50 sequential dispatches: ~30-60 seconds (server-per-dispatch)
True Black-Box Testing
Test your bot the way users experience it. Send messages, click buttons, trigger commands — then verify the responses. No internal state manipulation, no implementation coupling.
Multi-Step Conversations in One Test
Test complete user flows without juggling multiple test functions. The update() method lets you simulate follow-up messages, button clicks, and entire conversations in a single test.
Zero Configuration
Works out of the box with #[tokio::test]. No custom thread builders, no special runtime configuration, no port management. Each MockBot gets its own server on a dynamically assigned port.
Full Request Inspection
Access both the resulting message and the original bot request for detailed assertions:
let responses = bot.get_responses;
// Check the message that was sent
let msg = &responses.sent_messages_photo.message;
assert_eq!;
// Inspect the raw request your bot made
let request = &responses.sent_messages_photo.bot_request;
assert!;
Features
- Persistent mock server — Server starts once per test, reuses across dispatches
- Fluent builders —
MockMessageText,MockCallbackQuery,MockMessagePhoto, and more - Comprehensive API coverage — 40+ Telegram Bot API methods supported
- Type-safe responses — Dedicated collections for each message type
- Dependency injection — Full support for
dptree::deps![] - Dialogue state — Works with
InMemStorage,RedisStorage, or any teloxide storage - File operations — Mock file uploads, downloads, and media groups
- Stack-safe — Each dispatch runs in its own tokio task with proper stack isolation
Installation
Add to your Cargo.toml:
[]
= "0.1"
Quick Start
1. Extract your handler tree
teremock tests your bot by running updates through your handler tree. Extract it into a separate function:
use ;
type HandlerResult = ;
// Your handler function
async
// Extract the handler tree into a function
// Use it in your main dispatcher
async
2. Write your tests
3. Access detailed request information
For more specific assertions, use typed response collections:
async
4. Test multi-step conversations
Use update() to simulate follow-up messages in the same test. The mock server persists between dispatches, so you can test complete user flows:
async
Working with Teloxide Dialogues
If your bot uses teloxide's dialogue system for stateful conversations, teremock has you covered. Just inject your storage as a dependency and test away.
Setting up dialogue tests
use ;
use ;
async
The dialogue state transitions happen naturally through your handler tree — no need to manually set states. This is black-box testing at its finest.
Supported Telegram API Methods
Messages
sendMessage,sendPhoto,sendVideo,sendAudio,sendVoicesendVideoNote,sendDocument,sendAnimation,sendStickersendLocation,sendVenue,sendContact,sendPoll,sendDicesendInvoice,sendMediaGroup,sendChatAction
Editing
editMessageText,editMessageCaption,editMessageReplyMarkup
Management
deleteMessage,deleteMessages,forwardMessage,copyMessagepinChatMessage,unpinChatMessage,unpinAllChatMessages
Users & Moderation
banChatMember,unbanChatMember,restrictChatMember
Callbacks & Commands
answerCallbackQuery,setMessageReaction,setMyCommands
Files & Bot Info
getFile,getMe,getUpdates,getWebhookInfo
Examples
The examples/ directory contains complete bot implementations with tests:
| Example | Description |
|---|---|
| hello_world_bot | Simple message handling |
| calculator_bot | Dialogue state machine with callbacks |
| deep_linking_bot | Deep linking with command parameters |
| album_bot | Media group handling |
| file_download_bot | File upload and download operations |
| phrase_bot | Database integration patterns |
Database Testing
teremock works seamlessly with database-backed bots. Use your preferred test isolation strategy:
async
Acknowledgments
teremock builds upon the foundation laid by teloxide_tests by LasterAlex. The original library pioneered the concept of mock testing for teloxide bots.
Key architectural changes in teremock:
- Persistent server architecture (15-30x faster test execution)
- Stack-safe dispatch isolation
- Black-box testing philosophy (no internal state manipulation)
- Async
MockBot::new()for proper initialization
License
MIT License — see LICENSE for details.