Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/databricks/labs/pytester/fixtures/unwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from typing import TypeVar
from unittest.mock import MagicMock, create_autospec

import pytest

from databricks.labs.lsql.backends import MockBackend
from databricks.sdk import WorkspaceClient
from databricks.sdk.service import iam
Expand All @@ -14,13 +16,27 @@
T = TypeVar('T')


def call_fixture(fixture_fn: Callable[..., T], *args, **kwargs) -> T:
if not hasattr(fixture_fn, '__pytest_wrapped__'):
raise ValueError(f'{fixture_fn} is not a pytest fixture')
wrapped = getattr(fixture_fn, '__pytest_wrapped__')
if not hasattr(wrapped, 'obj'):
raise ValueError(f'{fixture_fn} is not a pytest fixture')
return wrapped.obj(*args, **kwargs)
# Pytest fixtures are not supposed to be called directly, but historically we hack through the countermeasures.
# TODO: Investigate and fix this if possible, to avoid breakage in future pytest versions.
# Potential solution: use `pytest.FixtureRequest` & `request.getfixturevalue()` to access fixtures.
if pytest.version_tuple >= (8, 4):

def call_fixture(fixture_fn: Callable[..., T], *args, **kwargs) -> T:
if not hasattr(fixture_fn, "_get_wrapped_function"):
raise ValueError(f'{fixture_fn} is not a pytest fixture')
accessor = getattr(fixture_fn, "_get_wrapped_function")
wrapped = accessor()
return wrapped(*args, **kwargs)

else:
# Older versions of pytest use a different mechanism to wrap fixtures.
def call_fixture(fixture_fn: Callable[..., T], *args, **kwargs) -> T:
if not hasattr(fixture_fn, '__pytest_wrapped__'):
raise ValueError(f'{fixture_fn} is not a pytest fixture')
wrapped = getattr(fixture_fn, '__pytest_wrapped__')
if not hasattr(wrapped, 'obj'):
raise ValueError(f'{fixture_fn} is not a pytest fixture')
return wrapped.obj(*args, **kwargs)


class CallContext:
Expand Down