Bird
Raised Fist0
PyTesttesting~10 mins

Approximate comparisons (pytest.approx) - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assert that 0.1 + 0.2 is approximately equal to 0.3 using pytest.approx.

PyTest
import pytest

def test_sum():
    assert 0.1 + 0.2 == [1]
Drag options to blanks, or click blank then click option'
Apytest.approx(0.3)
B0.3
Capprox(0.3)
D0.30000000000000004
Attempts:
3 left
💡 Hint
Common Mistakes
Using direct equality (==) with floating points causes test failures due to precision errors.
Using a plain float instead of pytest.approx for approximate comparison.
2fill in blank
medium

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.

PyTest
import pytest

def test_list_approx():
    result = [1.0, 2.0, 3.0]
    expected = [1.0, 2.0001, 3.0]
    assert result == [1]
Drag options to blanks, or click blank then click option'
Apytest.approx(expected)
Bexpected
Capprox(expected)
Dexpected.approx()
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing lists directly without pytest.approx causes failures due to small differences.
Trying to call approx() without importing pytest.
3fill in blank
hard

Fix the error in the test by completing the code to compare floating point numbers approximately with a relative tolerance of 1e-3.

PyTest
import pytest

def test_relative_tolerance():
    value = 100.0
    expected = 100.1
    assert value == pytest.approx(expected, [1]=1e-3)
Drag options to blanks, or click blank then click option'
Aabs
Brelative
Crel
Drel_tol
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'abs' instead of 'rel' for relative tolerance.
Using 'rel_tol' or 'relative' which are not valid pytest.approx parameters.
4fill in blank
hard

Fill both blanks to assert that 0.123456 is approximately equal to 0.123 with an absolute tolerance of 1e-3.

PyTest
import pytest

def test_absolute_tolerance():
    assert 0.123456 == pytest.approx(0.123, [1]=[2])
Drag options to blanks, or click blank then click option'
Aabs
Brel
C1e-3
D1e-4
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'rel' instead of 'abs' for absolute tolerance.
Setting tolerance too small to allow the difference.
5fill in blank
hard

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.

PyTest
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])
Drag options to blanks, or click blank then click option'
Arel
Babs
C1e-2
D1e-4
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'rel' and 'abs' keywords.
Using incorrect tolerance values causing test failures.

Practice

(1/5)
1. What is the main purpose of using pytest.approx in tests?
easy
A. To compare floating-point numbers allowing small differences
B. To check if two strings are exactly the same
C. To test if a function raises an exception
D. To verify if a list contains a specific element

Solution

  1. Step 1: Understand floating-point comparison issues

    Floating-point numbers can have tiny differences due to rounding, so exact equality often fails.
  2. Step 2: Role of pytest.approx

    pytest.approx allows tests to pass if numbers are close within a tolerance, avoiding false failures.
  3. Final Answer:

    To compare floating-point numbers allowing small differences -> Option A
  4. Quick Check:

    Approximate comparison = floating-point tolerance [OK]
Hint: Use approx for floats, not exact matches [OK]
Common Mistakes:
  • Using == for floats causing flaky tests
  • Confusing approx with exception testing
  • Using approx for string comparisons
2. Which of the following is the correct syntax to assert that result is approximately equal to 3.14 using pytest.approx?
easy
A. assert result == pytest.approx[3.14]
B. assert result == approx(3.14)
C. assert result = pytest.approx(3.14)
D. assert result == pytest.approx(3.14)

Solution

  1. Step 1: Identify correct usage of pytest.approx

    The correct syntax uses the function call form: pytest.approx(value).
  2. Step 2: Check assertion syntax

    Assertions use '==' to compare, '=' is assignment and brackets [] are invalid here.
  3. Final Answer:

    assert result == pytest.approx(3.14) -> Option D
  4. Quick Check:

    Use '==' and parentheses with pytest.approx [OK]
Hint: Use '==' and parentheses with pytest.approx [OK]
Common Mistakes:
  • Using '=' instead of '==' in assert
  • Calling approx without pytest prefix if not imported
  • Using square brackets instead of parentheses
3. What will be the result of this test code?
def test_value():
    value = 0.1 + 0.2
    assert value == pytest.approx(0.3)
medium
A. Test passes because 0.1 + 0.2 is exactly 0.3
B. Test fails due to floating-point precision error
C. Test passes because pytest.approx allows small differences
D. SyntaxError because pytest.approx is used incorrectly

Solution

  1. Step 1: Understand floating-point addition

    0.1 + 0.2 is not exactly 0.3 due to floating-point precision limits.
  2. Step 2: Effect of pytest.approx in assertion

    pytest.approx allows a small tolerance, so the assertion passes despite tiny difference.
  3. Final Answer:

    Test passes because pytest.approx allows small differences -> Option C
  4. Quick Check:

    Floating-point sum ≈ 0.3 passes with approx [OK]
Hint: Use approx to handle float rounding errors [OK]
Common Mistakes:
  • Assuming 0.1 + 0.2 equals exactly 0.3
  • Expecting test to fail without approx
  • Thinking pytest.approx causes syntax errors
4. Identify the error in this test code snippet:
def test_approx():
    result = 1.0001
    assert result == pytest.approx(1.0, rel=0.00001)
medium
A. The relative tolerance is too small, causing test to fail
B. pytest.approx does not accept rel parameter
C. The assertion syntax is incorrect
D. The variable result should be an integer

Solution

  1. Step 1: Check relative tolerance meaning

    rel=0.00001 means allowed difference is 0.00001 * 1.0 = 0.00001.
  2. Step 2: Compare actual difference with tolerance

    Difference is 0.0001, which is greater than 0.00001, so assertion fails.
  3. Final Answer:

    The relative tolerance is too small, causing test to fail -> Option A
  4. Quick Check:

    Difference > rel tolerance causes failure [OK]
Hint: Check if difference fits within rel tolerance [OK]
Common Mistakes:
  • Assuming rel parameter is invalid
  • Ignoring difference size vs tolerance
  • Confusing syntax error with logic error
5. You want to test that a function returns a value close to 100, but it may vary by up to 0.5 units. Which is the best way to write this assertion using pytest.approx?
hard
A. assert result == pytest.approx(100, rel=0.5)
B. assert result == pytest.approx(100, abs=0.5)
C. assert result == pytest.approx(100, abs=0.05)
D. assert result == pytest.approx(100, rel=0.005)

Solution

  1. Step 1: Understand absolute vs relative tolerance

    Absolute tolerance (abs) allows fixed difference; relative (rel) allows percentage difference.
  2. Step 2: Match tolerance to requirement

    Variation up to 0.5 units means absolute tolerance of 0.5 is correct.
  3. Final Answer:

    assert result == pytest.approx(100, abs=0.5) -> Option B
  4. Quick Check:

    Use abs=0.5 for fixed unit tolerance [OK]
Hint: Use abs for fixed tolerance, rel for percentage [OK]
Common Mistakes:
  • Using rel when fixed tolerance needed
  • Setting abs too small to cover variation
  • Confusing abs and rel parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard