Run Unit Tests in Parallel (#3283)

This commit is contained in:
Harshil 2022-10-23 17:55:49 +05:30 committed by GitHub
parent 24d390e1aa
commit 87b2e29d60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 16 deletions

View file

@ -98,6 +98,13 @@ Here's how to make a one-off code change.
$ pytest -v $ pytest -v
Since the tests can take a while to run, you can speed things up by running them in parallel
using `pytest-xdist`_ (note that this may effect the result of the test in some rare cases):
.. code-block:: bash
$ pytest -v -n auto --dist=loadfile
To run ``test_official`` (particularly useful if you made API changes), run To run ``test_official`` (particularly useful if you made API changes), run
.. code-block:: bash .. code-block:: bash
@ -280,3 +287,4 @@ break the API classes. For example:
.. _`RTD build`: https://docs.python-telegram-bot.org/en/doc-fixes .. _`RTD build`: https://docs.python-telegram-bot.org/en/doc-fixes
.. _`CSI`: https://standards.mousepawmedia.com/en/stable/csi.html .. _`CSI`: https://standards.mousepawmedia.com/en/stable/csi.html
.. _`section`: #documenting .. _`section`: #documenting
.. _`pytest-xdist`: https://github.com/pytest-dev/pytest-xdist

View file

@ -14,22 +14,10 @@ jobs:
pytest: pytest:
name: pytest name: pytest
runs-on: ${{matrix.os}} runs-on: ${{matrix.os}}
continue-on-error: ${{ matrix.experimental }}
strategy: strategy:
matrix: matrix:
python-version: ['3.7', '3.8', '3.9', '3.10'] python-version: ['3.7', '3.8', '3.9', '3.10', '3.11.0-rc.2']
os: [ubuntu-latest, windows-latest, macos-latest] os: [ubuntu-latest, windows-latest, macos-latest]
experimental: [false]
include:
- python-version: 3.11.0-rc.2
os: ubuntu-latest
experimental: true
- python-version: 3.11.0-rc.2
os: windows-latest
experimental: true
- python-version: 3.11.0-rc.2
os: macos-latest
experimental: true
fail-fast: False fail-fast: False
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
@ -44,6 +32,7 @@ jobs:
python -W ignore -m pip install -r requirements.txt python -W ignore -m pip install -r requirements.txt
python -W ignore -m pip install -r requirements-opts.txt python -W ignore -m pip install -r requirements-opts.txt
python -W ignore -m pip install -r requirements-dev.txt python -W ignore -m pip install -r requirements-dev.txt
python -W ignore -m pip install pytest-xdist[psutil]
- name: Test with pytest - name: Test with pytest
# We run 4 different suites here # We run 4 different suites here
@ -65,7 +54,9 @@ jobs:
no_rate_limiter_exit=$? no_rate_limiter_exit=$?
export TEST_RATE_LIMITER='true' export TEST_RATE_LIMITER='true'
pip install -r requirements-opts.txt pip install -r requirements-opts.txt
pytest -v --cov --cov-append # `-n auto --dist loadfile` uses pytest-xdist to run each test file on a different CPU
# worker
pytest -v --cov --cov-append -n auto --dist loadfile
full_exit=$? full_exit=$?
special_exit=$(( no_pytz_exit > no_passport_exit ? no_pytz_exit : no_passport_exit )) special_exit=$(( no_pytz_exit > no_passport_exit ? no_pytz_exit : no_passport_exit ))
special_exit=$(( special_exit > no_rate_limiter_exit ? special_exit : no_rate_limiter_exit )) special_exit=$(( special_exit > no_rate_limiter_exit ? special_exit : no_rate_limiter_exit ))

View file

@ -3,6 +3,7 @@ pre-commit
pytest==7.1.3 pytest==7.1.3
pytest-asyncio==0.19.0 pytest-asyncio==0.19.0
pytest-timeout==2.1.0 # used to timeout tests pytest-timeout==2.1.0 # used to timeout tests
pytest-xdist==2.5.0 # xdist runs tests in parallel
flaky # Used for flaky tests (flaky decorator) flaky # Used for flaky tests (flaky decorator)
beautifulsoup4 # used in test_official for parsing tg docs beautifulsoup4 # used in test_official for parsing tg docs

View file

@ -279,6 +279,8 @@ class TestBot:
print(record) print(record)
if record.getMessage().startswith("Task was destroyed but it is pending"): if record.getMessage().startswith("Task was destroyed but it is pending"):
caplog.records.pop(idx) caplog.records.pop(idx)
if record.getMessage().startswith("Task exception was never retrieved"):
caplog.records.pop(idx)
assert len(caplog.records) == 3 assert len(caplog.records) == 3
assert caplog.records[0].getMessage().startswith("Entering: get_me") assert caplog.records[0].getMessage().startswith("Entering: get_me")
assert caplog.records[-1].getMessage().startswith("Exiting: get_me") assert caplog.records[-1].getMessage().startswith("Exiting: get_me")

View file

@ -31,8 +31,8 @@ from telegram.ext import Defaults
from tests.conftest import env_var_2_bool from tests.conftest import env_var_2_bool
ABSOLUTE_TIME_SPECS = [ ABSOLUTE_TIME_SPECS = [
dtm.datetime.now(tz=dtm.timezone(dtm.timedelta(hours=-7))), dtm.datetime.now(tz=dtm.timezone(dtm.timedelta(hours=-7))).replace(second=0, microsecond=0),
dtm.datetime.utcnow(), dtm.datetime.utcnow().replace(second=0, microsecond=0),
] ]
DELTA_TIME_SPECS = [dtm.timedelta(hours=3, seconds=42, milliseconds=2), 30, 7.5] DELTA_TIME_SPECS = [dtm.timedelta(hours=3, seconds=42, milliseconds=2), 30, 7.5]
TIME_OF_DAY_TIME_SPECS = [ TIME_OF_DAY_TIME_SPECS = [