Get the actual callable that can be called to obtain the fixture value, dealing with unittest-specific instances and bound methods.
(
fixturedef: FixtureDef[FixtureValue], request: FixtureRequest
)
| 1078 | |
| 1079 | |
| 1080 | def resolve_fixture_function( |
| 1081 | fixturedef: FixtureDef[FixtureValue], request: FixtureRequest |
| 1082 | ) -> "_FixtureFunc[FixtureValue]": |
| 1083 | """Get the actual callable that can be called to obtain the fixture |
| 1084 | value, dealing with unittest-specific instances and bound methods.""" |
| 1085 | fixturefunc = fixturedef.func |
| 1086 | if fixturedef.unittest: |
| 1087 | if request.instance is not None: |
| 1088 | # Bind the unbound method to the TestCase instance. |
| 1089 | fixturefunc = fixturedef.func.__get__(request.instance) # type: ignore[union-attr] |
| 1090 | else: |
| 1091 | # The fixture function needs to be bound to the actual |
| 1092 | # request.instance so that code working with "fixturedef" behaves |
| 1093 | # as expected. |
| 1094 | if request.instance is not None: |
| 1095 | # Handle the case where fixture is defined not in a test class, but some other class |
| 1096 | # (for example a plugin class with a fixture), see #2270. |
| 1097 | if hasattr(fixturefunc, "__self__") and not isinstance( |
| 1098 | request.instance, fixturefunc.__self__.__class__ # type: ignore[union-attr] |
| 1099 | ): |
| 1100 | return fixturefunc |
| 1101 | fixturefunc = getimfunc(fixturedef.func) |
| 1102 | if fixturefunc != fixturedef.func: |
| 1103 | fixturefunc = fixturefunc.__get__(request.instance) # type: ignore[union-attr] |
| 1104 | return fixturefunc |
| 1105 | |
| 1106 | |
| 1107 | def pytest_fixture_setup( |
no test coverage detected