Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples how to test your bot #378

Open
sazarkin opened this issue Jul 3, 2020 · 6 comments
Open

Examples how to test your bot #378

sazarkin opened this issue Jul 3, 2020 · 6 comments
Labels
docs Something is missing in docs good first issue A good place to start contributing to this project without going too deep.

Comments

@sazarkin
Copy link

sazarkin commented Jul 3, 2020

Hello!
Thanks for awesome library.

I am trying to write tests for my bot, and not sure how to do it right. I've searched the docs but found nothing.

Do you have some examples?

@uwinx
Copy link
Contributor

uwinx commented Jul 3, 2020

My $0.01 on testing.
Tests are a huge topic to discuss. Proper tests require flexible architecture, otherwise you may have to mock a lot of stuff.
There are different types of tests and bots are not something different to add unit tests.

@sazarkin
Copy link
Author

sazarkin commented Jul 3, 2020 via email

@uwinx uwinx added docs Something is missing in docs good first issue A good place to start contributing to this project without going too deep. labels Jul 13, 2020
@aragentum
Copy link

@sazarkin try to use pytest + pytest-asyncio, in this case, code will be look something like this:

bot\__main__.py

from bot.handlers import echo_handler

...

if __name__ == '__main__':
    echo_handler.setup(dp)
    executor.start_polling(dp, skip_updates=True)

bot\handlers\echo_handler.py

from aiogram import types, Dispatcher


async def echo(message: types.Message):
    await message.answer(message.text)

def setup(dp: Dispatcher):
    dp.register_message_handler(echo)

bot\tests\handlers\test_echo_handler.py

import pytest

from unittest.mock import AsyncMock
from bot.handlers.echo_handler import echo


@pytest.mark.asyncio
async def test_echo_handler():
    text_mock = "test123"
    message_mock = AsyncMock(text=text_mock)
    await echo(message=message_mock)
    message_mock.answer.assert_called_with(text_mock)

@OCCCAS
Copy link

OCCCAS commented Oct 30, 2022

For testing I use aiogram_tests library.
You can find it in GitHub or PyPi, but it works only with aiogram3, but for aiogram2 you can use aiogram_unittest it works with unittest and pytest. aiogram_untitest on GitHub, on PyPi

@Hudrolax
Copy link

I can test different handlers, but I don't know how to test routers' logic. I mean how I can send a mocked message with a defined router (routers' chain) end to assert an answer from some handler?

@LabAsim
Copy link

LabAsim commented Feb 11, 2024

Currently, aiogram_tests is broken. I have found a fork that does work with aiogram 3.3.0.
https://github.com/Like6po/aiogram_tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Something is missing in docs good first issue A good place to start contributing to this project without going too deep.
Projects
None yet
Development

No branches or pull requests

6 participants