|
5 | 5 | from typing import TypeVar |
6 | 6 | from unittest.mock import MagicMock, create_autospec |
7 | 7 |
|
| 8 | +import pytest |
| 9 | + |
8 | 10 | from databricks.labs.lsql.backends import MockBackend |
9 | 11 | from databricks.sdk import WorkspaceClient |
10 | 12 | from databricks.sdk.service import iam |
|
14 | 16 | T = TypeVar('T') |
15 | 17 |
|
16 | 18 |
|
17 | | -def call_fixture(fixture_fn: Callable[..., T], *args, **kwargs) -> T: |
18 | | - if not hasattr(fixture_fn, '__pytest_wrapped__'): |
19 | | - raise ValueError(f'{fixture_fn} is not a pytest fixture') |
20 | | - wrapped = getattr(fixture_fn, '__pytest_wrapped__') |
21 | | - if not hasattr(wrapped, 'obj'): |
22 | | - raise ValueError(f'{fixture_fn} is not a pytest fixture') |
23 | | - return wrapped.obj(*args, **kwargs) |
| 19 | +# Pytest fixtures are not supposed to be called directly, but historically we hack through the countermeasures. |
| 20 | +# TODO: Investigate and fix this if possible, to avoid breakage in future pytest versions. |
| 21 | +# Potential solution: use `pytest.FixtureRequest` & `request.getfixturevalue()` to access fixtures. |
| 22 | +if pytest.version_tuple >= (8, 4): |
| 23 | + |
| 24 | + def call_fixture(fixture_fn: Callable[..., T], *args, **kwargs) -> T: |
| 25 | + if not hasattr(fixture_fn, "_get_wrapped_function"): |
| 26 | + raise ValueError(f'{fixture_fn} is not a pytest fixture') |
| 27 | + accessor = getattr(fixture_fn, "_get_wrapped_function") |
| 28 | + wrapped = accessor() |
| 29 | + return wrapped(*args, **kwargs) |
| 30 | + |
| 31 | +else: |
| 32 | + # Older versions of pytest use a different mechanism to wrap fixtures. |
| 33 | + def call_fixture(fixture_fn: Callable[..., T], *args, **kwargs) -> T: |
| 34 | + if not hasattr(fixture_fn, '__pytest_wrapped__'): |
| 35 | + raise ValueError(f'{fixture_fn} is not a pytest fixture') |
| 36 | + wrapped = getattr(fixture_fn, '__pytest_wrapped__') |
| 37 | + if not hasattr(wrapped, 'obj'): |
| 38 | + raise ValueError(f'{fixture_fn} is not a pytest fixture') |
| 39 | + return wrapped.obj(*args, **kwargs) |
24 | 40 |
|
25 | 41 |
|
26 | 42 | class CallContext: |
|
0 commit comments