Skip to content

Commit e64249a

Browse files
committed
Mark pytest outcome step failures in cucumber JSON
1 parent 5e3f8b7 commit e64249a

6 files changed

Lines changed: 43 additions & 4 deletions

File tree

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Removed
3030
Fixed
3131
+++++
3232
* 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>`_
33+
* Mark steps that raise pytest outcome exceptions as failed in cucumber JSON output.
3334

3435
Security
3536
++++++++

src/pytest_bdd/hooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def pytest_bdd_step_error(
5353
step: Step,
5454
step_func: Callable[..., object],
5555
step_func_args: dict[str, object],
56-
exception: Exception,
56+
exception: BaseException,
5757
) -> object:
5858
"""Called when step function failed to execute."""
5959

src/pytest_bdd/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def pytest_bdd_step_error(
101101
step: Step,
102102
step_func: Callable[..., object],
103103
step_func_args: dict[str, object],
104-
exception: Exception,
104+
exception: BaseException,
105105
) -> None:
106106
reporting.step_error(request, feature, scenario, step, step_func, step_func_args, exception)
107107

src/pytest_bdd/reporting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def step_error(
219219
step: Step,
220220
step_func: Callable[..., object],
221221
step_func_args: dict[str, object],
222-
exception: Exception,
222+
exception: BaseException,
223223
) -> None:
224224
"""Finalize the step report as failed."""
225225
scenario_reports_registry[request.node].fail()

src/pytest_bdd/scenario.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import pytest
2626
from _pytest.fixtures import FixtureDef, FixtureManager, FixtureRequest, call_fixture_func
27+
from _pytest.outcomes import TEST_OUTCOME
2728

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

250-
except Exception as exception:
251+
except TEST_OUTCOME as exception:
251252
request.config.hook.pytest_bdd_step_error(exception=exception, **kw)
252253
raise
253254

tests/feature/test_cucumber_json.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,40 @@ def test_passing_outline():
237237
]
238238

239239
assert jsonobject == expected
240+
241+
242+
def test_pytest_outcome_exception_marks_step_failed(pytester):
243+
"""Test pytest outcome exceptions are reflected in cucumber json."""
244+
pytester.makefile(
245+
".feature",
246+
test=textwrap.dedent(
247+
"""
248+
Feature: Outcome exceptions
249+
250+
Scenario: Failing outcome
251+
When a pytest outcome failure is raised
252+
"""
253+
),
254+
)
255+
pytester.makepyfile(
256+
textwrap.dedent(
257+
"""
258+
import pytest
259+
from pytest_bdd import scenario, when
260+
261+
@scenario("test.feature", "Failing outcome")
262+
def test_failing_outcome():
263+
pass
264+
265+
@when("a pytest outcome failure is raised")
266+
def _():
267+
pytest.fail("timeout-style failure")
268+
"""
269+
)
270+
)
271+
272+
result, jsonobject = runandparse(pytester)
273+
result.assert_outcomes(failed=1)
274+
[scenario] = jsonobject[0]["elements"]
275+
[step] = scenario["steps"]
276+
assert step["result"]["status"] == "failed"

0 commit comments

Comments
 (0)