1
1
"""pytest-bdd missing test code generation."""
2
+ from __future__ import annotations
2
3
3
4
import itertools
4
5
import os .path
6
+ from typing import TYPE_CHECKING , cast
5
7
6
8
import py
7
9
from mako .lookup import TemplateLookup
11
13
from .steps import get_step_fixture_name
12
14
from .types import STEP_TYPES
13
15
16
+ if TYPE_CHECKING :
17
+ from typing import Any , Sequence
18
+
19
+ from _pytest .config import Config
20
+ from _pytest .config .argparsing import Parser
21
+ from _pytest .fixtures import FixtureDef , FixtureManager
22
+ from _pytest .main import Session
23
+ from _pytest .python import Function
24
+
25
+ from .parser import Feature , ScenarioTemplate , Step
26
+
14
27
template_lookup = TemplateLookup (directories = [os .path .join (os .path .dirname (__file__ ), "templates" )])
15
28
16
29
17
- def add_options (parser ) :
30
+ def add_options (parser : Parser ) -> None :
18
31
"""Add pytest-bdd options."""
19
32
group = parser .getgroup ("bdd" , "Generation" )
20
33
@@ -35,34 +48,36 @@ def add_options(parser):
35
48
)
36
49
37
50
38
- def cmdline_main (config ) :
51
+ def cmdline_main (config : Config ) -> int | None :
39
52
"""Check config option to show missing code."""
40
53
if config .option .generate_missing :
41
54
return show_missing_code (config )
55
+ return None # Make mypy happy
42
56
43
57
44
- def generate_code (features , scenarios , steps ) :
58
+ def generate_code (features : list [ Feature ] , scenarios : list [ ScenarioTemplate ] , steps : list [ Step ]) -> str :
45
59
"""Generate test code for the given filenames."""
46
60
grouped_steps = group_steps (steps )
47
61
template = template_lookup .get_template ("test.py.mak" )
48
- return template .render (
62
+ code = template .render (
49
63
features = features ,
50
64
scenarios = scenarios ,
51
65
steps = grouped_steps ,
52
66
make_python_name = make_python_name ,
53
67
make_python_docstring = make_python_docstring ,
54
68
make_string_literal = make_string_literal ,
55
69
)
70
+ return cast (str , code )
56
71
57
72
58
- def show_missing_code (config ) :
73
+ def show_missing_code (config : Config ) -> int :
59
74
"""Wrap pytest session to show missing code."""
60
75
from _pytest .main import wrap_session
61
76
62
77
return wrap_session (config , _show_missing_code_main )
63
78
64
79
65
- def print_missing_code (scenarios , steps ) :
80
+ def print_missing_code (scenarios : list [ ScenarioTemplate ] , steps : list [ Step ]) -> None :
66
81
"""Print missing code with TerminalWriter."""
67
82
tw = py .io .TerminalWriter ()
68
83
scenario = step = None
@@ -108,14 +123,10 @@ def print_missing_code(scenarios, steps):
108
123
tw .write (code )
109
124
110
125
111
- def _find_step_fixturedef (fixturemanager , item , name , type_ ):
112
- """Find step fixturedef.
113
-
114
- :param request: PyTest Item object.
115
- :param step: `Step`.
116
-
117
- :return: Step function.
118
- """
126
+ def _find_step_fixturedef (
127
+ fixturemanager : FixtureManager , item : Function , name : str , type_ : str
128
+ ) -> Sequence [FixtureDef [Any ]] | None :
129
+ """Find step fixturedef."""
119
130
step_fixture_name = get_step_fixture_name (name , type_ )
120
131
fixturedefs = fixturemanager .getfixturedefs (step_fixture_name , item .nodeid )
121
132
if fixturedefs is not None :
@@ -127,7 +138,7 @@ def _find_step_fixturedef(fixturemanager, item, name, type_):
127
138
return None
128
139
129
140
130
- def parse_feature_files (paths , ** kwargs ) :
141
+ def parse_feature_files (paths : list [ str ] , ** kwargs : Any ) -> tuple [ list [ Feature ], list [ ScenarioTemplate ], list [ Step ]] :
131
142
"""Parse feature files of given paths.
132
143
133
144
:param paths: `list` of paths (file or dirs)
@@ -146,7 +157,7 @@ def parse_feature_files(paths, **kwargs):
146
157
return features , scenarios , steps
147
158
148
159
149
- def group_steps (steps ) :
160
+ def group_steps (steps : list [ Step ]) -> list [ Step ] :
150
161
"""Group steps by type."""
151
162
steps = sorted (steps , key = lambda step : step .type )
152
163
seen_steps = set ()
@@ -161,7 +172,7 @@ def group_steps(steps):
161
172
return grouped_steps
162
173
163
174
164
- def _show_missing_code_main (config , session ) :
175
+ def _show_missing_code_main (config : Config , session : Session ) -> None :
165
176
"""Preparing fixture duplicates for output."""
166
177
tw = py .io .TerminalWriter ()
167
178
session .perform_collect ()
0 commit comments