How to pass different values for same parameter in Example using pytest bdd for scenario statement

1.7k Views Asked by At

Here is my feature file

Scenario Outline: Test different value for same parameter
 Examples:
 | app     | app1     |
 | instagram| facebook |

Given <app> is installed on my device
And <app1> is installed on my device
@given("<app> is installed on my device")
def app_installation(app):
    install_app(app)

As of now, i cannot use app2 value with same step and i have to duplicate app_installation with app1 parameter

Is there a way that i can use any parameter in Example which value can be mapped to app

1

There are 1 best solutions below

0
On

Since pytest-bdd is reading your examples table and creates a fixture for every table entry you can load the table data dynamically. This can be done by passing the header name of the examples table column instead of the value and then using the pytest request fixture to retrieve the actual table value.

Scenario Outline: Test different value for same parameter
 Examples:
 | app     | app1     |
 | instagram| facebook |

Given app is installed on my device
And app1 is installed on my device
# IMPORTANT:
#    The step is parsed by `parsers.parse` and not by using
#    `<>`, therefore in the `app` variable will be the column
#    name (app, app1, app2, ...) of the examples table instead
#    of the actual value (instagram, facebook, ...).
@given(parsers.parse("Given {app} is installed on my device"))
def app_installed(request, app):
    app_to_install = request.getfixturevalue(app)
    # Install app ...

Note: You can use <app> in your feature file as well, but then you have to remove the angle brackets in the @given step before calling request.getfixturevalue(app)