8

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.

wowkin2
  • 5,895
  • 5
  • 23
  • 66
LinkOut
  • 124
  • 1
  • 8

1 Answers1

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.

wowkin2
  • 5,895
  • 5
  • 23
  • 66
  • 4
    Can you give a simple example for a simple callback handler test handling /start commands? – kozhioyrin Sep 21 '21 at 13:43
  • @oyrinkozhi callback handler should not handle commands, but actually callbacks. Otherwise I misunderstood what you mean. – wowkin2 Sep 21 '21 at 13:58
  • 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 servers – Nathan Tew May 11 '23 at 06:10
  • @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](https://github.com/python-telegram-bot/python-telegram-bot/blob/9997a9f47ea9637eb31bd6d9e657c2bf954d523c/tests/test_replykeyboardmarkup.py). And updated link in answer. – wowkin2 May 11 '23 at 09:49
  • @wowkin2 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 itself – Nathan Tew May 14 '23 at 09:31
  • @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](https://github.com/python-telegram-bot/python-telegram-bot/blob/9997a9f47ea9637eb31bd6d9e657c2bf954d523c/tests/ext/test_conversationhandler.py#L1244), 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. – wowkin2 May 14 '23 at 21:04