Skip to content

pytest-assume ignores __tracebackhide__ #43

@antoche

Description

@antoche

Let's say I have a utility function in my tests calling some assumptions for tests to use:

import pytest

def assert_is_42(n):
    __tracebackhide__ = True
    assert n == 42

def assume_is_42(n):
    with pytest.assume:
        assert_is_42(n)

def test_bar():
    assume_is_42(1)

If I run this as is, the output will show the utility function (assume_is_42) as the failure site:

==================================================================== FAILURES =====================================================================
____________________________________________________________________ test_bar _____________________________________________________________________

n = 1

    def assume_is_42(n):
        with pytest.assume:
>           assert_is_42(n)
E           FailedAssumption: 
E           1 Failed Assumptions:
E           
E           test_assume.py:9: AssumptionFailure
E           >>  assert_is_42(n)
E           AssertionError: assert 1 == 42

test_assume.py:9: FailedAssumption

What I would like to see instead is the failure site in the test function itself. Pytest allows us to do this using the __tracebackhide__ special variable (docs), which I'm using in assert_is_42.

But if I set __tracebackhide__ = True in assume_is_42(), I get the opposite behavior: now the failure point is in assert_is_42, seemingly ignoring all __tracebackhide__ values:

import pytest

def assert_is_42(n):
    __tracebackhide__ = True
    assert n == 42

def assume_is_42(n):
    __tracebackhide__ = True
    with pytest.assume:
        assert_is_42(n)

def test_bar():
    assume_is_42(1)

results in

==================================================================== FAILURES =====================================================================
____________________________________________________________________ test_bar _____________________________________________________________________

n = 1

    def assert_is_42(n):
        __tracebackhide__ = True
>       assert n == 42
E       FailedAssumption: 
E       1 Failed Assumptions:
E       
E       test_assume.py:10: AssumptionFailure
E       >>      assert_is_42(n)
E       AssertionError: assert 1 == 42

test_assume.py:5: FailedAssumption

Here is the output I would hope to get instead:

    def test_bar():
>       assume_is_42(1)
E       FailedAssumption: 
E       1 Failed Assumptions:
E       
E       test_assume.py:13: AssumptionFailure
E       >>  assume_is_42(n)
E       AssertionError: assert 1 == 42

test_assume.py:13: FailedAssumption

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions