Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Removed
Fixed
+++++
* Made type annotations stronger and removed most of the ``typing.Any`` usages and ``# type: ignore`` annotations. `#658 <https://github.com/pytest-dev/pytest-bdd/pull/658>`_
* Mark steps that raise pytest outcome exceptions as failed in cucumber JSON output. `#770 <https://github.com/pytest-dev/pytest-bdd/issues/770>`_

Security
++++++++
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_bdd/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def pytest_bdd_step_error(
step: Step,
step_func: Callable[..., object],
step_func_args: dict[str, object],
exception: Exception,
exception: BaseException,
) -> object:
"""Called when step function failed to execute."""

Expand Down
2 changes: 1 addition & 1 deletion src/pytest_bdd/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def pytest_bdd_step_error(
step: Step,
step_func: Callable[..., object],
step_func_args: dict[str, object],
exception: Exception,
exception: BaseException,
) -> None:
reporting.step_error(request, feature, scenario, step, step_func, step_func_args, exception)

Expand Down
2 changes: 1 addition & 1 deletion src/pytest_bdd/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def step_error(
step: Step,
step_func: Callable[..., object],
step_func_args: dict[str, object],
exception: Exception,
exception: BaseException,
) -> None:
"""Finalize the step report as failed."""
scenario_reports_registry[request.node].fail()
Expand Down
3 changes: 2 additions & 1 deletion src/pytest_bdd/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import pytest
from _pytest.fixtures import FixtureDef, FixtureManager, FixtureRequest, call_fixture_func
from _pytest.outcomes import TEST_OUTCOME

from . import exceptions
from .compat import getfixturedefs, inject_fixture
Expand Down Expand Up @@ -247,7 +248,7 @@ def _execute_step_function(
# so that we can allow "yield" statements in it
return_value = call_fixture_func(fixturefunc=context.step_func, request=request, kwargs=kwargs)

except Exception as exception:
except TEST_OUTCOME as exception:
request.config.hook.pytest_bdd_step_error(exception=exception, **kw)
raise

Expand Down
37 changes: 37 additions & 0 deletions tests/feature/test_cucumber_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,40 @@ def test_passing_outline():
]

assert jsonobject == expected


def test_pytest_outcome_exception_marks_step_failed(pytester):
"""Test pytest outcome exceptions are reflected in cucumber json."""
pytester.makefile(
".feature",
test=textwrap.dedent(
"""
Feature: Outcome exceptions

Scenario: Failing outcome
When a pytest outcome failure is raised
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""
import pytest
from pytest_bdd import scenario, when

@scenario("test.feature", "Failing outcome")
def test_failing_outcome():
pass

@when("a pytest outcome failure is raised")
def _():
pytest.fail("timeout-style failure")
"""
)
)

result, jsonobject = runandparse(pytester)
result.assert_outcomes(failed=1)
[scenario] = jsonobject[0]["elements"]
[step] = scenario["steps"]
assert step["result"]["status"] == "failed"