-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
python: fix instance handling in static and class method tests #12096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Fixed a regression in pytest 8.0.0 where test classes containing ``setup_method`` and tests using ``@staticmethod`` or ``@classmethod`` would crash with ``AttributeError: 'NoneType' object has no attribute 'setup_method'``. | ||
|
|
||
| Now the :attr:`request.instance <pytest.FixtureRequest.instance>` attribute of tests using ``@staticmethod`` and ``@classmethod`` is no longer ``None``, but a fresh instance of the class, like in non-static methods. | ||
| Previously it was ``None``, and all fixtures of such tests would share a single ``self``. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -410,22 +410,37 @@ def test_function_instance(pytester: Pytester) -> None: | |
| items = pytester.getitems( | ||
| """ | ||
| def test_func(): pass | ||
|
|
||
| class TestIt: | ||
| def test_method(self): pass | ||
|
|
||
| @classmethod | ||
| def test_class(cls): pass | ||
|
|
||
| @staticmethod | ||
| def test_static(): pass | ||
| """ | ||
| ) | ||
| assert len(items) == 4 | ||
|
|
||
| assert isinstance(items[0], Function) | ||
| assert items[0].name == "test_func" | ||
| assert items[0].instance is None | ||
|
|
||
| assert isinstance(items[1], Function) | ||
| assert items[1].name == "test_method" | ||
| assert items[1].instance is not None | ||
| assert items[1].instance.__class__.__name__ == "TestIt" | ||
|
|
||
| # Even class and static methods get an instance! | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it is possible for this to break some plugin which is using
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You prompted me to check how plugins use pytest-asyncio however was broken by yesterday's removal of |
||
| # This is the instance used for bound fixture methods, which | ||
| # class/staticmethod tests are perfectly able to request. | ||
| assert isinstance(items[2], Function) | ||
| assert items[2].name == "test_class" | ||
| assert items[2].instance is not None | ||
|
|
||
| assert isinstance(items[3], Function) | ||
| assert items[3].name == "test_static" | ||
| assert items[3].instance is None | ||
| assert items[3].instance is not None | ||
|
|
||
| assert items[1].instance is not items[2].instance is not items[3].instance | ||
Uh oh!
There was an error while loading. Please reload this page.