Updated Writing Tests (markdown)

Bibo-Joshi 2024-01-07 12:56:40 +01:00
parent 34db7fa33a
commit 8b8d60030f

@ -2,7 +2,13 @@ In case you want to practice [test-driven development](https://en.wikipedia.org/
## Unit Tests ## Unit Tests
Unit tests are performed on a logically encapsulated component of the system. The definition of unit tests in contrast to integration tests is that they have no external dependencies. Unit tests are performed on a logically encapsulated component of the system. The definition of unit tests in contrast to integration tests is that they have no external dependencies.
@Eldinnie has written an initial POC of a unit test framework for python-telegram-bot, but as the library grew it was not maintained. Perhaps you might be able to help us out here and help in completing the project ;)
Thanks to PTBs modular structure, integration with unit tests can already be achieved for many use cases without additional tooling. For example, you can
* manually enqueue updates in the [`Application.update_queue`](https://docs.python-telegram-bot.org/en/stable/telegram.ext.application.html#telegram.ext.Application.update_queue) or directly call [`Application.process_update`](https://docs.python-telegram-bot.org/en/stable/telegram.ext.application.html#telegram.ext.Application.process_update). This allows you to check that verify that updates are being processed as desiderd without fetching any data from Telegram. Note that for this approach, you'll probably want to set [the updater to `None`](https://docs.python-telegram-bot.org/en/stable/telegram.ext.applicationbuilder.html#telegram.ext.ApplicationBuilder.updater)
* mock the networking connection to Telegram, i.e. instead of sending the API requests to Telegram you send them to a fictional backend that you control yourself. That way, your tests can not be corrupted by networking issues and do not need an internet connection. This can be achieved e.g. by subclassing the [`Bot`](https://docs.python-telegram-bot.org/en/stable/telegram.bot.html) class and overriding all methods or by implementing a subclass of [`BaseRequest`](https://docs.python-telegram-bot.org/en/stable/telegram.request.baserequest.html).
Moreover, @Eldinnie has written an initial POC of a unit test framework for python-telegram-bot, but as the library grew it was not maintained. Perhaps you might be able to help us out here and help in completing the project ;)
https://github.com/Eldinnie/ptbtest https://github.com/Eldinnie/ptbtest
## Integration Tests ## Integration Tests