python-telegram-bot/tests
2023-12-10 20:17:11 +01:00
..
_files Adjust Tests to New Error Messages (#3970) 2023-11-11 11:35:23 +01:00
_games Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
_inline Update pre-commit Dependencies (#3916) 2023-10-03 14:17:42 +02:00
_passport One-Time Code Formatting Improvement via --preview Flag of black (#3882) 2023-09-22 18:19:21 +02:00
_payment Adjust Tests to New Error Messages (#3970) 2023-11-11 11:35:23 +01:00
_utils Remove Superfluous Defaults.__ne__ (#3884) 2023-09-11 21:12:20 +02:00
auxil Verify Type Hints for Bot Method & Telegram Class Parameters (#3868) 2023-09-15 21:33:42 +02:00
data Improve Backwards Compatibility of TelegramObjects Pickle Behavior (#3382) 2022-11-24 12:11:37 +01:00
docs Remove Functionality Deprecated Since Bot API 6.5, 6.6 or 6.7 (#3858) 2023-09-03 14:23:48 +02:00
ext Improve Error Handling in Built-In Webhook Handler (#3987) 2023-12-09 17:35:23 +01:00
request Adjust read_timeout Behavior for Bot.get_updates (#3963) 2023-11-27 18:24:21 +01:00
__init__.py Fix travis 2015-09-07 16:33:00 -03:00
conftest.py Verify Type Hints for Bot Method & Telegram Class Parameters (#3868) 2023-09-15 21:33:42 +02:00
README.rst Documentation Improvements (#3919) 2023-11-27 19:11:25 +01:00
test_bot.py Add Docstring to Dunder Methods (#3929) 2023-12-10 20:17:11 +01:00
test_botcommand.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_botcommandscope.py Empower ruff (#3594) 2023-03-25 19:18:04 +01:00
test_botdescription.py API 6.6 (#3584) 2023-03-25 11:47:26 +01:00
test_botname.py API 6.7 (#3673) 2023-05-07 13:44:34 +02:00
test_callbackquery.py Empower ruff (#3594) 2023-03-25 19:18:04 +01:00
test_chat.py API 6.8 (#3853) 2023-09-03 13:43:44 +02:00
test_chatadministratorrights.py API 6.9 (#3898) 2023-10-03 14:32:57 +02:00
test_chatinvitelink.py Localize Received datetime Objects According to Defaults.tzinfo (#3632) 2023-04-18 16:16:23 +02:00
test_chatjoinrequest.py Localize Received datetime Objects According to Defaults.tzinfo (#3632) 2023-04-18 16:16:23 +02:00
test_chatlocation.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_chatmember.py API 6.9 (#3898) 2023-10-03 14:32:57 +02:00
test_chatmemberupdated.py API 6.7 (#3673) 2023-05-07 13:44:34 +02:00
test_chatpermissions.py Remove Functionality Deprecated Since Bot API 6.5, 6.6 or 6.7 (#3858) 2023-09-03 14:23:48 +02:00
test_choseninlineresult.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_constants.py Empower ruff (#3594) 2023-03-25 19:18:04 +01:00
test_dice.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_error.py Empower ruff (#3594) 2023-03-25 19:18:04 +01:00
test_forcereply.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_forum.py Remove Functionality Deprecated Since Bot API 6.5, 6.6 or 6.7 (#3858) 2023-09-03 14:23:48 +02:00
test_helpers.py Drop Support for Python 3.7 (#3728, #3742, #3749, #3740, #3754, #3753, #3764, #3762, #3759) 2023-06-29 18:17:47 +02:00
test_inlinequeryresultsbutton.py API 6.7 (#3673) 2023-05-07 13:44:34 +02:00
test_keyboardbutton.py Remove Functionality Deprecated Since Bot API 6.5, 6.6 or 6.7 (#3858) 2023-09-03 14:23:48 +02:00
test_keyboardbuttonpolltype.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_keyboardbuttonrequest.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_loginurl.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_menubutton.py Empower ruff (#3594) 2023-03-25 19:18:04 +01:00
test_message.py Remove Functionality Deprecated Since Bot API 6.5, 6.6 or 6.7 (#3858) 2023-09-03 14:23:48 +02:00
test_messageautodeletetimerchanged.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_messageentity.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_messageid.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_meta.py Empower ruff (#3594) 2023-03-25 19:18:04 +01:00
test_modules.py Update Copyright to 2023 (#3459) 2023-01-01 21:31:29 +01:00
test_official.py Improve Type Hinting for Arguments with Default Values in Bot (#3942) 2023-10-25 21:53:43 +02:00
test_poll.py Drop Backward Compatibility Layer Introduced in #3853 (API 6.8) (#3873) 2023-10-03 15:15:17 +02:00
test_pollhandler.py Empower ruff (#3594) 2023-03-25 19:18:04 +01:00
test_proximityalerttriggered.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_replykeyboardmarkup.py Empower ruff (#3594) 2023-03-25 19:18:04 +01:00
test_replykeyboardremove.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_sentwebappmessage.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_shared.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_slots.py Add More ruff Rules (#3763) 2023-06-29 11:38:09 +02:00
test_story.py API 6.8 (#3853) 2023-09-03 13:43:44 +02:00
test_switchinlinequerychosenchat.py API 6.7 (#3673) 2023-05-07 13:44:34 +02:00
test_telegramobject.py Improve Warning Categories & Stacklevels (#3674) 2023-04-27 22:36:04 +02:00
test_update.py API 6.8 (#3853) 2023-09-03 13:43:44 +02:00
test_user.py Empower ruff (#3594) 2023-03-25 19:18:04 +01:00
test_userprofilephotos.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_version.py Empower ruff (#3594) 2023-03-25 19:18:04 +01:00
test_videochat.py Localize Received datetime Objects According to Defaults.tzinfo (#3632) 2023-04-18 16:16:23 +02:00
test_warnings.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_webappdata.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_webappinfo.py Tidy Up Tests Directory (#3553) 2023-02-22 20:19:46 +01:00
test_webhookinfo.py Localize Received datetime Objects According to Defaults.tzinfo (#3632) 2023-04-18 16:16:23 +02:00
test_writeaccessallowed.py Add Rich Equality Comparison to WriteAccessAllowed (#3911) 2023-10-02 20:21:51 +02: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 ``requirements-dev.txt`` 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.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


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 same bot is 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