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
Is there a way to execute pytest-bdd scenario only if previous scenario passed and skip execution if previous scenario failed.
For example: I have 2 pytest-bdd scenarios (Scn A and Scn B) with same marker as @Set1 . I execute tests using this tag
I want to stop execution of second scenario Scn B if Scn A failed.
The text was updated successfully, but these errors were encountered:
Please check pytest plugin https://pytest-dependency.readthedocs.io/en/stable/usage.html
In your case, you have to inject two tests by "scenario" and mark result tests as dependent. This has to work, but in case if wouldn't - please provide info
This Stack Overflow post contains a better solution for including the pytest plugin pytest-dependency into pytest-bdd. Although this might work for most implementations, the pytest-dependency plugin relies on passing arguments as kwargs, and one containing a list.
The Stack Overflow answer provided above makes use of ast to parse the marker. I made some updates to resolve issues with pytest-dependency integration:
Hope this helps others! It took me a while to figure it out.
from typing import Callable, cast
import pytest
import ast
def pytest_bdd_apply_tag(tag: str, function) -> Callable:
tree = ast.parse(tag)
body = tree.body[0].value
if isinstance(body, ast.Call):
arg = {}
name = body.func.id
# Obtain all keywords ('name', 'depends') applied to the marker
for kw in body.keywords:
if isinstance(kw.value, ast.List):
kwvlist = []
# Obtain the list of keyword values (EX: depends=['TC-1', 'TC-2'] would give ['TC-1', 'TC-2'] as result)
# Store in a python `list`
for kwvelts in kw.value.elts:
kwvlist.append(kwvelts.value)
else:
# Value is an ast.Constant, so we get it's value (EX: name='TC-3')
kwvlist = kw.value.value
# Create the `dict` we will use to inject `kwargs` into the `dependency` marker.
arg.update({kw.arg: kwvlist})
if len(body.args) > 0:
arg = body.args[0].value
mark = getattr(pytest.mark, name).with_args(arg)
else:
# Expand the `dict` so it provides kwargs and not `args={}`
mark = getattr(pytest.mark, name).with_args(**arg)
else:
mark = getattr(pytest.mark, tag)
marked = mark(function)
return cast(Callable, marked)
Is there a way to execute pytest-bdd scenario only if previous scenario passed and skip execution if previous scenario failed.
For example: I have 2 pytest-bdd scenarios (Scn A and Scn B) with same marker as @Set1 . I execute tests using this tag
I want to stop execution of second scenario Scn B if Scn A failed.
The text was updated successfully, but these errors were encountered: