python-telegram-bot/tests
Juan Andrés Cuevas 4f255b6e21
Unify datetime Imports (#4605)
Co-authored-by: Miguel Salomon <128310363+Migueldsc12@users.noreply.github.com>
Co-authored-by: Jeamhowards Montiel <106713677+Jeam-zx@users.noreply.github.com>
Co-authored-by: Snehashish Biswas <coderrx06@gmail.com>
Co-authored-by: Luis Pérez <luis.i.perez.0@gmail.com>
Co-authored-by: henryg311 <55552582+henryg311@users.noreply.github.com>
Co-authored-by: AnyaMarcanito <129221958+AnyaMarcanito@users.noreply.github.com>
Co-authored-by: Jeam Montiel <19-10234@usb.ve>
2024-12-15 10:26:37 +01:00
..
_files Improve Exception Handling in File.download_* (#4542) 2024-11-03 16:35:16 +01:00
_games Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
_inline Full Support for Bot API 8.0 (#4566, #4568, #4570, #4571, #4574, #4576, #4572) 2024-12-01 10:26:48 +01:00
_passport Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
_payment Full Support for Bot API 8.0 (#4566, #4568, #4570, #4571, #4574, #4576, #4572) 2024-12-01 10:26:48 +01:00
_utils Update Copyright to 2024 (#4121) 2024-02-19 20:06:25 +01:00
auxil Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
data Improve Backwards Compatibility of TelegramObjects Pickle Behavior (#3382) 2022-11-24 12:11:37 +01:00
docs Drop Support for Python 3.8 (#4398) 2024-10-24 20:48:49 +02:00
ext Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
request Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_official Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
__init__.py Fix travis 2015-09-07 16:33:00 -03:00
conftest.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
README.rst Documentation Improvements (#4536, #4556) 2024-11-04 20:38:41 +01:00
test_birthdate.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_bot.py Full Support for Bot API 8.0 (#4566, #4568, #4570, #4571, #4574, #4576, #4572) 2024-12-01 10:26:48 +01:00
test_botcommand.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_botcommandscope.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_botdescription.py Rename Testing Base Classes (#4453) 2024-09-03 05:24:25 +02:00
test_botname.py Rename Testing Base Classes (#4453) 2024-09-03 05:24:25 +02:00
test_business.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_callbackquery.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_chat.py Full Support for Bot API 8.0 (#4566, #4568, #4570, #4571, #4574, #4576, #4572) 2024-12-01 10:26:48 +01:00
test_chatadministratorrights.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_chatbackground.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_chatboost.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_chatfullinfo.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_chatinvitelink.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_chatjoinrequest.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_chatlocation.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_chatmember.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_chatmemberupdated.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_chatpermissions.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_choseninlineresult.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_constants.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_copytextbutton.py Bot API 7.11 (#4546) 2024-11-04 20:11:10 +01:00
test_dice.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_enum_types.py Add Internal Constants for Encodings (#4378) 2024-07-21 21:13:30 +02:00
test_error.py Update Copyright to 2024 (#4121) 2024-02-19 20:06:25 +01:00
test_forcereply.py Rename Testing Base Classes (#4453) 2024-09-03 05:24:25 +02:00
test_forum.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_gifts.py Full Support for Bot API 8.0 (#4566, #4568, #4570, #4571, #4574, #4576, #4572) 2024-12-01 10:26:48 +01:00
test_giveaway.py API 7.10 (#4461, #4460, #4463, #4464) 2024-09-17 18:09:19 +02:00
test_helpers.py Update Copyright to 2024 (#4121) 2024-02-19 20:06:25 +01:00
test_inlinequeryresultsbutton.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_keyboardbutton.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_keyboardbuttonpolltype.py Rename Testing Base Classes (#4453) 2024-09-03 05:24:25 +02:00
test_keyboardbuttonrequest.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_linkpreviewoptions.py Rename Testing Base Classes (#4453) 2024-09-03 05:24:25 +02:00
test_loginurl.py Rename Testing Base Classes (#4453) 2024-09-03 05:24:25 +02:00
test_maybeinaccessiblemessage.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_menubutton.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_message.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_messageautodeletetimerchanged.py Update Copyright to 2024 (#4121) 2024-02-19 20:06:25 +01:00
test_messageentity.py Drop Support for Python 3.8 (#4398) 2024-10-24 20:48:49 +02:00
test_messageid.py Update Copyright to 2024 (#4121) 2024-02-19 20:06:25 +01:00
test_messageorigin.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_meta.py Drop python-telegram-bot-raw And Switch to pyproject.toml Based Packaging (#4288) 2024-06-15 10:29:19 +02:00
test_modules.py Update Copyright to 2024 (#4121) 2024-02-19 20:06:25 +01:00
test_paidmedia.py API 7.10 (#4461, #4460, #4463, #4464) 2024-09-17 18:09:19 +02:00
test_poll.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_proximityalerttriggered.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_reaction.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_reply.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_replykeyboardmarkup.py Rename Testing Base Classes (#4453) 2024-09-03 05:24:25 +02:00
test_replykeyboardremove.py Rename Testing Base Classes (#4453) 2024-09-03 05:24:25 +02:00
test_sentwebappmessage.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_shared.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_slots.py Update Copyright to 2024 (#4121) 2024-02-19 20:06:25 +01:00
test_stars.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_story.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_switchinlinequerychosenchat.py Rename Testing Base Classes (#4453) 2024-09-03 05:24:25 +02:00
test_telegramobject.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_update.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_user.py Full Support for Bot API 8.0 (#4566, #4568, #4570, #4571, #4574, #4576, #4572) 2024-12-01 10:26:48 +01:00
test_userprofilephotos.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_version.py Update Copyright to 2024 (#4121) 2024-02-19 20:06:25 +01:00
test_videochat.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_warnings.py Add Version to PTBDeprecationWarning (#4262) 2024-05-20 16:12:34 +02:00
test_webappdata.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_webappinfo.py Enforce the offline_bot Fixture in Test*WithoutRequest (#4465) 2024-09-13 19:10:09 +02:00
test_webhookinfo.py Unify datetime Imports (#4605) 2024-12-15 10:26:37 +01:00
test_writeaccessallowed.py Update Copyright to 2024 (#4121) 2024-02-19 20:06:25 +01:00

==============
Testing in PTB
==============

PTB uses `pytest`_ for testing. To run the tests, you need to
have pytest installed along with a few other dependencies. You can find the list of dependencies
in the ``pyproject.toml`` file in the root of the repository.

Running tests
=============

To run the entire test suite, you can use the following command:

.. code-block:: bash

    $ pytest

This will run all the tests, including the ones which make a request to the Telegram servers, which
may take a long time (total > 13 mins). To run only the tests that don't require a connection, you
can run the following command:

.. code-block:: bash

    $ pytest -m no_req

Or alternatively, you can run the following command to run only the tests that require a connection:

.. code-block:: bash

    $ pytest -m req

To further speed up the tests, you can run them in parallel using the ``-n`` flag (requires `pytest-xdist`_). But beware that
this will use multiple CPU cores on your machine. The ``--dist`` flag is used to specify how the
tests will be distributed across the cores. The ``loadgroup`` option is used to distribute the tests
such that tests marked with ``@pytest.mark.xdist_group("name")`` are run on the same core — important if you want avoid race conditions in some tests:

.. code-block:: bash

    $ pytest -n auto --dist=loadgroup

This will result in a significant speedup, but may cause some tests to fail. If you want to run
the failed tests in isolation, you can use the ``--lf`` flag:

.. code-block:: bash

    $ pytest --lf


Writing tests
=============

PTB has a separate test file for every file in the ``telegram.*`` namespace. Further, the tests for
the ``telegram`` module are split into two classes, based on whether the test methods in them make a
request or not. When writing tests, make sure to split them into these two classes, and make sure
to name the test class as: ``TestXXXWithoutRequest`` for tests that don't make a request, and ``TestXXXWithRequest`` for tests that do.

Writing tests is a creative process; allowing you to design your test however you'd like, but there
are a few conventions that you should follow:

- Each new test class needs a ``test_slot_behaviour``, ``test_to_dict``, ``test_de_json`` and
  ``test_equality`` (in most cases).

- Make use of pytest's fixtures and parametrize wherever possible. Having knowledge of pytest's
  tooling can help you as well. You can look at the existing tests for examples and inspiration.

- New fixtures should go into ``conftest.py``. New auxiliary functions and classes, used either directly in the tests or in the fixtures, should go into the ``tests/auxil`` directory.

If you have made some API changes, you may want to run ``test_official`` to validate that the changes are
complete and correct. To run it, export an environment variable first:

.. code-block:: bash

    $ export TEST_OFFICIAL=true

and then run ``pytest tests/test_official/test_official.py``. Note: You need py 3.10+ to run this test.

We also have another marker, ``@pytest.mark.dev``, which you can add to tests that you want to run selectively.
Use as follows:

.. code-block:: bash

    $ pytest -m dev


Debugging tests
===============

Writing tests can be challenging, and fixing failing tests can be even more so. To help with this,
PTB has started to adopt the use of ``logging`` in the test suite. You can insert debug logging 
statements in your tests to help you understand what's going on. To enable these logs, you can set 
``log_level = DEBUG`` in ``setup.cfg`` or use the ``--log-level=INFO`` flag when running the tests.
If a test is large and complicated, it is recommended to leave the debug logs for others to use as 
well.


Bots used in tests
==================

If you run the tests locally, the test setup will use one of the two public bots available. Which
bot of the two gets chosen for the test session is random. Whereas when the tests on the
Github Actions CI are run, the test setup allocates a different, but the same bot is allocated for every combination of Python version and
OS. The operating systems and Python versions the CI runs the tests on can be viewed in the `corresponding workflow`_.


That's it! If you have any questions, feel free to ask them in the `PTB dev
group`_.

.. _pytest: https://docs.pytest.org/en/stable/
.. _pytest-xdist: https://pypi.org/project/pytest-xdist/
.. _PTB dev group: https://t.me/pythontelegrambotgroup
.. _corresponding workflow: https://github.com/python-telegram-bot/python-telegram-bot/blob/master/.github/workflows/unit_tests.yml