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

Code before:
                np.array([[4.0], [5.0]])
            )
        message = "\n".join(str(e.value).split("\n")[1:])
        assert message == "\n".join(
            [
                "  ",
                "  Impossible to compare arrays with different shapes.",
                "  Shapes: (2, 1) and (2, 2)",
            ]
        )

        with pytest.raises(AssertionError) as e:
            assert [1.0, 2.0, 3.0] == pytest.approx([4.0, 5.0])
        message = "\n".join(str(e.value).split("\n")[1:])
        assert message == "\n".join(
            [
                "  ",
                "  Impossible to compare lists with different sizes.",
                "  Lengths: 2 and 3",
            ]
        )

    def test_error_messages_with_different_verbosity(
        self, assert_approx_raises_regex, monkeypatch: MonkeyPatch
    ):
        monkeypatch.delenv("CI")
        np = pytest.importorskip("numpy")
        for v in [0, 1, 2]:
            # Verbosity level doesn't affect the error message for scalars
            assert_approx_raises_regex(
                2.0,
                1.0,
                [
                    "",
                    "  comparison failed",
                    f"  Obtained: {SOME_FLOAT}",
                    f"  Expected: {SOME_FLOAT} ± {SOME_FLOAT}",
                ],
                verbosity_level=v,
            )

        a = np.linspace(1, 101, 20)
        b = np.linspace(2, 102, 20)
        assert_approx_raises_regex(
            a,
            b,
            [
                r"^  $",
                r"^  comparison failed. Mismatched elements: 20 / 20:$",
                rf"^  Max absolute difference: {SOME_FLOAT}$",
                rf"^  Max relative difference: {SOME_FLOAT}$",

Review comment (security):
I believe we want to remove this env var for all tests, correct?

If so, I suggest you add this autouse fixture to `conftest.py`:

```python
@pytest.fixture(autouse=True)
def remove_ci_env_var(monkeypatch: MonkeyPatch) -> None:
    """
    Make the test suite insensitive if it is running in CI or not.
    
    If a test requires the variable, it should set it explicitly.
    """
    monkeypatch.delenv("CI", raising=False)
```

Then you can revert the changes done to the tests.

Code after:
            )
        message = "\n".join(str(e.value).split("\n")[1:])
        assert message == "\n".join(
            [
                "  ",
                "  Impossible to compare arrays with different shapes.",
                "  Shapes: (2, 1) and (2, 2)",
            ]
        )

        with pytest.raises(AssertionError) as e:
            assert [1.0, 2.0, 3.0] == pytest.approx([4.0, 5.0])
        message = "\n".join(str(e.value).split("\n")[1:])
        assert message == "\n".join(
            [
                "  ",
                "  Impossible to compare lists with different sizes.",
                "  Lengths: 2 and 3",
            ]
        )

    def test_error_messages_with_different_verbosity(self, assert_approx_raises_regex):
        np = pytest.importorskip("numpy")
        for v in [0, 1, 2]:
            # Verbosity level doesn't affect the error message for scalars
            assert_approx_raises_regex(
                2.0,
                1.0,
                [
                    "",
                    "  comparison failed",
                    f"  Obtained: {SOME_FLOAT}",
                    f"  Expected: {SOME_FLOAT} ± {SOME_FLOAT}",
                ],
                verbosity_level=v,
            )

        a = np.linspace(1, 101, 20)
        b = np.linspace(2, 102, 20)
        assert_approx_raises_regex(
            a,
            b,
            [
                r"^  $",
                r"^  comparison failed. Mismatched elements: 20 / 20:$",
                rf"^  Max absolute difference: {SOME_FLOAT}$",
                rf"^  Max relative difference: {SOME_FLOAT}$",
                r"^  Index \| Obtained\s+\| Expected\s+$",
                rf"^  \(0,\)\s+\| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}e-{SOME_INT}$",
                rf"^  \(1,\)\s+\| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}e-{SOME_INT}\.\.\.$",
                "^  $",