Skip to content

Commit 6491228

Browse files
authored
Skip analyzing doctests and add num_parallel_threads fixture everywhere (#90)
1 parent a920fcc commit 6491228

2 files changed

Lines changed: 50 additions & 17 deletions

File tree

src/pytest_run_parallel/plugin.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import threading
55
import warnings
66

7+
import _pytest.doctest
78
import _pytest.outcomes
89
import pytest
910

@@ -142,6 +143,27 @@ def closure(*args, **kwargs):
142143

143144
@pytest.hookimpl(trylast=True)
144145
def pytest_itemcollected(item):
146+
if not hasattr(item, "obj"):
147+
if not hasattr(item, "_parallel_custom_item"):
148+
warnings.warn(
149+
f"Encountered pytest item with type {type(item)} with no 'obj' "
150+
"attribute, which is incompatible with pytest-run-parallel. "
151+
f"Tests using {type(item)} will not run in a thread pool.\n"
152+
"The pytest-run-parallel plugin only supports custom collection "
153+
"tree objects that wrap Python functions stored in an attribute "
154+
"named 'obj'.\n"
155+
"Define a '_parallel_custom_item' attribute on the pytest item"
156+
"instance or class to silence this warning.\n"
157+
"If you do not want to use pytest-run-parallel, uninstall it from "
158+
"your environment."
159+
)
160+
item.add_marker(pytest.mark.parallel_threads(1))
161+
return
162+
163+
if isinstance(item, _pytest.doctest.DoctestItem):
164+
item.add_marker(pytest.mark.parallel_threads(1))
165+
return
166+
145167
n_workers = get_num_workers(item.config, item)
146168
fixtures = getattr(item, "fixturenames", ())
147169

@@ -164,23 +186,6 @@ def pytest_itemcollected(item):
164186
else:
165187
item.add_marker(pytest.mark.parallel_threads(1))
166188

167-
if not hasattr(item, "obj"):
168-
if hasattr(item, "_parallel_custom_item"):
169-
return
170-
warnings.warn(
171-
f"Encountered pytest item with type {type(item)} with no 'obj' "
172-
"attribute, which is incompatible with pytest-run-parallel. "
173-
f"Tests using {type(item)} will not run in a thread pool.\n"
174-
"The pytest-run-parallel plugin only supports custom collection "
175-
"tree objects that wrap Python functions stored in an attribute "
176-
"named 'obj'.\n"
177-
"Define a '_parallel_custom_item' attribute on the pytest item"
178-
"instance or class to silence this warning.\n"
179-
"If you do not want to use pytest-run-parallel, uninstall it from "
180-
"your environment."
181-
)
182-
return
183-
184189
skipped_functions = [
185190
x.split(".") for x in item.config.getini("thread_unsafe_functions")
186191
]

tests/test_run_parallel.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,3 +589,31 @@ def test_parallel_2(num_parallel_threads):
589589
"*All tests were run in parallel! 🎉*",
590590
]
591591
)
592+
593+
594+
def test_doctests_marked_thread_unsafe(pytester):
595+
pytester.makepyfile("""
596+
def test_parallel(num_parallel_threads):
597+
assert num_parallel_threads == 10
598+
""")
599+
600+
pytester.makefile(
601+
".txt",
602+
"""
603+
hello this is a doctest
604+
>>> x = 3
605+
>>> x
606+
3
607+
>>> num_parallel_threads = getfixture("num_parallel_threads")
608+
>>> num_parallel_threads
609+
1
610+
""",
611+
)
612+
613+
result = pytester.runpytest("--parallel-threads=10", "-v")
614+
result.stdout.fnmatch_lines(
615+
[
616+
"*::test_parallel PARALLEL PASSED*",
617+
"*::test_doctests_marked_thread_unsafe.txt PASSED*",
618+
]
619+
)

0 commit comments

Comments
 (0)