Skip to content

Commit 222346a

Browse files
committed
Feedback response
1 parent add60f6 commit 222346a

File tree

3 files changed

+70
-54
lines changed

3 files changed

+70
-54
lines changed

README.rst

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -561,15 +561,15 @@ Example:
561561
assert cucumbers["start"] - cucumbers["eat"] == left
562562
563563
564-
Using the datatable Fixture
565-
----------------------------
564+
Step Definitions and Accessing the Datatable
565+
--------------------------------------------
566566

567-
The ``datatable`` fixture allows you to utilise data tables defined in your Gherkin scenarios
567+
The ``datatable`` argument allows you to utilise data tables defined in your Gherkin scenarios
568568
directly within your test functions. This is particularly useful for scenarios that require tabular data as input,
569569
enabling you to manage and manipulate this data conveniently.
570570

571-
The ``datatable`` fixture in pytest-bdd allows you to access the data tables defined in your Gherkin scenarios.
572-
When you use the ``datatable`` fixture in a step definition, it will return the table as a list of lists,
571+
The ``datatable`` argument in pytest-bdd allows you to access the datatables defined in your Gherkin scenarios.
572+
When you use the ``datatable`` argument in a step definition, it will return the table as a list of lists,
573573
where each inner list represents a row from the table.
574574

575575
For example, the Gherkin table:
@@ -579,7 +579,7 @@ For example, the Gherkin table:
579579
| name | email |
580580
| John | [email protected] |
581581
582-
Will be returned by the ``datatable`` fixture as:
582+
Will be returned by the ``datatable`` argument as:
583583

584584
.. code-block:: python
585585
@@ -588,54 +588,70 @@ Will be returned by the ``datatable`` fixture as:
588588
["John", "[email protected]"]
589589
]
590590
591-
.. NOTE:: When using the datatable fixture, it is essential to ensure that the step to which it is applied
591+
.. NOTE:: When using the datatable argument, it is essential to ensure that the step to which it is applied
592592
actually has an associated data table. If the step does not have an associated data table,
593-
attempting to use the datatable fixture will raise an error.
593+
attempting to use the datatable argument will raise an error.
594594
Make sure that your Gherkin steps correctly reference the data table when defined.
595595

596596
Full example:
597597

598598
.. code-block:: gherkin
599599
600-
Feature: User roles and permissions
600+
Feature: Manage user accounts
601601
602-
Scenario: Assigning roles to a user
602+
Scenario: Creating a new user with roles and permissions
603603
Given the following user details:
604-
| name | email |
605-
| John | [email protected] |
604+
| name | email | age |
605+
| John | [email protected] | 30 |
606+
| Alice | [email protected] | 25 |
607+
608+
When each user is assigned the following roles:
609+
| Admin | Full access to the system |
610+
| Contributor | Can add content |
606611
607-
When the user is assigned the following roles:
608-
| role |
609-
| Admin |
610-
| Editor |
612+
And the page is saved
611613
612614
Then the user should have the following permissions:
613-
| permission | allowed |
614-
| view content | true |
615-
| edit content | true |
616-
| delete content| false |
615+
| permission | allowed |
616+
| view dashboard | true |
617+
| edit content | true |
618+
| delete content | false |
617619
618620
.. code-block:: python
619621
620622
from pytest_bdd import given, when, then
621623
622-
@given("the following user details:")
623-
def _(datatable):
624-
assert datatable == [["name", "email"], ["John", "[email protected]"]]
625624
626-
@when("the user is assigned the following roles:")
625+
@given("the following user details:", target_fixture="users")
627626
def _(datatable):
628-
assert datatable == [["role"], ["Admin"], ["Editor"]]
627+
users = []
628+
for row in datatable[1:]:
629+
users.append(row)
630+
631+
print(users)
632+
return users
633+
634+
635+
@when("each user is assigned the following roles:")
636+
def _(datatable, users):
637+
roles = datatable
638+
for user in users:
639+
for role_row in datatable:
640+
assign_role(user, role_row)
641+
642+
643+
@when("the page is saved")
644+
def _():
645+
save_page()
646+
629647
630648
@then("the user should have the following permissions:")
631-
def _(datatable):
632-
expected_permissions = [
633-
["permission", "allowed"],
634-
["view content", "true"],
635-
["edit content", "true"],
636-
["delete content", "false"]
637-
]
638-
assert datatable == expected_permissions
649+
def _(datatable, users):
650+
expected_permissions = []
651+
for row in datatable[1:]:
652+
expected_permissions.append(row)
653+
654+
assert users_have_correct_permissions(users, expected_permissions)
639655
640656
641657
Organizing your scenarios

src/pytest_bdd/gherkin_parser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
import textwrap
66
import typing
7+
from collections.abc import Sequence
78
from dataclasses import dataclass, field
89
from typing import Any
910

@@ -13,8 +14,6 @@
1314
from . import exceptions
1415

1516
if typing.TYPE_CHECKING:
16-
from typing import Sequence
17-
1817
from typing_extensions import Self
1918

2019

tests/datatable/test_datatable.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import textwrap
22
from typing import List
33

4-
from src.pytest_bdd.gherkin_parser import DataTable
54
from src.pytest_bdd.utils import collect_dumped_objects
65

7-
DATA_TABLE_FEATURE = """\
6+
7+
def test_steps_with_datatables(pytester):
8+
pytester.makefile(
9+
".feature",
10+
datatable=textwrap.dedent(
11+
"""\
812
Feature: Manage user accounts
913
1014
Scenario: Creating a new user with roles and permissions
@@ -26,9 +30,11 @@
2630
| edit content | true |
2731
| delete content | false |
2832
"""
29-
30-
31-
DATA_TABLE_STEPS = """\
33+
),
34+
)
35+
pytester.makeconftest(
36+
textwrap.dedent(
37+
"""\
3238
from pytest_bdd import given, when, then
3339
from pytest_bdd.utils import dump_obj
3440
@@ -56,21 +62,19 @@ def _(datatable):
5662
dump_obj(then_datatable)
5763
5864
"""
59-
60-
61-
DATA_TABLE_TEST_FILE = """\
65+
)
66+
)
67+
pytester.makepyfile(
68+
textwrap.dedent(
69+
"""\
6270
from pytest_bdd import scenario
6371
6472
@scenario("datatable.feature", "Creating a new user with roles and permissions")
6573
def test_datatable():
6674
pass
6775
"""
68-
69-
70-
def test_steps_with_datatables(pytester):
71-
pytester.makefile(".feature", datatable=textwrap.dedent(DATA_TABLE_FEATURE))
72-
pytester.makeconftest(textwrap.dedent(DATA_TABLE_STEPS))
73-
pytester.makepyfile(textwrap.dedent(DATA_TABLE_TEST_FILE))
76+
)
77+
)
7478

7579
result = pytester.runpytest("-s")
7680
result.assert_outcomes(passed=1)
@@ -116,19 +120,16 @@ def test_steps_with_missing_datatables(pytester):
116120
textwrap.dedent(
117121
"""\
118122
from pytest_bdd import given, when, then
119-
from pytest_bdd.utils import dump_obj
120123
121124
122125
@given("this step has a data table:")
123126
def _(datatable):
124-
given_datatable = datatable
125-
dump_obj(given_datatable)
127+
print(datatable)
126128
127129
128130
@when("this step has no data table but tries to use the datatable fixture")
129131
def _(datatable):
130-
when_datatable = datatable
131-
dump_obj(when_datatable)
132+
print(datatable)
132133
133134
134135
@then("an error is thrown")

0 commit comments

Comments
 (0)