You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.rst
+51-51Lines changed: 51 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -267,7 +267,7 @@ The code will look like:
267
267
Example code also shows possibility to pass argument converters which may be useful if you need to postprocess step
268
268
arguments after the parser.
269
269
270
-
You can implement your own step parser. It's interface is quite simple. The code can looks like:
270
+
You can implement your own step parser. It's interface is quite simple. The code can look like:
271
271
272
272
.. code-block:: python
273
273
@@ -334,10 +334,10 @@ it will stay untouched. To allow this, special parameter `target_fixture` exists
334
334
Then foo should be "injected foo"
335
335
336
336
337
-
In this exampleexisting fixture `foo` will be overridden by given step `I have injecting given` only for scenario it's
337
+
In this example, the existing fixture `foo` will be overridden by given step `I have injecting given` only for the scenario it's
338
338
used in.
339
339
340
-
Sometimes it is also useful to let `when` and `then` steps to provide a fixture as well.
340
+
Sometimes it is also useful to let `when` and `then` steps provide a fixture as well.
341
341
A common use case is when we have to assert the outcome of an HTTP request:
342
342
343
343
.. code-block:: python
@@ -396,15 +396,15 @@ But in much cleaner and powerful way:
396
396
Lines
397
397
Then the text should be parsed with correct indentation
398
398
399
-
Step is considered as multiline one, if the **next** line(s) after it's first line, is indented relatively
399
+
A step is considered as a multiline one, if the **next** line(s) after it's first line is indented relatively
400
400
to the first line. The step name is then simply extended by adding further lines with newlines.
401
401
In the example above, the Given step name will be:
402
402
403
403
.. code-block:: python
404
404
405
405
'I have a step with:\nSome\nExtra\nLines'
406
406
407
-
You can of course register step using full name (including the newlines), but it seems more practical to use
407
+
You can of course register a step using the full name (including the newlines), but it seems more practical to use
408
408
step arguments and capture lines after first line (or some subset of them) into the argument:
409
409
410
410
.. code-block:: python
@@ -428,7 +428,7 @@ step arguments and capture lines after first line (or some subset of them) into
428
428
Scenarios shortcut
429
429
------------------
430
430
431
-
If you have relatively large set of feature files, it's boring to manually bind scenarios to the tests using the scenario decorator. Of course with the manual approach you get all the power to be able to additionally parametrize the test, give the test function a nice name, document it, etc, but in the majority of the cases you don't need that.
431
+
If you have a relatively large set of feature files, it's boring to manually bind scenarios to the tests using the scenario decorator. Of course with the manual approach you get all the power to be able to additionally parametrize the test, give the test function a nice name, document it, etc, but in the majority of the cases you don't need that.
432
432
Instead, you want to bind all the scenarios found in the ``features`` folder(s) recursively automatically, by using the ``scenarios`` helper.
433
433
434
434
.. code-block:: python
@@ -449,7 +449,7 @@ Note that you can pass multiple paths, and those paths can be either feature fil
But what if you need to manually bind certain scenario, leaving others to be automatically bound?
452
+
But what if you need to manually bind a certain scenario, leaving others to be automatically bound?
453
453
Just write your scenario in a "normal" way, but ensure you do it **before** the call of ``scenarios`` helper.
454
454
455
455
@@ -470,7 +470,7 @@ In the example above, the ``test_something`` scenario binding will be kept manua
470
470
Scenario outlines
471
471
-----------------
472
472
473
-
Scenarios can be parametrized to cover few cases. These are called `Scenario Outlines <https://cucumber.io/docs/gherkin/reference/#scenario-outline>`_ in Gherkin, and the variable templates are written using angular brackets (e.g. ``<var_name>``).
473
+
Scenarios can be parametrized to cover multiple cases. These are called `Scenario Outlines <https://cucumber.io/docs/gherkin/reference/#scenario-outline>`_ in Gherkin, and the variable templates are written using angular brackets (e.g. ``<var_name>``).
474
474
475
475
Example:
476
476
@@ -514,7 +514,7 @@ Example:
514
514
Organizing your scenarios
515
515
-------------------------
516
516
517
-
The more features and scenarios you have, the more important becomes the question about their organization.
517
+
The more features and scenarios you have, the more important the question of their organization becomes.
518
518
The things you can do (and that is also a recommended way):
519
519
520
520
* organize your feature files in the folders by semantic groups:
@@ -534,7 +534,7 @@ The things you can do (and that is also a recommended way):
534
534
│
535
535
└──login.feature
536
536
537
-
This looks fine, but how do you run tests only for certain feature?
537
+
This looks fine, but how do you run tests only for a certain feature?
538
538
As pytest-bdd uses pytest, and bdd scenarios are actually normal tests. But test files
539
539
are separate from the feature files, the mapping is up to developers, so the test files structure can look
540
540
completely different:
@@ -559,7 +559,7 @@ completely different:
559
559
pass
560
560
561
561
562
-
For picking up tests to run we can use
562
+
For picking up tests to run we can use the
563
563
`tests selection <https://pytest.org/en/7.1.x/how-to/usage.html#specifying-which-tests-to-run>`_ technique. The problem is that
564
564
you have to know how your tests are organized, knowing only the feature files organization is not enough.
565
565
Cucumber uses `tags <https://cucumber.io/docs/cucumber/api/#tags>`_ as a way of categorizing your features
@@ -581,8 +581,8 @@ scenario test, so we can use standard test selection:
581
581
582
582
pytest -m "backend and login and successful"
583
583
584
-
The feature and scenario markers are not different from standard pytest markers, and the ``@`` symbol is stripped out automatically to allow test selector expressions. If you want to have bdd-related tags to be distinguishable from the other test markers, use prefix like ``bdd``.
585
-
Note that if you use pytest ``--strict`` option, all bdd tags mentioned in the feature files should be also in the ``markers`` setting of the ``pytest.ini`` config. Also for tags please use names which are python-compatible variable names, eg starts with a non-number, underscore alphanumeric, etc. That way you can safely use tags for tests filtering.
584
+
The feature and scenario markers are not different from standard pytest markers, and the ``@`` symbol is stripped out automatically to allow test selector expressions. If you want to have bdd-related tags to be distinguishable from the other test markers, use a prefix like ``bdd``.
585
+
Note that if you use pytest with the ``--strict`` option, all bdd tags mentioned in the feature files should be also in the ``markers`` setting of the ``pytest.ini`` config. Also for tags please use names which are python-compatible variable names, i.e. start with a non-number, only underscores or alphanumeric characters, etc. That way you can safely use tags for tests filtering.
586
586
587
587
You can customize how tags are converted to pytest marks by implementing the
588
588
``pytest_bdd_apply_tag`` hook and returning ``True`` from it:
@@ -621,7 +621,7 @@ The target PyTest fixture "article" gets the return value and any other step can
621
621
Given I have a beautiful article
622
622
When I publish this article
623
623
624
-
When step is referring the article to publish it.
624
+
The When step is referencing the ``article`` to publish it.
625
625
626
626
.. code-block:: python
627
627
@@ -630,10 +630,10 @@ When step is referring the article to publish it.
630
630
article.publish()
631
631
632
632
633
-
Many other BDD toolkits operate a global context and put the side effects there.
633
+
Many other BDD toolkits operate on a global context and put the side effects there.
634
634
This makes it very difficult to implement the steps, because the dependencies
635
-
appear only as the side-effects in the run-time and not declared in the code.
636
-
The publish article step has to trust that the article is already in the context,
635
+
appear only as the side-effects during run-time and not declared in the code.
636
+
The "publish article" step has to trust that the article is already in the context,
637
637
has to know the name of the attribute it is stored there, the type etc.
638
638
639
639
In pytest-bdd you just declare an argument of the step function that it depends on
@@ -673,7 +673,7 @@ This way side-effects were applied to our article and PyTest makes sure that all
673
673
steps that require the "article" fixture will receive the same object. The value
674
674
of the "published_article" and the "article" fixtures is the same object.
675
675
676
-
Fixtures are evaluated only once within the PyTest scope and their values are cached.
676
+
Fixtures are evaluated **only once** within the PyTest scope and their values are cached.
677
677
678
678
679
679
Backgrounds
@@ -705,24 +705,24 @@ features.
705
705
Then I should see "Your article was published."
706
706
707
707
In this example, all steps from the background will be executed before all the scenario's own given
708
-
steps, adding possibility to prepare some common setup for multiple scenarios in a single feature.
709
-
About background best practices, please read Gherkin's
708
+
steps, adding a possibility to prepare some common setup for multiple scenarios in a single feature.
709
+
About best practices for Background, please read Gherkin's
710
710
`Tips for using Background <https://cucumber.io/docs/gherkin/reference/#tips-for-using-background>`_.
711
711
712
-
.. NOTE:: There is only step "Given" should be used in "Background" section,
713
-
steps "When" and "Then" are prohibited, because their purpose are
714
-
related to actions and consuming outcomes, that is conflict with
715
-
"Background" aim - prepare system for tests or "put the system
712
+
.. NOTE:: Only "Given" steps should be used in "Background" section.
713
+
Steps "When" and "Then" are prohibited, because their purposes are
714
+
related to actions and consuming outcomes; that is in conflict with the
715
+
aim of "Background" - to prepare the system for tests or "put the system
716
716
in a known state" as "Given" does it.
717
-
The statement above is applied for strict Gherkin mode, which is
717
+
The statement above applies to strict Gherkin mode, which is
718
718
enabled by default.
719
719
720
720
721
721
Reusing fixtures
722
722
----------------
723
723
724
-
Sometimes scenarios define new names for the existing fixture that can be
725
-
inherited (reused). For example, if we have pytest fixture:
724
+
Sometimes scenarios define new names for an existing fixture that can be
725
+
inherited (reused). For example, if we have the pytest fixture:
726
726
727
727
728
728
.. code-block:: python
@@ -738,15 +738,15 @@ Then this fixture can be reused with other names using given():
738
738
739
739
.. code-block:: python
740
740
741
-
@given('I have beautiful article')
741
+
@given('I have a beautiful article')
742
742
defi_have_an_article(article):
743
743
"""I have an article."""
744
744
745
745
746
746
Reusing steps
747
747
-------------
748
748
749
-
It is possible to define some common steps in the parent conftest.py and
749
+
It is possible to define some common steps in the parent ``conftest.py`` and
750
750
simply expect them in the child test file.
751
751
752
752
.. code-block:: gherkin
@@ -781,14 +781,14 @@ simply expect them in the child test file.
781
781
deftest_conftest():
782
782
pass
783
783
784
-
There are no definitions of the steps in the test file. They were
784
+
There are no definitions of steps in the test file. They were
785
785
collected from the parent conftest.py.
786
786
787
787
788
788
Default steps
789
789
-------------
790
790
791
-
Here is the list of steps that are implemented inside of the pytest-bdd:
791
+
Here is the list of steps that are implemented inside pytest-bdd:
792
792
793
793
given
794
794
* trace - enters the `pdb` debugger via `pytest.set_trace()`
@@ -801,8 +801,8 @@ then
801
801
Feature file paths
802
802
------------------
803
803
804
-
By default, pytest-bdd will use current module's path as base path for finding feature files, but this behaviour can be changed in the pytest configuration file (i.e. `pytest.ini`, `tox.ini` or `setup.cfg`) by declaring the new base path in the `bdd_features_base_dir` key. The path is interpreted as relative to the `pytest root directory <https://docs.pytest.org/en/latest/reference/customize.html#rootdir>`__.
805
-
You can also override features base path on a per-scenario basis, in order to override the path for specific tests.
804
+
By default, pytest-bdd will use the current module's path as the base path for finding feature files, but this behaviour can be changed in the pytest configuration file (i.e. `pytest.ini`, `tox.ini` or `setup.cfg`) by declaring the new base path in the `bdd_features_base_dir` key. The path is interpreted as relative to the `pytest root directory <https://docs.pytest.org/en/latest/reference/customize.html#rootdir>`__.
805
+
You can also override the features base path on a per-scenario basis, in order to override the path for specific tests.
806
806
807
807
pytest.ini:
808
808
@@ -869,15 +869,15 @@ in the Python docs.
869
869
870
870
Programmatic step generation
871
871
----------------------------
872
-
Sometimes you have step definitions that would be much easier to automate rather than writing manually over and over again.
872
+
Sometimes you have step definitions that would be much easier to automate rather than writing them manually over and over again.
873
873
This is common, for example, when using libraries like `pytest-factoryboy <https://pytest-factoryboy.readthedocs.io/>`_ that automatically creates fixtures.
874
874
Writing step definitions for every model can become a tedious task.
875
875
876
876
For this reason, pytest-bdd provides a way to generate step definitions automatically.
877
877
878
878
The trick is to pass the ``stacklevel`` parameter to the ``given``, ``when``, ``then``, ``step`` decorators. This will instruct them to inject the step fixtures in the appropriate module, rather than just injecting them in the caller frame.
879
879
880
-
Let's look at a concrete example; let's say you have a class ``Wallet`` that has some amount for each currency:
880
+
Let's look at a concrete example; let's say you have a class ``Wallet`` that has some amount of each currency:
881
881
882
882
.. code-block:: python
883
883
@@ -1010,24 +1010,24 @@ Hooks
1010
1010
pytest-bdd exposes several `pytest hooks <https://docs.pytest.org/en/7.1.x/reference/reference.html#hooks>`_
1011
1011
which might be helpful building useful reporting, visualization, etc. on top of it:
1012
1012
1013
-
* pytest_bdd_before_scenario(request, feature, scenario) - Called before scenario is executed
1013
+
* `pytest_bdd_before_scenario(request, feature, scenario)` - Called before scenario is executed
1014
1014
1015
-
* pytest_bdd_after_scenario(request, feature, scenario) - Called after scenario is executed
1015
+
* `pytest_bdd_after_scenario(request, feature, scenario)` - Called after scenario is executed
1016
1016
(even if one of steps has failed)
1017
1017
1018
-
* pytest_bdd_before_step(request, feature, scenario, step, step_func) - Called before step function
1018
+
* `pytest_bdd_before_step(request, feature, scenario, step, step_func)` - Called before step function
1019
1019
is executed and it's arguments evaluated
1020
1020
1021
-
* pytest_bdd_before_step_call(request, feature, scenario, step, step_func, step_func_args) - Called before step
1021
+
* `pytest_bdd_before_step_call(request, feature, scenario, step, step_func, step_func_args)` - Called before step
1022
1022
function is executed with evaluated arguments
1023
1023
1024
-
* pytest_bdd_after_step(request, feature, scenario, step, step_func, step_func_args) - Called after step function
1024
+
* `pytest_bdd_after_step(request, feature, scenario, step, step_func, step_func_args)` - Called after step function
1025
1025
is successfully executed
1026
1026
1027
-
* pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception) - Called when step
1027
+
* `pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception)` - Called when step
1028
1028
function failed to execute
1029
1029
1030
-
* pytest_bdd_step_func_lookup_error(request, feature, scenario, step, exception) - Called when step lookup failed
1030
+
* `pytest_bdd_step_func_lookup_error(request, feature, scenario, step, exception)` - Called when step lookup failed
1031
1031
1032
1032
1033
1033
Browser testing
@@ -1052,7 +1052,7 @@ To have an output in json format:
1052
1052
1053
1053
pytest --cucumberjson=<path to json report>
1054
1054
1055
-
This will output an expanded (meaning scenario outlines will be expanded to several scenarios) cucumber format.
1055
+
This will output an expanded (meaning scenario outlines will be expanded to several scenarios) Cucumber format.
1056
1056
1057
1057
To enable gherkin-formatted output on terminal, use
1058
1058
@@ -1065,8 +1065,8 @@ Test code generation helpers
1065
1065
----------------------------
1066
1066
1067
1067
For newcomers it's sometimes hard to write all needed test code without being frustrated.
1068
-
To simplify their life, simple code generator was implemented. It allows to create fully functional
1069
-
but of course empty tests and step definitions for given a feature file.
1068
+
To simplify their life, a simple code generator was implemented. It allows to create fully functional
1069
+
(but of course empty) tests and step definitions for a given feature file.
1070
1070
It's done as a separate console script provided by pytest-bdd package:
1071
1071
1072
1072
::
@@ -1083,7 +1083,7 @@ It will print the generated code to the standard output so you can easily redire
1083
1083
Advanced code generation
1084
1084
------------------------
1085
1085
1086
-
For more experienced users, there's smart code generation/suggestion feature. It will only generate the
1086
+
For more experienced users, there's a smart code generation/suggestion feature. It will only generate the
1087
1087
test code which is not yet there, checking existing tests and step definitions the same way it's done during the
1088
1088
test execution. The code suggestion tool is called via passing additional pytest arguments:
1089
1089
@@ -1128,7 +1128,7 @@ Migration of your tests from versions 5.x.x
1128
1128
The primary focus of the pytest-bdd is the compatibility with the latest gherkin developments
1129
1129
e.g. multiple scenario outline example tables with tags support etc.
1130
1130
1131
-
In order to provide the best compatibility it is best to support the features described in the official
1131
+
In order to provide the best compatibility, it is best to support the features described in the official
1132
1132
gherkin reference. This means deprecation of some non-standard features that were implemented in pytest-bdd.
1133
1133
1134
1134
@@ -1146,7 +1146,7 @@ The example tables should have horizontal orientation.
1146
1146
Step arguments are no longer fixtures
1147
1147
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1148
1148
Step parsed arguments conflicted with the fixtures. Now they no longer define fixture.
1149
-
If the fixture has to be defined by the step the target_fixture param should be used.
1149
+
If the fixture has to be defined by the step, the target_fixture param should be used.
1150
1150
1151
1151
1152
1152
Variable templates in steps are only parsed for Scenario Outlines
@@ -1204,7 +1204,7 @@ Scenario `example_converters` are removed in favor of the converters provided on
1204
1204
1205
1205
Refuse combining scenario outline and pytest parametrization
0 commit comments