Bird
0
0

What will happen when this test is executed?

medium📝 Predict Output Q5 of 15
PyTest - Writing Assertions
What will happen when this test is executed?
def test_precision():
    assert 1.0002 == pytest.approx(1.0, rel=1e-3)
AThe test will fail because 1.0002 exceeds the relative tolerance
BThe test will pass because 1.0002 is within the relative tolerance
CThe test will raise a ValueError due to invalid parameters
DThe test will be skipped due to syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Calculate relative difference

    The relative difference is |1.0002 - 1.0| / 1.0 = 0.0002.
  2. Step 2: Compare with tolerance

    The relative tolerance is 0.001, and 0.0002 < 0.001, so the assertion passes.
  3. Final Answer:

    The test will pass because 1.0002 is within the relative tolerance -> Option B
  4. Quick Check:

    Relative difference less than tolerance means pass [OK]
Quick Trick: Check if relative difference is within tolerance [OK]
Common Mistakes:
MISTAKES
  • Misunderstanding relative tolerance meaning
  • Assuming any difference causes failure
  • Confusing absolute and relative tolerance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes