Skip to content

Commit bade62a

Browse files
committed
Remove accidental duplication of code
Remove unnecessary safeguards (as the parser already does these!) Add test to increase coverage
1 parent 430501b commit bade62a

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

src/pytest_bdd/gherkin_parser.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ def from_dict(cls, data: dict[str, Any]) -> Self:
130130

131131
def transpose(self) -> DataTable:
132132
# Transpose the cells, turning rows into columns
133-
if not self.rows:
134-
return self # Return itself if there are no rows to transpose
135133

136134
# Get the list of lists of cell values (i.e., extract all row cells)
137135
cells_matrix = [row.cells for row in self.rows]
@@ -146,10 +144,7 @@ def transpose(self) -> DataTable:
146144
# Create a new list for each transposed column
147145
transposed_column = []
148146
for row in cells_matrix:
149-
if col_idx < len(row): # Ensure we don't go out of bounds
150-
transposed_column.append(row[col_idx])
151-
else:
152-
transposed_column.append(Cell(location=Location(0, 0), value="")) # Empty cell
147+
transposed_column.append(row[col_idx])
153148

154149
# Create a new row from the transposed column
155150
transposed_row = Row(id=str(col_idx), location=self.location, cells=transposed_column)

src/pytest_bdd/scenario.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ def get_fixture_path(fixture_def: FixtureDef) -> list[str]:
152152

153153

154154
def get_step_function(request: FixtureRequest, step: Step) -> StepFunctionContext | None:
155-
"""Get the step function (context) for the given step, considering data_table."""
155+
"""Get the step function (context) for the given step.
156+
We first figure out what's the step fixture name that we have to inject.
157+
Then we let `patch_argumented_step_functions` find out what step definition fixtures can parse the current step,
158+
and it will inject them for the step fixture name.
159+
Finally, we let request.getfixturevalue(...) fetch the step definition fixture.
160+
Data tables are considered if the step has them defined.
161+
"""
156162
__tracebackhide__ = True
157163
bdd_name = get_step_fixture_name(step=step)
158164

@@ -221,19 +227,13 @@ def _execute_step_function(
221227

222228
request.config.hook.pytest_bdd_after_step(**kw)
223229

224-
if context.target_fixture is not None:
225-
inject_fixture(request, context.target_fixture, return_value)
226-
227-
request.config.hook.pytest_bdd_after_step(**kw)
228-
229230

230231
def _execute_scenario(feature: Feature, scenario: Scenario, request: FixtureRequest) -> None:
231232
"""Execute the scenario.
232233
233234
:param feature: Feature.
234235
:param scenario: Scenario.
235236
:param request: request.
236-
:param encoding: Encoding.
237237
"""
238238
__tracebackhide__ = True
239239
request.config.hook.pytest_bdd_before_scenario(request=request, feature=feature, scenario=scenario)

tests/datatable/test_datatable.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
DATA_TABLE_STEPS = """\
30-
from pytest_bdd import given, when, then, parsers
30+
from pytest_bdd import given, when, then
3131
from pytest_bdd.utils import dump_obj
3232
3333
@@ -56,7 +56,7 @@ def _(data_table):
5656
from pytest_bdd import scenario
5757
5858
@scenario("data_table.feature", "Creating a new user with roles and permissions")
59-
def test_outline():
59+
def test_data_table():
6060
pass
6161
"""
6262

@@ -165,7 +165,7 @@ def test_steps_with_missing_data_tables(pytester):
165165
pytester.makeconftest(
166166
textwrap.dedent(
167167
"""\
168-
from pytest_bdd import given, when, then, parsers
168+
from pytest_bdd import given, when, then
169169
from pytest_bdd.utils import dump_obj
170170
171171
@@ -196,11 +196,56 @@ def _(data_table):
196196
from pytest_bdd import scenario
197197
198198
@scenario("missing_data_table.feature", "Data table is missing for a step")
199-
def test_outline():
199+
def test_data_table():
200200
pass
201201
"""
202202
)
203203
)
204204
result = pytester.runpytest("-s")
205205
result.assert_outcomes(failed=1)
206206
result.stdout.fnmatch_lines(["*fixture 'data_table' not found*"])
207+
208+
209+
def test_steps_with_data_tables_too_short_for_to_dict(pytester):
210+
pytester.makefile(
211+
".feature",
212+
too_short_data_table=textwrap.dedent(
213+
"""\
214+
Feature: Short data table
215+
216+
Scenario: Data table too short for transforming to dict
217+
Given this step has a data table:
218+
| name | email | age |
219+
220+
"""
221+
),
222+
)
223+
pytester.makeconftest(
224+
textwrap.dedent(
225+
"""\
226+
from pytest_bdd import given, when, then
227+
228+
229+
@given("this step has a data table:")
230+
def _(data_table):
231+
data_table.to_dict()
232+
233+
"""
234+
)
235+
)
236+
237+
pytester.makepyfile(
238+
textwrap.dedent(
239+
"""\
240+
from pytest_bdd.utils import dump_obj
241+
from pytest_bdd import scenario
242+
243+
@scenario("too_short_data_table.feature", "Data table too short for transforming to dict")
244+
def test_data_table():
245+
pass
246+
"""
247+
)
248+
)
249+
result = pytester.runpytest("-s")
250+
result.assert_outcomes(failed=1)
251+
result.stdout.fnmatch_lines(["*ValueError: DataTable needs at least two rows: one for headers and one for values*"])

0 commit comments

Comments
 (0)