Skip to content

assert a == approx(b) when b is dict containing zero value results in ZeroDivisionError #10533

@arkanoid87

Description

@arkanoid87

Pytest behaves differently when comparing dictionaries containing zero values compared to lists containing same values

pytest==7.2.0
Ubuntu 22.04

import pytest

def test_foo_dict():
    a = {'foo': 42.0}
    b = {'foo': 0.0}
    assert a == pytest.approx(b) # ZeroDivisionError in pytest/python_api.py

def test_foo_list():
    a = [42.0]
    b = [0.0]
    assert a == pytest.approx(b) # OK
_____________________ test_foo_dict

    def test_foo_dict():
        a = {'foo': 42.0}
        b = {'foo': 0.0}
>       assert a == pytest.approx(b)
E       AssertionError: assert {'foo': 42.0} == approx({'foo': 0.0 ± 1.0e-12})
E         (pytest_assertion plugin: representation of details failed: /home/arkanoid/test/venv/lib/python3.10/site-packages/_pytest/python_api.py:274: ZeroDivisionError: float division by zero.
E          Probably an object has a faulty __repr__.)

extra/test_pytest_issue.py:9: AssertionError

_____________________ test_foo_list

    def test_foo_list():
        a = [42.0]
        b = [0.0]
>       assert a == pytest.approx(b)
E       assert [42.0] == approx([0.0 ± 1.0e-12])
E         comparison failed. Mismatched elements: 1 / 1:
E         Max absolute difference: 42.0
E         Max relative difference: 1.0
E         Index | Obtained | Expected     
E         0     | 42.0     | 0.0 ± 1.0e-12

extra/test_pytest_issue.py:15: AssertionError

Metadata

Metadata

Labels

type: bugproblem that needs to be addressed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions