Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/pytest_run_parallel/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,13 @@ def pytest_collection_finish(self, session):
self._handle_collected_item(item)

def _handle_collected_item(self, item):
if not hasattr(item, "obj"):
if isinstance(item, _pytest.doctest.DoctestItem):
self._mark_test_thread_unsafe(
item, "is a doctest (pytest-run-parallel does not support doctests)"
)
return

if getattr(item, "obj", None) is None:
if not hasattr(item, "_parallel_custom_item"):
warnings.warn(
f"Encountered pytest item with type {type(item)} with no 'obj' "
Expand All @@ -273,12 +279,6 @@ def _handle_collected_item(self, item):
)
return

if isinstance(item, _pytest.doctest.DoctestItem):
self._mark_test_thread_unsafe(
item, "is a doctest (pytest-run-parallel does not support doctests)"
)
return

n_workers, parallel_threads_marker_used = get_num_workers(item)
if n_workers < 0:
raise ValueError("parallel-threads cannot be negative")
Expand Down
48 changes: 47 additions & 1 deletion tests/test_run_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,10 @@ def test_should_skip():
)


def test_incompatible_test_item(pytester):
def test_incompatible_test_item_no_obj_item(pytester):
"""Doctests and other doctest-like custom pytest.Item subclasses are incompatible
without an obj attribute on the Item. This shouldn't crash pytest-run-parallel
and the test should only run on one thread"""
pytester.makeconftest("""
import inspect
import pytest
Expand Down Expand Up @@ -615,7 +618,50 @@ def test_incompatible_item():
assert result.parseoutcomes()["warnings"] == 1


def test_incompatible_test_item_obj_is_none(pytester):
"""Doctests and other doctest-like custom pytest.Item subclasses are incompatible
if the obj attribute is None. This shouldn't crash pytest-run-parallel
and the test should only run on one thread"""
pytester.makeconftest("""
import inspect
import pytest

class CustomItem(pytest.Item):
def __init__(self, name, parent=None, config=None, session=None, nodeid=None, function=None, **kwargs):
super().__init__(name, parent, config, session, nodeid, **kwargs)
self.function = function
self.obj = None

def runtest(self):
self.function()

@pytest.hookimpl(wrapper=True, trylast=True)
def pytest_pycollect_makeitem(collector, name: str, obj: object):
result = yield
if not inspect.isfunction(obj):
return result
return CustomItem.from_parent(name=name, parent=collector, function=obj)
""")

pytester.makepyfile("""
import pytest

def test_incompatible_item():
assert True
""")
result = pytester.runpytest("--parallel-threads=10", "-v", "-W", "default")
result.stdout.fnmatch_lines(
[
"*::test_incompatible_item PASSED*",
]
)
assert result.parseoutcomes()["warnings"] == 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be useful to check the content of the warning.

result.stdout.fnmatch_lines(["*Encountered pytest item * with no 'obj' attribute*"])


def test_known_incompatible_test_item_doesnt_warn(pytester):
"""If the Item has a _parallel_custom_item attribute, pytest-run-parallel
shouldn't warn about the incompatibility"""
pytester.makeconftest("""
import inspect
import pytest
Expand Down
Loading