Skip to content

Commit f1211ba

Browse files
Zac-HDclaude
andauthored
Add regression test for #2568 (properties evaluated during collection) (#14745)
Issue #2568 reported that `@property` attributes on test classes were evaluated during collection (via fixture parsing), in addition to any access during the test itself. This no longer reproduces: fixture parsing looks descriptors up on the type and only calls getattr for known fixture definitions, and class/module collection peeks in __dict__ instead of doing getattr. Pin the exact scenario from the issue - a property alongside a test method, evaluated exactly once - so the issue can be closed. Claude-Session: https://claude.ai/code/session_01FvmFSTZ2eh4UAiho9J2u3p Co-authored-by: Claude <noreply@anthropic.com>
1 parent 34b6a4e commit f1211ba

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

‎testing/python/collect.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,31 @@ def oops(self):
304304
result = pytester.runpytest()
305305
assert result.ret == ExitCode.NO_TESTS_COLLECTED
306306

307+
def test_does_not_eval_properties_when_collecting_tests(
308+
self, pytester: Pytester
309+
) -> None:
310+
"""Regression test for #2568.
311+
312+
Properties on a test class must only be evaluated when a test accesses
313+
them, not during collection or fixture parsing.
314+
"""
315+
pytester.makepyfile(
316+
"""\
317+
calls = []
318+
319+
class TestCase:
320+
@property
321+
def prop(self):
322+
calls.append(1)
323+
return len(calls)
324+
325+
def test_prop(self):
326+
assert self.prop == 1
327+
"""
328+
)
329+
result = pytester.runpytest()
330+
result.assert_outcomes(passed=1)
331+
307332
def test_abstract_class_is_not_collected(self, pytester: Pytester) -> None:
308333
"""Regression test for #12275 (non-unittest version)."""
309334
pytester.makepyfile(

0 commit comments

Comments
 (0)