From a412a90e5dcbf7c4dc0cb5b6370e95f0a9113b46 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Thu, 20 Jun 2024 17:00:09 +0200 Subject: [PATCH] test: add test for _get_wrapped_function --- src/_pytest/fixtures.py | 6 ++++-- testing/python/fixtures.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index d8024800e..ea64a17de 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1216,9 +1216,11 @@ class FixtureFunctionDefinition: ) fail(message, pytrace=False) - def _get_wrapped_function(self): + def _get_wrapped_function(self) -> Callable[..., Any]: if self._instance is not None: - return self._fixture_function.__get__(self._instance) + return cast( + Callable[..., Any], self._fixture_function.__get__(self._instance) + ) return self._fixture_function diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 5076c3ed8..b07d4cdfa 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1,4 +1,5 @@ # mypy: allow-untyped-defs +import inspect import os from pathlib import Path import sys @@ -3295,6 +3296,33 @@ class TestFixtureMarker: assert output1 == output2 +class FixtureFunctionDefTestClass: + def __init__(self) -> None: + self.i = 10 + + @pytest.fixture + def fixture_function_def_test_method(self): + return self.i + + +@pytest.fixture +def fixture_function_def_test_func(): + return 9 + + +def test_get_wrapped_func_returns_method(): + obj = FixtureFunctionDefTestClass() + wrapped_function_result = ( + obj.fixture_function_def_test_method._get_wrapped_function() + ) + assert inspect.ismethod(wrapped_function_result) + assert wrapped_function_result() == 10 + + +def test_get_wrapped_func_returns_function(): + assert fixture_function_def_test_func._get_wrapped_function()() == 9 + + class TestRequestScopeAccess: pytestmark = pytest.mark.parametrize( ("scope", "ok", "error"),