-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Refactor and overhaul the test suite #3426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
83 commits
Select commit
Hold shift + click to select a range
fc5aea4
Split tests based on whether they make a request
harshil21 77ecda1
Override get_me in tests to return cached User() object
harshil21 f1207dc
move testing of initialization of bot to Req part
harshil21 4fe9955
use TEST_WITH_OPT_DEPS to justify removing TestBot.localize
harshil21 c67f100
import `TEST_WITH_OPT_DEPS` from conftest instead of redefining every…
harshil21 7b412cf
use context manager for data_file in fixtures
harshil21 8725e4b
rearrange the order of one test method (so tests pass)
harshil21 0920f95
Optimize `TestApplication` significantly by making small changes
harshil21 256758d
remove pytest.mark.timeout from requirements
harshil21 e2cc536
Fix: 2 app & job tests were not a coroutine function
harshil21 0a7d03b
don't close event loop after session end
harshil21 40ad364
use DictExtBot in more places, where appropriate.
harshil21 fb550d1
rearrange test methods in TestBot again
harshil21 58aaf04
Don't rerun tests if they have xfailed
harshil21 b46a4bb
Make the bot_methods decorator not return camelCase names by default
harshil21 dea47cf
small optimization: Make test_coroutine_functions use inspect.iscorou…
harshil21 857949a
Feat: Make some tests run concurrently via asyncio.gather/as_complete…
harshil21 6f40363
Fix/Refactor: Move test_custom_emoji_sticker to TestStickerReq
harshil21 e90f0a0
Miscellaneous fixes
harshil21 b330c97
small refactor: delete a duplicate test, split one test
harshil21 738fd44
refactor and rearrange: remove bot.delete_webhook() from arbitrary ca…
harshil21 77b8438
rearrange tests
harshil21 664c103
Adding two missing slot_behaviour tests in test_sticker.py
harshil21 b2e292e
small refactor: use a for loop, fixture, and add another assert
harshil21 cc1e45e
delete TestUserNoReq::test_de_json_without_username
harshil21 6ea3515
Add a README for tests and add some comments to requirements-dev.txt
harshil21 4b55ee5
hopefully fix tests on GH CI
harshil21 b3e9e8d
Hopefully fix tests on py3.10+
harshil21 4ea871e
Hotfix to make the tests run
harshil21 4f69b52
add a flaky marker to test_signal_handler
harshil21 c8fab41
Update keys for bot data
Bibo-Joshi 8fa504f
remove env var fetching of bots
harshil21 7f5996e
Experiment: Try making Job Queue tests more stable
harshil21 1baebf2
Experiment 1 cont: Attempt to stablize job queue tests on macos
harshil21 a608e48
Experiment 1 cont: stablilize another test on macos
harshil21 67b2ba2
Experiment 1 end: use asyncio.wait_for so tests cannot hang
harshil21 5c778d1
Experiment 2: Make GH CI use 10 workers with loadgroup
harshil21 b5c5d16
merge master and fix conflicts
harshil21 050f650
mark job queue tests as flaky
harshil21 d7c09a1
attempt to fix potential race condition in test_passport.py
harshil21 e37ab7e
attempt to fix the error: 'free variable input_video referenced befor…
harshil21 3bdd981
fix error while collecting tests in test_datetime.py
harshil21 402aa03
why tests no run?
harshil21 024315b
change scope of real_topic back to function
harshil21 f3c671d
Experiment 2 end: Revert back to -n auto, but keep --dist=loadgroup
harshil21 dc847df
Experiment 3: Run tests of optional dependencies in one go
harshil21 744308d
run getUpdates and webhook tests in same worker
harshil21 72e0a6e
Experiment 3 cont: make the variable in a single line
harshil21 ebad4e4
increase sleep duration in test_conversation_timout
harshil21 a9dcdae
merge master and fix conflict
harshil21 2510df8
fix potential race condition while unpinning message
harshil21 8c3296b
merge master and fix conflicts'
harshil21 03501e4
make test_get_chat_arbitrary_callback_data use channel_id for testing
harshil21 891ed29
Address review of fc5aea4: Use `TestXXXWith(out)Request`
harshil21 6aa539f
make tz_bot fixture use default_bots dictionary as cache instead
harshil21 0588363
Address review of fc5aea4: Change Space to suggested name instead
harshil21 e079c6b
Address review of fc5aea4: Add docstring to TestBotWithoutRequest
harshil21 53031be
Address review of 77ecda1: Changes in test_bot.py and conftest.py
harshil21 2596d4c
Address review of 4fe9955: Change skipif reason
harshil21 ab3be7e
Address review of 0920f95: Rename the bot used in app tests
harshil21 ebecf27
fix some typos and tiny bugs in test_app.py
harshil21 e60d2e1
Address review of 857949a: slightly modify test logic
harshil21 01c8b5c
Address review of b330c97: change variable name i -> key
harshil21 6a9bbc0
Address review of 6ea3515: Update test readme and add to index.html
harshil21 119858d
Address review of 4b55ee5: Drop testing of file_id with get_file
harshil21 a9ce337
Address review of 4ea871e: put variable in class instead of global
harshil21 7d64045
Address review of 4f69b52: Modify comment in test_signal_handlers
harshil21 f65efd6
merge master and fix conflicts
harshil21 6acd324
add CSI comment about shutting down bots
harshil21 ac14ae1
add back test_flag as autouse function
harshil21 eb8de60
add except TelegramError to an app test
harshil21 5d6a0c7
fix link in contributing.rst
harshil21 2587d97
expand comment in test.yml
harshil21 3da231f
move contributing and testing below coc
harshil21 7a79ae4
make BotInfo a proper class
harshil21 ac401b2
self -> bot
harshil21 e6eec74
move one_time_bot from application.py to conftest
harshil21 38ca987
add CSI comment about datetime test parametrization
harshil21 5b45a02
revert Experiment 1
harshil21 a3c4b03
add csi comment in get_bot_user
harshil21 4d29721
Revert Experiment 2: tests are a bit too flaky
harshil21 018d82b
merge master, fix conflicts and apply new changes
harshil21 45a092a
merge master and fix conflict yet again
harshil21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.. include:: ../../tests/README.rst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
pre-commit | ||
pre-commit # needed for pre-commit hooks in the git commit command | ||
|
||
# For the test suite | ||
pytest==7.2.1 | ||
pytest-asyncio==0.20.3 | ||
pytest-timeout==2.1.0 # used to timeout tests | ||
pytest-asyncio==0.20.3 # needed because pytest doesn't come with native support for coroutines as tests | ||
pytest-xdist==3.1.0 # xdist runs tests in parallel | ||
|
||
flaky # Used for flaky tests (flaky decorator) | ||
beautifulsoup4 # used in test_official for parsing tg docs | ||
|
||
wheel # required for building the wheels for releases |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
============== | ||
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. | ||
|
||
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``. | ||
|
||
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 for every combination of Python version and | ||
OS. | ||
|
||
Thus, number of bots used for testing locally is 2 (called as fallback bots), and on the CI, | ||
its [3.7, 3.8, 3.9, 3.10, 3.11] x [ubuntu-latest, macos-latest, windows-latest] = 15. Bringing the | ||
total number of bots used for testing to 17. | ||
|
||
|
||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.