Complete the code to assert that 0.1 + 0.2 is approximately equal to 0.3 using pytest.approx.
import pytest def test_sum(): assert 0.1 + 0.2 == [1]
pytest.approx allows approximate comparison of floating point numbers, handling small rounding errors.
Complete the code to assert that the list [1.0, 2.0, 3.0] is approximately equal to [1.0, 2.0001, 3.0] using pytest.approx.
import pytest def test_list_approx(): result = [1.0, 2.0, 3.0] expected = [1.0, 2.0001, 3.0] assert result == [1]
pytest.approx works with lists to compare each element approximately.
Fix the error in the test by completing the code to compare floating point numbers approximately with a relative tolerance of 1e-3.
import pytest def test_relative_tolerance(): value = 100.0 expected = 100.1 assert value == pytest.approx(expected, [1]=1e-3)
The correct keyword argument for relative tolerance in pytest.approx is rel.
Fill both blanks to assert that 0.123456 is approximately equal to 0.123 with an absolute tolerance of 1e-3.
import pytest def test_absolute_tolerance(): assert 0.123456 == pytest.approx(0.123, [1]=[2])
Use abs for absolute tolerance and set it to 1e-3 to allow small absolute differences.
Fill all three blanks to assert that the dictionary {'a': 1.0, 'b': 2.0} is approximately equal to {'a': 1.001, 'b': 2.002} with relative tolerance 1e-2 and absolute tolerance 1e-4.
import pytest def test_dict_approx(): result = {'a': 1.0, 'b': 2.0} expected = {'a': 1.001, 'b': 2.002} assert result == pytest.approx(expected, [1]=[2], [3]=[4])
pytest.approx accepts both rel for relative tolerance and abs for absolute tolerance. Here, rel=1e-2 and abs=1e-4 allow approximate comparison of dictionary values.