Skip to content

Commit a3cc860

Browse files
committed
Remove internal pytest functions from displayed traceback on step failure
1 parent c893aa3 commit a3cc860

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/pytest_bdd/scenario.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,19 @@ def _execute_step_function(
245245

246246
# Execute the step as if it was a pytest fixture using `call_fixture_func`,
247247
# so that we can allow "yield" statements in it
248-
return_value = call_fixture_func(fixturefunc=context.step_func, request=request, kwargs=kwargs)
248+
try:
249+
return_value = call_fixture_func(fixturefunc=context.step_func, request=request, kwargs=kwargs)
250+
except BaseException as e:
251+
# BaseException needed to catch pytest.fail.
252+
253+
# HACK: call_fixture_func will appear in the traceback whenever a
254+
# step fails. This is ugly and irrelevant, so this hack will hide
255+
# it by injecting __tracebackhide__ into the local variables of the
256+
# stack trace. This is always the second frame, and this is safe
257+
# because this is the frame of code that is no longer running.
258+
if e.__traceback__ is not None and e.__traceback__.tb_next is not None:
259+
e.__traceback__.tb_next.tb_frame.f_locals["__tracebackhide__"] = True
260+
raise
249261

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

tests/feature/test_gherkin_terminal_reporter.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ def test_scenario_1():
181181
)
182182
result = pytester.runpytest("--gherkin-terminal-reporter", "--showlocals")
183183
result.assert_outcomes(passed=0, failed=1)
184-
result.stdout.fnmatch_lines("""request*=*<FixtureRequest for *""")
185184
result.stdout.fnmatch_lines("""local_var*=*MULTIPASS*""")
186185

187186

0 commit comments

Comments
 (0)