Skip to content

Commit 052a35d

Browse files
authored
Merge pull request #18 from Quansight-Labs/add_thread_unsafe_marker
Add thread_unsafe marker as an alias for parallel_threads(1)
2 parents 1ea2c91 + 269aa26 commit 052a35d

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ Features
3737
* Two global CLI flags:
3838
* ``--parallel-threads`` to run a test suite in parallel
3939
* ``--iterations`` to run multiple times in each thread
40-
* Two corresponding markers:
40+
* Three corresponding markers:
4141
* ``pytest.mark.parallel_threads(n)`` to mark a single test to run in
4242
parallel in ``n`` threads
43+
* ``pytest.mark.thread_unsafe`` to mark a single test to run in a single
44+
thread. It is equivalent to using ``pytest.mark.parallel_threads(1)``
4345
* ``pytest.mark.iterations(n)`` to mark a single test to run ``n`` times
4446
in each thread
4547
* And the corresponding fixtures:

src/pytest_run_parallel/plugin.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def pytest_configure(config):
4343
"markers",
4444
"iterations(n): run the given test function `n` times in each thread",
4545
)
46+
config.addinivalue_line(
47+
"markers",
48+
"thread_unsafe: mark the test function as single-threaded",
49+
)
4650

4751

4852
def wrap_function_parallel(fn, n_workers, n_iterations):
@@ -106,6 +110,11 @@ def pytest_itemcollected(item):
106110
if m is not None:
107111
n_iterations = int(m.args[0])
108112

113+
m = item.get_closest_marker("thread_unsafe")
114+
if m is not None:
115+
n_workers = 1
116+
item.add_marker(pytest.mark.parallel_threads(1))
117+
109118
if n_workers > 1 or n_iterations > 1:
110119
original_globals = item.obj.__globals__
111120
item.obj = wrap_function_parallel(item.obj, n_workers, n_iterations)

tests/test_run_parallel.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,24 @@ def test_should_skip():
512512
"*::test_should_skip SKIPPED*",
513513
]
514514
)
515+
516+
517+
def test_thread_unsafe_marker(pytester):
518+
# create a temporary pytest test module
519+
pytester.makepyfile("""
520+
import pytest
521+
522+
@pytest.mark.thread_unsafe
523+
def test_should_run_single(num_parallel_threads):
524+
assert num_parallel_threads == 1
525+
""")
526+
527+
# run pytest with the following cmd args
528+
result = pytester.runpytest("--parallel-threads=10", "-v")
529+
530+
# fnmatch_lines does an assertion internally
531+
result.stdout.fnmatch_lines(
532+
[
533+
"*::test_should_run_single PASSED*",
534+
]
535+
)

0 commit comments

Comments
 (0)