6
6
from typing import Callable , TypeVar
7
7
8
8
import pytest
9
+ from _pytest .fixtures import FixtureFunctionDefinition
9
10
from typing_extensions import ParamSpec
10
11
11
12
T = TypeVar ("T" )
@@ -16,13 +17,13 @@ def create_fixture(
16
17
name : str ,
17
18
function : Callable [P , T ],
18
19
fixtures : Collection [str ] | None = None ,
19
- ) -> Callable [P , T ]:
20
+ ) -> tuple [ FixtureFunctionDefinition , Callable [P , T ] ]:
20
21
"""Dynamically create a pytest fixture.
21
22
22
23
:param name: Name of the fixture.
23
24
:param function: Function to be called.
24
25
:param fixtures: List of fixtures dependencies, but that will not be passed to ``function``.
25
- :return: The created fixture function.
26
+ :return: The created fixture function and the actual function .
26
27
27
28
Example:
28
29
@@ -41,13 +42,14 @@ def book(name, db):
41
42
if fixtures is None :
42
43
fixtures = []
43
44
44
- @pytest .fixture (name = name )
45
45
@usefixtures (* fixtures )
46
46
@functools .wraps (function )
47
47
def fn (* args : P .args , ** kwargs : P .kwargs ) -> T :
48
48
return function (* args , ** kwargs )
49
49
50
- return fn
50
+ fixture = pytest .fixture (name = name , fixture_function = fn )
51
+
52
+ return fixture , fn
51
53
52
54
53
55
def usefixtures (* fixtures : str ) -> Callable [[Callable [P , T ]], Callable [P , T ]]:
0 commit comments