From b9c28c4893d1eceafed4ae946e92462bba1736d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Andr=C3=A9s=20Margffoy=20Tuay?= Date: Tue, 13 Aug 2024 16:07:39 -0500 Subject: [PATCH] Add plugin tests --- .github/workflows/main.yml | 10 ++- README.rst | 6 +- src/pytest_run_parallel/plugin.py | 2 +- tests/test_run_parallel.py | 111 ++++++++++++++++++++++-------- tox.ini | 9 ++- 5 files changed, 101 insertions(+), 37 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 13b2de2..2f9c3d6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,15 +13,21 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - include: - - python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', 'pypy-3.8'] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13', '3.13t', 'pypy-3.8'] steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 + if: ${{ !endsWith(matrix.python-version, 't') }} with: python-version: ${{ matrix.python-version }} + - uses: deadsnakes/action@6c8b9b82fe0b4344f4b98f2775fcc395df45e494 # v3.1.0 + if: ${{ endsWith(matrix.python-version, 't') }} + with: + python-version: "3.13" + nogil: true + - name: Install tox run: pip install tox diff --git a/README.rst b/README.rst index f2840ac..03d0cb5 100644 --- a/README.rst +++ b/README.rst @@ -10,8 +10,8 @@ pytest-run-parallel :target: https://pypi.org/project/pytest-run-parallel :alt: Python versions -.. image:: https://github.com/andfoy/pytest-run-parallel/actions/workflows/main.yml/badge.svg - :target: https://github.com/andfoy/pytest-run-parallel/actions/workflows/main.yml +.. image:: https://github.com/Quansight-Labs/pytest-run-parallel/actions/workflows/main.yml/badge.svg + :target: https://github.com/Quansight-Labs/pytest-run-parallel/actions/workflows/main.yml :alt: See Build Status on GitHub Actions A simple pytest plugin to run tests concurrently @@ -69,7 +69,7 @@ If you encounter any problems, please `file an issue`_ along with a detailed des .. _`GNU GPL v3.0`: https://www.gnu.org/licenses/gpl-3.0.txt .. _`Apache Software License 2.0`: https://www.apache.org/licenses/LICENSE-2.0 .. _`cookiecutter-pytest-plugin`: https://github.com/pytest-dev/cookiecutter-pytest-plugin -.. _`file an issue`: https://github.com/andfoy/pytest-run-parallel/issues +.. _`file an issue`: https://github.com/Quansight-Labs/pytest-run-parallel/issues .. _`pytest`: https://github.com/pytest-dev/pytest .. _`tox`: https://tox.readthedocs.io/en/latest/ .. _`pip`: https://pypi.org/project/pip/ diff --git a/src/pytest_run_parallel/plugin.py b/src/pytest_run_parallel/plugin.py index 4669a00..44b7524 100644 --- a/src/pytest_run_parallel/plugin.py +++ b/src/pytest_run_parallel/plugin.py @@ -11,7 +11,7 @@ def pytest_addoption(parser): dest='parallel_threads', default=1, type=int, - help='Set the number of threads used to execute each test concurrently' + help='Set the number of threads used to execute each test concurrently.' ) diff --git a/tests/test_run_parallel.py b/tests/test_run_parallel.py index 4655593..8221fd1 100644 --- a/tests/test_run_parallel.py +++ b/tests/test_run_parallel.py @@ -1,61 +1,116 @@ -def test_bar_fixture(pytester): + +def test_default_threads(pytester): """Make sure that pytest accepts our fixture.""" # create a temporary pytest test module pytester.makepyfile(""" - def test_sth(bar): - assert bar == "europython2015" + import pytest + from threading import Lock + + class Counter: + def __init__(self): + self._count = 0 + self._lock = Lock() + + def increase(self): + with self._lock: + self._count += 1 + + @pytest.fixture(scope='session') + def counter(): + return Counter() + + @pytest.mark.order(1) + def test_thread_increase(counter): + counter.increase() + + @pytest.mark.order(2) + @pytest.mark.parallel_threads(1) + def test_check_thread_count(counter): + assert counter._count == 10 """) # run pytest with the following cmd args result = pytester.runpytest( - '--foo=europython2015', + '--parallel-threads=10', '-v' ) # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines([ - '*::test_sth PASSED*', + '*::test_check_thread_count PASSED*', ]) # make sure that we get a '0' exit code for the testsuite assert result.ret == 0 -def test_help_message(pytester): - result = pytester.runpytest( - '--help', - ) - # fnmatch_lines does an assertion internally - result.stdout.fnmatch_lines([ - 'run-parallel:', - '*--foo=DEST_FOO*Set the value for the fixture "bar".', - ]) +def test_marker(pytester): + # create a temporary pytest test module + pytester.makepyfile(""" + import pytest + from threading import Lock + class Counter: + def __init__(self): + self._count = 0 + self._lock = Lock() -def test_hello_ini_setting(pytester): - pytester.makeini(""" - [pytest] - HELLO = world - """) + def increase(self): + with self._lock: + self._count += 1 - pytester.makepyfile(""" - import pytest + @pytest.fixture(scope='session') + def counter(): + return Counter() + + @pytest.fixture(scope='session') + def counter2(): + return Counter() + + @pytest.mark.order(1) + def test_thread_increase(counter): + counter.increase() - @pytest.fixture - def hello(request): - return request.config.getini('HELLO') + @pytest.mark.order(1) + @pytest.mark.parallel_threads(5) + def test_thread_increase_five(counter2): + counter2.increase() - def test_hello_world(hello): - assert hello == 'world' + @pytest.mark.order(2) + @pytest.mark.parallel_threads(1) + def test_check_thread_count(counter): + assert counter._count == 10 + + @pytest.mark.order(2) + @pytest.mark.parallel_threads(1) + def test_check_thread_count(counter2): + assert counter2._count == 5 """) - result = pytester.runpytest('-v') + # run pytest with the following cmd args + result = pytester.runpytest( + '--parallel-threads=10', + '-v' + ) # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines([ - '*::test_hello_world PASSED*', + '*::test_check_thread_count PASSED*', ]) # make sure that we get a '0' exit code for the testsuite assert result.ret == 0 + + +def test_help_message(pytester): + result = pytester.runpytest( + '--help', + ) + # fnmatch_lines does an assertion internally + result.stdout.fnmatch_lines([ + 'run-parallel:', + ' --parallel-threads=PARALLEL_THREADS' + # ' Set the number of threads used to execute each test concurrently.', + ]) + diff --git a/tox.ini b/tox.ini index 76eb17e..bfd7484 100644 --- a/tox.ini +++ b/tox.ini @@ -1,10 +1,13 @@ # For more information about tox, see https://tox.readthedocs.io/en/latest/ [tox] -envlist = py38,py39,py310,py311,py312,pypy3,flake8 +envlist = py38,py39,py310,py311,py312,py313,py313t,pypy3,flake8 [testenv] -deps = pytest>=6.2.0 -commands = pytest {posargs:tests} +deps = + pytest>=6.2.0 + pytest-cov + pytest-order +commands = pytest -v --cov-report lcov --cov=pytest_run_parallel {posargs:tests} [testenv:flake8] skip_install = true