Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Reuse Step with Scenario Outline Examples #514

Closed
MummanaSubramanya opened this issue Apr 20, 2022 · 17 comments
Closed

Unable to Reuse Step with Scenario Outline Examples #514

MummanaSubramanya opened this issue Apr 20, 2022 · 17 comments

Comments

@MummanaSubramanya
Copy link

MummanaSubramanya commented Apr 20, 2022

I have a situation where Im unable to reuse a step when using scenario outline. Below is an example

Example.feature

Scenario Outline:
Given I have Students list in database
When Student list is filtered with <last_name>
And Student list is filtered with <first_name>
And Student list is filtered with <student_age>
Then Student count is more than zero

Examples:
| last_name| first_name| student_age|
| xyz_ln | xyz_fn. | 20 |
| xyz_ln | fgh_fn. | 21 |

Stepdef.py

@when("Student list is filtered with <filter_value>")
def filter_student_list(context, filter_value):
printf(f"logic for filtering the list with: {filter_value}" )

Error:

When I execute the the above example. It throws stepdef not found error. Reason is in stepdef.py I have used variable <filter_value> as a generic name. But pytest expects that in feature file we need to use the same variable name. Can you please help me in resolving this?

@elchupanebrej
Copy link

elchupanebrej commented Apr 20, 2022

from pytest_bdd.parsers import parse
from pytest_bdd.steps import when

@when(parse("Student list is filtered with {filter_value}"))
def filter_student_list(filter_value):
    print(f"logic for filtering the list with: {filter_value}")

@elchupanebrej
Copy link

elchupanebrej commented Apr 20, 2022

We could reduce count of such questions after implementation of #467

@MummanaSubramanya
Copy link
Author

MummanaSubramanya commented Apr 21, 2022

I have updated the code as you mentioned and it still fails with same error:

Step definition not found: When "Student list is filtered with <last_name>"

Stepdef.py:

from pytest_bdd.parsers import parse
from pytest_bdd.steps import when

@when(parse("Student list is filtered with {filter_value}"))
def filter_student_list(context, filter_value):
    print(f"logic for filtering the list with: {filter_value}")

Scenario.feature:

Scenario Outline: Sample Example
Given I have Students list in database
When Student list is filtered with <last_name>
And Student list is filtered with <first_name>
And Student list is filtered with <student_age>
Then Student count is more than zero

Examples:
| last_name| first_name| student_age|
| xyz_ln | xyz_fn. | 20 |
| xyz_ln | fgh_fn. | 21 |

@elchupanebrej
Copy link

Could you please provide info about:

  • pytest_bdd version
  • project layout

step definitions must be placed at conftest.py or in test modules.

@ktzoulas
Copy link

ktzoulas commented Apr 23, 2022

Try rename the step definition file to 'test_scenario.py' and import the 'scanerios' method to define the relative location of your feature file or just move the method in your 'conftest.py'.

@MummanaSubramanya
Copy link
Author

MummanaSubramanya commented Apr 27, 2022

I have a very simple setup

under tests folder, I have [ Myfeature.feature, test_feature.py ]
under steps folder, I have. [ conftest.py, given.py, when.py, then.py ]

@MummanaSubramanya
Copy link
Author

MummanaSubramanya commented Apr 27, 2022

Okay I was able to run the test but it just literally prints the params which I set in feature file its not printing the values which I set in examples. Please see the screenshot attached

feature file:

    Scenario Outline: Filter the student list
        Given I have student list
        And student list filtered with <first_name>
        And student list filtered with <last_name>
        Examples:
            | first_name    | last_name |
            | test_fn1      | test_ln1  |
            | test_fn2      | test_ln2  |

stepdefs:

@given(parsers.parse('I have student list'))
def student_list():
    print("Given student list")
    
@given(parsers.parse('student list filtered with {filter_value}'))
def filtering_the_student(filter_value):
    print("filtering with value: " + filter_value)

Screenshot 2022-04-27 at 11 25 44 PM

@ktzoulas
Copy link

Nice job! The parameters of each example are printed in the Test column, for example tests/features/test_scenarios.py::test_scenarios[test_fn1-test_ln1]. Not sure how you can override them, but you could try the https://github.com/wswebcreation/multiple-cucumber-html-reporter repo which accepts the JSON file generated using the --cucumber-json parameter and overrides their values.

@MummanaSubramanya
Copy link
Author

MummanaSubramanya commented Apr 27, 2022

I was expecting that pytest should convert that <first_name> to my example values which is test_fn1 and test_fn2 in stepdef too.

Basically my expected result should look like this:

Given student list
filtering with value: test_fn1
filtering with value: test_ln1

Also, I dnt want to go with third party library as I have some restrictions.

@MummanaSubramanya
Copy link
Author

Can we say its a bug on pytest side?

@elchupanebrej
Copy link

Could you please confirm that you use pytest>4

@MummanaSubramanya
Copy link
Author

Could you please confirm that you use pytest>4

please find the versions:

pytest==6.2.3
pytest-bdd==4.0.2

@elchupanebrej
Copy link

Combining of rendered steps and step parameters has been released in pytest_bdd==5.0 . The main discussion could be found in #293

@MummanaSubramanya
Copy link
Author

Combining of rendered steps and step parameters has been released in pytest_bdd==5.0 . The main discussion could be found in #293

Thank you for replying, I already went through that discussion earlier and seems like its a missing a case what Im expecting here. Also, may be you execute my example scenario in your machine and let me know if its working for you. :)

@elchupanebrej
Copy link

I took the latest master and pytest==7.1.1:

Снимок экрана 2022-04-28 165438

@MummanaSubramanya
Copy link
Author

I took the latest master and pytest==7.1.1:

Снимок экрана 2022-04-28 165438

Will check with latest version and revert back to you

@MummanaSubramanya
Copy link
Author

I confirm that its working in my end. Well let me test few more scenarios and will close if I dnt face any issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants