-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathplugin.py
143 lines (104 loc) · 3.92 KB
/
plugin.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
"""Pytest plugin entry point. Used for any fixtures needed."""
from __future__ import annotations
from collections.abc import Generator
from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast
import pytest
from typing_extensions import ParamSpec
from . import generation, given, then, when
from .reports import cucumber_json, cucumber_junit, gherkin_terminal_reporter, reporting
from .utils import CONFIG_STACK
if TYPE_CHECKING:
from _pytest.config import Config, PytestPluginManager
from _pytest.config.argparsing import Parser
from _pytest.fixtures import FixtureRequest
from _pytest.nodes import Item
from _pytest.runner import CallInfo
from pluggy._result import _Result
from .parser import Feature, Scenario, Step
P = ParamSpec("P")
T = TypeVar("T")
def pytest_addhooks(pluginmanager: PytestPluginManager) -> None:
"""Register plugin hooks."""
from pytest_bdd import hooks
pluginmanager.add_hookspecs(hooks)
@given("trace")
@when("trace")
@then("trace")
def _() -> None:
"""Enter pytest's pdb trace."""
pytest.set_trace()
@pytest.fixture
def _pytest_bdd_example() -> dict:
"""The current scenario outline parametrization.
This is used internally by pytest_bdd.
If no outline is used, we just return an empty dict to render
the current template without any actual variable.
Otherwise pytest_bdd will add all the context variables in this fixture
from the example definitions in the feature file.
"""
return {}
def pytest_addoption(parser: Parser) -> None:
"""Add pytest-bdd options."""
add_bdd_ini(parser)
cucumber_json.add_options(parser)
cucumber_junit.add_options(parser)
generation.add_options(parser)
gherkin_terminal_reporter.add_options(parser)
def add_bdd_ini(parser: Parser) -> None:
parser.addini("bdd_features_base_dir", "Base features directory.")
@pytest.hookimpl(trylast=True)
def pytest_configure(config: Config) -> None:
"""Configure all subplugins."""
CONFIG_STACK.append(config)
cucumber_json.configure(config)
cucumber_junit.configure(config)
gherkin_terminal_reporter.configure(config)
def pytest_unconfigure(config: Config) -> None:
"""Unconfigure all subplugins."""
if CONFIG_STACK:
CONFIG_STACK.pop()
cucumber_json.unconfigure(config)
cucumber_junit.unconfigure(config)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item: Item, call: CallInfo) -> Generator[None, _Result, None]:
outcome = yield
reporting.runtest_makereport(item, call, outcome.get_result())
@pytest.hookimpl(tryfirst=True)
def pytest_bdd_before_scenario(request: FixtureRequest, feature: Feature, scenario: Scenario) -> None:
reporting.before_scenario(request, feature, scenario)
@pytest.hookimpl(tryfirst=True)
def pytest_bdd_step_error(
request: FixtureRequest,
feature: Feature,
scenario: Scenario,
step: Step,
step_func: Callable[..., Any],
step_func_args: dict,
exception: Exception,
) -> None:
reporting.step_error(request, feature, scenario, step, step_func, step_func_args, exception)
@pytest.hookimpl(tryfirst=True)
def pytest_bdd_before_step(
request: FixtureRequest,
feature: Feature,
scenario: Scenario,
step: Step,
step_func: Callable[..., Any],
) -> None:
reporting.before_step(request, feature, scenario, step, step_func)
@pytest.hookimpl(tryfirst=True)
def pytest_bdd_after_step(
request: FixtureRequest,
feature: Feature,
scenario: Scenario,
step: Step,
step_func: Callable[..., Any],
step_func_args: dict[str, Any],
) -> None:
reporting.after_step(request, feature, scenario, step, step_func, step_func_args)
def pytest_cmdline_main(config: Config) -> int | None:
return generation.cmdline_main(config)
def pytest_bdd_apply_tag(tag: str, function: Callable[P, T]) -> Callable[P, T]:
mark = getattr(pytest.mark, tag)
marked = mark(function)
return cast(Callable[P, T], marked)