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

Remove internal pytest functions from displayed traceback on step failure #766

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

benfogle
Copy link

This change removes the stack trace of call_fixture_func from the output when a test fails:

# contents of example.feature
Feature: Test
    Scenario: Test
        Given foo
# contents of test_example.py
from pytest_bdd import scenario, given

@scenario("example.feature", "Test")
def test_example():
    pass

@given("foo")
def foo():
    assert 1 == 2

Before

$ pytest
=================================== test session starts ====================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/ben/scratch/example
plugins: bdd-8.1.0
collected 1 item

test_example.py F                                                                    [100%]

========================================= FAILURES =========================================
_______________________________________ test_example _______________________________________

fixturefunc = <function foo at 0x7f7db80b9f30>
request = <FixtureRequest for <Function test_example>>, kwargs = {}

    def call_fixture_func(
        fixturefunc: _FixtureFunc[FixtureValue], request: FixtureRequest, kwargs
    ) -> FixtureValue:
        if is_generator(fixturefunc):
            fixturefunc = cast(
                Callable[..., Generator[FixtureValue, None, None]], fixturefunc
            )
            generator = fixturefunc(**kwargs)
            try:
                fixture_result = next(generator)
            except StopIteration:
                raise ValueError(f"{request.fixturename} did not yield a value") from None
            finalizer = functools.partial(_teardown_yield_fixture, fixturefunc, generator)
            request.addfinalizer(finalizer)
        else:
            fixturefunc = cast(Callable[..., FixtureValue], fixturefunc)
>           fixture_result = fixturefunc(**kwargs)

venv/lib/python3.10/site-packages/_pytest/fixtures.py:898:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    @given("foo")
    def foo():
>       assert 1 == 2
E       assert 1 == 2

test_example.py:10: AssertionError
================================= short test summary info ==================================
FAILED test_example.py::test_example - assert 1 == 2
==================================== 1 failed in 0.02s =====================================

After

$ pytest
=================================== test session starts ====================================
platform linux -- Python 3.10.12, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/ben/scratch/example
plugins: bdd-8.1.0, xdist-3.6.1, anyio-4.7.0
collected 1 item

test_example.py F                                                                    [100%]

========================================= FAILURES =========================================
_______________________________________ test_example _______________________________________

    @given("foo")
    def foo():
>       assert 1 == 2
E       assert 1 == 2

test_example.py:10: AssertionError
================================= short test summary info ==================================
FAILED test_example.py::test_example - assert 1 == 2
==================================== 1 failed in 0.04s =====================================

@@ -57,6 +58,10 @@

scenario_wrapper_template_registry: WeakKeyDictionary[Callable[..., object], ScenarioTemplate] = WeakKeyDictionary()

# HACK: call_fixture_func will appear in the traceback whenever a step fails. This is
# ugly and irrelevant, so this hack will hide it.
_pytest.fixtures.__tracebackhide__ = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not patch pytest internals

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arguably we shouldn't be using pytest internals either, but point taken. I can rework it to not do that, though it's a bit more complicated.

@benfogle benfogle force-pushed the remove-internal-stack-trace branch from 1a9a8f7 to a3cc860 Compare January 31, 2025 13:37
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

Successfully merging this pull request may close these issues.

2 participants