File tree Expand file tree Collapse file tree 3 files changed +33
-1
lines changed
Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Original file line number Diff line number Diff 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:
Original file line number Diff line number Diff 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
4852def 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 )
Original file line number Diff line number Diff 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+ )
You can’t perform that action at this time.
0 commit comments