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
Let's say I have a utility function in my tests calling some assumptions for tests to use:
If I run this as is, the output will show the utility function (
assume_is_42) as the failure site: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 inassert_is_42.But if I set
__tracebackhide__ = Trueinassume_is_42(), I get the opposite behavior: now the failure point is inassert_is_42, seemingly ignoring all__tracebackhide__values:results in
Here is the output I would hope to get instead: