Skip to content

ENH: testing: hide assertion internals in pytest traceback #780

@ntessore

Description

@ntessore

Trivial one, but: when using xpx.testing, pytest shows the failed asserts within the xpx library code, not at the call site.

# test_xpx.py
import numpy as np
import array_api_extra as xpx

def test_less():
    xpx.testing.assert_less(np.arange(3), np.arange(3))
% pytest -xvv test_xpx.py
==================================== test session starts ====================================
...

test_xpx.py::test_less FAILED                                                         [100%]

========================================= FAILURES ==========================================
_________________________________________ test_less _________________________________________

    def test_less():
>       xpx.testing.assert_less(np.arange(3), np.arange(3))

test_xpx.py:5:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

x = array([0, 1, 2]), y = array([0, 1, 2])

    def assert_less(
        x: Array,
        y: Array,
        *,
        err_msg: str = "",
        verbose: bool = True,
        check_dtype: bool = True,
        check_shape: bool = True,
        check_scalar: bool = False,
        xp: ModuleType | None = None,
    ) -> None:
        ...
        (the entire function's source code here)
        ...
>       np.testing.assert_array_less(x_np, y_np, err_msg=err_msg, verbose=verbose)
E       AssertionError:
E       Arrays are not strictly ordered `x < y`
E
E       Mismatched elements: 3 / 3 (100%)
E       Max absolute difference among violations: 0
E       Max relative difference among violations: 0.
E        x: array([0, 1, 2])
E        y: array([0, 1, 2])

.../site-packages/array_api_extra/testing.py:928: AssertionError
================================== short test summary info ==================================
FAILED test_xpx.py::test_less - AssertionError:
Arrays are not strictly ordered `x < y`

Mismatched elements: 3 / 3 (100%)
Max absolute difference among violations: 0
Max relative difference among violations: 0.
 x: array([0, 1, 2])
 y: array([0, 1, 2])
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================== 1 failed in 0.24s =====================================

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions