Bird
0
0

What will be the result of this test code?

medium📝 Predict Output Q13 of 15
PyTest - Writing Assertions
What will be the result of this test code?
def test_value():
    value = 0.1 + 0.2
    assert value == pytest.approx(0.3)
ATest passes because 0.1 + 0.2 is exactly 0.3
BTest fails due to floating-point precision error
CTest passes because pytest.approx allows small differences
DSyntaxError because pytest.approx is used incorrectly
Step-by-Step Solution
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]
Quick Trick: Use approx to handle float rounding errors [OK]
Common Mistakes:
MISTAKES
  • Assuming 0.1 + 0.2 equals exactly 0.3
  • Expecting test to fail without approx
  • Thinking pytest.approx causes syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes