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

Pytest-bdd scenario- how to stop execution based on previous scenario failure #460

Open
idea1002 opened this issue Nov 3, 2021 · 2 comments

Comments

@idea1002
Copy link

idea1002 commented Nov 3, 2021

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.

@elchupanebrej
Copy link

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

@rjauregui1
Copy link

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)

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