Sometimes numbers are close but not exactly equal due to small differences. Approximate comparisons help check if values are close enough.
Approximate comparisons (pytest.approx)
assert actual == pytest.approx(expected, rel=relative_tolerance, abs=absolute_tolerance)
expected is the value you want to compare against.
You can set rel for relative tolerance and abs for absolute tolerance to control how close values must be.
assert 0.1 + 0.2 == pytest.approx(0.3)
assert 100.0 == pytest.approx(100.1, rel=1e-2)
assert 0.0001 == pytest.approx(0.0, abs=1e-3)
This test checks three cases where values are close but not exactly equal. It uses pytest.approx to allow small differences.
import pytest def test_approximate_comparison(): result = 0.1 + 0.2 expected = 0.3 assert result == pytest.approx(expected) # Test with relative tolerance value = 100.0 expected_value = 100.1 assert value == pytest.approx(expected_value, rel=1e-2) # Test with absolute tolerance small_value = 0.0001 zero = 0.0 assert small_value == pytest.approx(zero, abs=1e-3)
Use pytest.approx to avoid test failures caused by tiny floating point errors.
You can combine rel and abs to fine-tune how close values must be.
Approximate comparisons work for floats and sequences like lists or tuples of floats.
Approximate comparisons help test values that are close but not exactly equal.
Use pytest.approx with optional relative and absolute tolerances.
This avoids false test failures due to small rounding differences.
Practice
pytest.approx in tests?Solution
Step 1: Understand floating-point comparison issues
Floating-point numbers can have tiny differences due to rounding, so exact equality often fails.Step 2: Role of
pytest.approxpytest.approxallows tests to pass if numbers are close within a tolerance, avoiding false failures.Final Answer:
To compare floating-point numbers allowing small differences -> Option AQuick Check:
Approximate comparison = floating-point tolerance [OK]
- Using == for floats causing flaky tests
- Confusing approx with exception testing
- Using approx for string comparisons
result is approximately equal to 3.14 using pytest.approx?Solution
Step 1: Identify correct usage of pytest.approx
The correct syntax uses the function call form: pytest.approx(value).Step 2: Check assertion syntax
Assertions use '==' to compare, '=' is assignment and brackets [] are invalid here.Final Answer:
assert result == pytest.approx(3.14) -> Option DQuick Check:
Use '==' and parentheses with pytest.approx [OK]
- Using '=' instead of '==' in assert
- Calling approx without pytest prefix if not imported
- Using square brackets instead of parentheses
def test_value():
value = 0.1 + 0.2
assert value == pytest.approx(0.3)Solution
Step 1: Understand floating-point addition
0.1 + 0.2 is not exactly 0.3 due to floating-point precision limits.Step 2: Effect of pytest.approx in assertion
pytest.approx allows a small tolerance, so the assertion passes despite tiny difference.Final Answer:
Test passes because pytest.approx allows small differences -> Option CQuick Check:
Floating-point sum ≈ 0.3 passes with approx [OK]
- Assuming 0.1 + 0.2 equals exactly 0.3
- Expecting test to fail without approx
- Thinking pytest.approx causes syntax errors
def test_approx():
result = 1.0001
assert result == pytest.approx(1.0, rel=0.00001)Solution
Step 1: Check relative tolerance meaning
rel=0.00001 means allowed difference is 0.00001 * 1.0 = 0.00001.Step 2: Compare actual difference with tolerance
Difference is 0.0001, which is greater than 0.00001, so assertion fails.Final Answer:
The relative tolerance is too small, causing test to fail -> Option AQuick Check:
Difference > rel tolerance causes failure [OK]
- Assuming rel parameter is invalid
- Ignoring difference size vs tolerance
- Confusing syntax error with logic error
pytest.approx?Solution
Step 1: Understand absolute vs relative tolerance
Absolute tolerance (abs) allows fixed difference; relative (rel) allows percentage difference.Step 2: Match tolerance to requirement
Variation up to 0.5 units means absolute tolerance of 0.5 is correct.Final Answer:
assert result == pytest.approx(100, abs=0.5) -> Option BQuick Check:
Use abs=0.5 for fixed unit tolerance [OK]
- Using rel when fixed tolerance needed
- Setting abs too small to cover variation
- Confusing abs and rel parameters
Ready to prove it?
Pass the test and this pattern moves unknown
