Repository: pytest-dev/pytest
File: testing/python/approx.py (Python)

Code before:
        assert b != pytest.approx(a, abs=0.5)

    def test_approx_dicts_with_mismatch_on_keys(self) -> None:
        """https://github.com/pytest-dev/pytest/issues/13816"""
        expected = {"a": 1, "b": 3}
        actual = {"a": 1, "c": 3}

        with pytest.raises(
            AssertionError,
            match=re.escape(
                "comparison failed.\n  Mappings has different keys: "
                "expected dict_keys(['a', 'b']) but got dict_keys(['a', 'c'])"
            ),
        ):
            assert actual == approx(expected)

    def test_approx_on_unordered_mapping_with_mismatch(self) -> None:
        """https://github.com/pytest-dev/pytest/pull/12445"""
        expected = {"a": 1, "c": 3}
        actual = {"c": 5, "a": 1}

        with pytest.raises(
            AssertionError,
            match="Mismatched elements: 1 / 2:\n  Max absolute difference: 2\n",
        ):
            assert expected == approx(actual)


class MyVec3:  # incomplete
    """sequence like"""

    _x: int
    _y: int
    _z: int

    def __init__(self, x: int, y: int, z: int):
        self._x, self._y, self._z = x, y, z

    def __repr__(self) -> str:
        return f"<MyVec3 {self._x} {self._y} {self._z}>"

    def __len__(self) -> int:
        return 3

    def __getitem__(self, key: int) -> int:
        if key == 0:
            return self._x
        if key == 1:
            return self._y
        if key == 2:
            return self._z

Review comment (suggestion):
Given the output message is central to the issue, please rewrite this test using `pytester` and verifying the output itself.

Also, let's use a few more keys in both dictionaries to cover more ground. I suggest:

```python
expected = {"a": 1, "b": 2, "c": 3, "d": 4}
actual = {"d": 4, "c": 5, "a": 8}
```

Code after:
        assert b != pytest.approx(a, abs=0.5)

    def test_approx_dicts_with_mismatch_on_keys(self) -> None:
        """https://github.com/pytest-dev/pytest/issues/13816"""
        expected = {"a": 1, "b": 3}
        actual = {"a": 1, "c": 3}

        with pytest.raises(
            AssertionError,
            match=re.escape(
                "comparison failed.\n  Mappings has different keys: "
                "expected dict_keys(['a', 'b']) but got dict_keys(['a', 'c'])"
            ),
        ):
            assert actual == approx(expected)

    def test_approx_on_unordered_mapping_with_mismatch(
        self, pytester: Pytester
    ) -> None:
        """https://github.com/pytest-dev/pytest/issues/12444"""
        pytester.makepyfile(
            """
            import pytest

            def test_approx_on_unordered_mapping_with_mismatch():
                expected = {"a": 1, "b": 2, "c": 3, "d": 4}
                actual = {"d": 4, "c": 5, "a": 8, "b": 2}
                assert actual == pytest.approx(expected)
            """
        )
        result = pytester.runpytest()
        result.assert_outcomes(failed=1)
        result.stdout.fnmatch_lines(
            [
                "*comparison failed.**Mismatched elements: 2 / 4:*",
                "*Max absolute difference: 7*",
                "*Index | Obtained | Expected *",
                "* a * | 8 * | 1 *",
                "* c * | 5 * | 3 *",
            ]
        )

    def test_approx_on_unordered_mapping_matching(self, pytester: Pytester) -> None:
        """https://github.com/pytest-dev/pytest/issues/12444"""
        pytester.makepyfile(
            """
            import pytest
            def test_approx_on_unordered_mapping_matching():
                expected = {"a": 1, "b": 2, "c": 3, "d": 4}
                actual = {"d": 4, "c": 3, "a": 1, "b": 2}
                assert actual == pytest.approx(expected)