Skip to content

Commit 386ed90

Browse files
authored
Fix item collection breaking when non Function items are present (#318)
* Fix item collection breaking when non Function items are present * Bump version 3.2.1
1 parent 398c50f commit 386ed90

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Changelog
44
Unreleased
55
----------
66

7+
3.2.1
8+
----------
9+
10+
- Fix regression introduced in 3.2.0 where pytest-bdd would break in presence of test items that are not functions.
11+
712
3.2.0
813
----------
914

pytest_bdd/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
from pytest_bdd.steps import given, when, then
44
from pytest_bdd.scenario import scenario, scenarios
55

6-
__version__ = '3.2.0'
6+
__version__ = '3.2.1'
77

88
__all__ = [given.__name__, when.__name__, then.__name__, scenario.__name__, scenarios.__name__]

pytest_bdd/plugin.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def pytest_collection_modifyitems(session, config, items):
101101
# TODO: Try to only re-sort the items that have __pytest_bdd_counter__, and not the others,
102102
# since there may be other hooks that are executed before this and that want to reorder item as well
103103
def item_key(item):
104-
pytest_bdd_counter = getattr(item.function, '__pytest_bdd_counter__', 0)
105-
return (item.reportinfo()[:2], pytest_bdd_counter)
104+
if isinstance(item, pytest.Function):
105+
declaration_order = getattr(item.function, '__pytest_bdd_counter__', 0)
106+
else:
107+
declaration_order = 0
108+
return (item.reportinfo()[:2], declaration_order)
106109
items.sort(key=item_key)

tests/test_hooks.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,26 @@ def test_foo():
3030

3131
assert result.stdout.lines.count('pytest_pyfunc_call hook') == 1
3232
assert result.stdout.lines.count('pytest_generate_tests hook') == 1
33+
34+
35+
def test_item_collection_does_not_break_on_non_function_items(testdir):
36+
"""Regression test for https://github.com/pytest-dev/pytest-bdd/issues/317"""
37+
testdir.makeconftest("""
38+
import pytest
39+
40+
@pytest.mark.tryfirst
41+
def pytest_collection_modifyitems(session, config, items):
42+
items[:] = [CustomItem(name=item.name, parent=item.parent) for item in items]
43+
44+
class CustomItem(pytest.Item):
45+
def runtest(self):
46+
assert True
47+
""")
48+
49+
testdir.makepyfile("""
50+
def test_convert_me_to_custom_item_and_assert_true():
51+
assert False
52+
""")
53+
54+
result = testdir.runpytest()
55+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)