Skip to content

Commit

Permalink
Add plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andfoy committed Aug 13, 2024
1 parent 5b7eb97 commit b9c28c4
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 37 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_run_parallel/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
)


Expand Down
111 changes: 83 additions & 28 deletions tests/test_run_parallel.py
Original file line number Diff line number Diff line change
@@ -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.',
])

9 changes: 6 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b9c28c4

Please sign in to comment.