pytest-bdd icon indicating copy to clipboard operation
pytest-bdd copied to clipboard

pytest-bdd callbacks not called

Open PiotrNestor opened this issue 9 years ago • 3 comments
trafficstars

How to get the pytest-bdd callbacks I tried the following implementation(,but the callback is NOT called):

import pytest

@pytest.mark.tryfirst
def pytest_bdd_after_scenario(request, feature, scenario):
    print("After scenario")

The complete example: Execute with: PYTHONPATH=. python outline.py

outline.feature

Feature: Example

Scenario Outline: Outlined given, when, then
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers

Examples:
| start | eat | left |
|  12   |  5  |  7   |
|  12   |  6  |  6   |

outline.py

import pytest
from pytest_bdd import given, when, then, scenario
from steps import *

@scenario(
    'outline.feature',
    'Outlined given, when, then',
    example_converters=dict(start=int, eat=float, left=str)
)
def test_outlined():
    pass

if __name__ == '__main__':
    pytest.main(args=['-q', '-s', __file__])

steps.py

import pytest
from pytest_bdd import given, when, then, scenario

@pytest.mark.tryfirst
def pytest_bdd_after_scenario(request, feature, scenario):
    print("After scenario")


@given('there are <start> cucumbers')
def start_cucumbers(start):
    print("\nStart")
    assert isinstance(start, int)
    return dict(start=start)


@when('I eat <eat> cucumbers')
def eat_cucumbers(start_cucumbers, eat):
    print("Eat")
    assert isinstance(eat, float)
    start_cucumbers['eat'] = eat


@then('I should have <left> cucumbers')
def should_have_left_cucumbers(start_cucumbers, start, eat, left):
    print("Validate result")
    assert isinstance(left, str)
    assert start - eat == int(left)
    assert start_cucumbers['start'] == start
    assert start_cucumbers['eat'] == eat

PiotrNestor avatar Feb 29 '16 10:02 PiotrNestor

pytest_bdd_after_scenario is a pytest hook, so I'd guess you need to implement it in a conftest.py in order to work.

The-Compiler avatar Feb 29 '16 10:02 The-Compiler

https://docs.pytest.org/en/latest/writing_plugins.html#plugin-discovery-order-at-tool-startup

amakhnach avatar Sep 25 '17 13:09 amakhnach

How to run multiple scenarios in a feature file with single browser instance and not restarting the browser instance separately for each scenarios using pytest-bdd?

Immanuel-Codoid avatar Apr 07 '22 10:04 Immanuel-Codoid