-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathsteps.py
255 lines (191 loc) · 7.64 KB
/
steps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""Step decorators.
Example:
@given("I have an article", target_fixture="article")
def _(author):
return create_test_article(author=author)
@when("I go to the article page")
def _(browser, article):
browser.visit(urljoin(browser.url, "/articles/{0}/".format(article.id)))
@then("I should not see the error message")
def _(browser):
with pytest.raises(ElementDoesNotExist):
browser.find_by_css(".message.error").first
Multiple names for the steps:
@given("I have an article")
@given("there is an article")
def _(author):
return create_test_article(author=author)
Reusing existing fixtures for a different step name:
@given("I have a beautiful article")
def _(article):
pass
"""
from __future__ import annotations
import enum
from dataclasses import dataclass, field
from itertools import count
from typing import Any, Callable, Iterable, TypeVar
import pytest
from _pytest.fixtures import FixtureDef, FixtureRequest
from typing_extensions import Literal
from .parser import Step
from .parsers import StepParser, get_parser
from .types import GIVEN, THEN, WHEN
from .utils import get_caller_module_locals
TCallable = TypeVar("TCallable", bound=Callable[..., Any])
@enum.unique
class StepNamePrefix(enum.Enum):
step_def = "pytestbdd_stepdef"
step_impl = "pytestbdd_stepimpl"
@dataclass
class StepFunctionContext:
type: Literal["given", "when", "then"] | None
step_func: Callable[..., Any]
parser: StepParser
converters: dict[str, Callable[..., Any]] = field(default_factory=dict)
target_fixture: str | None = None
target_exception: str | None = None
def get_step_fixture_name(step: Step) -> str:
"""Get step fixture name"""
return f"{StepNamePrefix.step_impl.value}_{step.type}_{step.name}"
def given(
name: str | StepParser,
converters: dict[str, Callable] | None = None,
target_fixture: str | None = None,
stacklevel: int = 1,
) -> Callable:
"""Given step decorator.
:param name: Step name or a parser object.
:param converters: Optional `dict` of the argument or parameter converters in form
{<param_name>: <converter function>}.
:param target_fixture: Target fixture name to replace by steps definition function.
:param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
:return: Decorator function for the step.
"""
return step(name, GIVEN, converters=converters, target_fixture=target_fixture, stacklevel=stacklevel)
def when(
name: str | StepParser,
converters: dict[str, Callable] | None = None,
target_fixture: str | None = None,
target_exception: str | None = None,
stacklevel: int = 1,
) -> Callable:
"""When step decorator.
:param name: Step name or a parser object.
:param converters: Optional `dict` of the argument or parameter converters in form
{<param_name>: <converter function>}.
:param target_fixture: Target fixture name to replace by steps definition function.
:param target_exception: Target exception name to receive Exception object
:param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
:return: Decorator function for the step.
"""
return step(
name,
WHEN,
converters=converters,
target_fixture=target_fixture,
target_exception=target_exception,
stacklevel=stacklevel,
)
def then(
name: str | StepParser,
converters: dict[str, Callable] | None = None,
target_fixture: str | None = None,
stacklevel: int = 1,
) -> Callable:
"""Then step decorator.
:param name: Step name or a parser object.
:param converters: Optional `dict` of the argument or parameter converters in form
{<param_name>: <converter function>}.
:param target_fixture: Target fixture name to replace by steps definition function.
:param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
:return: Decorator function for the step.
"""
return step(name, THEN, converters=converters, target_fixture=target_fixture, stacklevel=stacklevel)
def step(
name: str | StepParser,
type_: Literal["given", "when", "then"] | None = None,
converters: dict[str, Callable] | None = None,
target_fixture: str | None = None,
target_exception: str | None = None,
stacklevel: int = 1,
) -> Callable[[TCallable], TCallable]:
"""Generic step decorator.
:param name: Step name as in the feature file.
:param type_: Step type ("given", "when" or "then"). If None, this step will work for all the types.
:param converters: Optional step arguments converters mapping.
:param target_fixture: Optional fixture name to replace by step definition.
:param target_exception: Optional target exception name
:param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
:return: Decorator function for the step.
Example:
>>> @step("there is an wallet", target_fixture="wallet")
>>> def _() -> dict[str, int]:
>>> return {"eur": 0, "usd": 0}
"""
if converters is None:
converters = {}
def decorator(func: TCallable) -> TCallable:
parser = get_parser(name)
context = StepFunctionContext(
type=type_,
step_func=func,
parser=parser,
converters=converters,
target_fixture=target_fixture,
target_exception=target_exception,
)
def step_function_marker() -> StepFunctionContext:
return context
step_function_marker._pytest_bdd_step_context = context
caller_locals = get_caller_module_locals(stacklevel=stacklevel)
fixture_step_name = find_unique_name(
f"{StepNamePrefix.step_def.value}_{type_ or '*'}_{parser.name}", seen=caller_locals.keys()
)
caller_locals[fixture_step_name] = pytest.fixture(name=fixture_step_name)(step_function_marker)
return func
return decorator
def find_unique_name(name: str, seen: Iterable[str]) -> str:
"""Find unique name among a set of strings.
New names are generated by appending an increasing number at the end of the name.
Example:
>>> find_unique_name("foo", ["foo", "foo_1"])
'foo_2'
"""
seen = set(seen)
if name not in seen:
return name
for i in count(1):
new_name = f"{name}_{i}"
if new_name not in seen:
return new_name
def inject_fixture(request: FixtureRequest, arg: str, value: Any) -> None:
"""Inject fixture into pytest fixture request.
:param request: pytest fixture request
:param arg: argument name
:param value: argument value
"""
fd = FixtureDef(
fixturemanager=request._fixturemanager,
baseid=None,
argname=arg,
func=lambda: value,
scope="function",
params=None,
)
fd.cached_result = (value, 0, None)
old_fd = request._fixture_defs.get(arg)
add_fixturename = arg not in request.fixturenames
def fin() -> None:
request._fixturemanager._arg2fixturedefs[arg].remove(fd)
if old_fd is not None:
request._fixture_defs[arg] = old_fd
if add_fixturename:
request._pyfuncitem._fixtureinfo.names_closure.remove(arg)
request.addfinalizer(fin)
# inject fixture definition
request._fixturemanager._arg2fixturedefs.setdefault(arg, []).append(fd)
# inject fixture value in request cache
request._fixture_defs[arg] = fd
if add_fixturename:
request._pyfuncitem._fixtureinfo.names_closure.append(arg)