How to create unit test for a Python Telegram Bot
Asked Answered
B

1

9

I've built this Telegram Bot in Python, with python-telegram-bot. It's not so complex, but I want to do some regression tests to check if everything works fine after a new feature or a change, and more generally to test specific features to find bugs/edge cases.

How can I achieve this?
For now, I'm doing this manually, clicking bot's keyboard and typing some text.

Brashear answered 27/8, 2020 at 20:45 Comment(0)
N
4

Have you looked to unit tests that are present in python-telegram-bot library? I think it is a good place to start.

For example, in this file (historical version) you can see how to test a dialog with bot that uses ConversationHandler.

Nedra answered 1/9, 2020 at 9:46 Comment(6)
Can you give a simple example for a simple callback handler test handling /start commands?Hight
@oyrinkozhi callback handler should not handle commands, but actually callbacks. Otherwise I misunderstood what you mean.Nedra
isn't that for testing the PTB package itself, and no bots that you build using the package? testing your own bot would require using either another package to automate sending messages to your bot, or creating a mock bot that doesn't need to communicate with telegram's serversBuenabuenaventura
@NathanTew depending on what level of testing you need: unit, integration or functional. There a lot of new tests were added since original answer, but if you'll look into test_replykeyboardmarkup.py there is a TestReplyKeyboardMarkupWithRequest with test for request<->response link to historical. And updated link in answer.Nedra
@Nedra I assumed that OP was looking for unit tests for the conversation flow/logic for their implementation for their own bot (which is also what I need), but the file you linked seems to be testing the sendMessage method of the PTB package itselfBuenabuenaventura
@NathanTew conversation is not a unit-test, it is functional test that tests more than a single function call. For what you need to you can have a look into test_conversationhandler.py, but it is a bit more complicated there, mainly because they needed to mock everything there for test. In your case - you'll be able to reuse your app.Nedra

© 2022 - 2024 — McMap. All rights reserved.