Skip to content

Commit 0138d0b

Browse files
committed
Don't execute class properties when collecting Python test classes
Previously, properties such as in the added test case were triggered during collection, possibly executing unintended code. Let's skip them instead. We should probably skip all custom descriptors, however I am not sure how to do this cleanly, so for now just skip properties. Fixes pytest-dev#2234.
1 parent ccf9877 commit 0138d0b

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Pieter Mulder
117117
Piotr Banaszkiewicz
118118
Punyashloka Biswal
119119
Quentin Pradet
120+
Ran Benita
120121
Ralf Schmitt
121122
Raphael Pierzina
122123
Raquel Alegre

CHANGELOG.rst

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
*
55

6+
* Don't trigger execution class properties in Python test classes during collection (`#2234`_).
7+
68
* Replace ``raise StopIteration`` usages in the code by simple ``returns`` to finish generators, in accordance to `PEP-479`_ (`#2160`_).
79
Thanks `@tgoodlet`_ for the report and `@nicoddemus`_ for the PR.
810

@@ -16,6 +18,8 @@
1618

1719
*
1820

21+
.. _#2234: https://github.com/pytest-dev/pytest/issues/2234
22+
1923
.. _#2160: https://github.com/pytest-dev/pytest/issues/2160
2024

2125
.. _PEP-479: https://www.python.org/dev/peps/pep-0479/

_pytest/fixtures.py

+3
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,9 @@ def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
10681068
self._holderobjseen.add(holderobj)
10691069
autousenames = []
10701070
for name in dir(holderobj):
1071+
# Skip properties before the following getattr executes them.
1072+
if isinstance(getattr(type(holderobj), name, None), property):
1073+
continue
10711074
obj = getattr(holderobj, name, None)
10721075
# fixture functions have a pytest_funcarg__ prefix (pre-2.3 style)
10731076
# or are "@pytest.fixture" marked

testing/python/collect.py

+10
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ def test_issue1579_namedtuple(self, testdir):
166166
"because it has a __new__ constructor*"
167167
)
168168

169+
def test_issue2234_property(self, testdir):
170+
testdir.makepyfile("""
171+
class TestCase(object):
172+
@property
173+
def prop(self):
174+
raise NotImplementedError()
175+
""")
176+
result = testdir.runpytest()
177+
assert result.ret == EXIT_NOTESTSCOLLECTED
178+
169179

170180
class TestGenerator:
171181
def test_generative_functions(self, testdir):

0 commit comments

Comments
 (0)