Bird
0
0

Which of the following assertions correctly checks if output is approximately equal to 2.718 using pytest?

easy🧠 Conceptual Q2 of 15
PyTest - Writing Assertions
Which of the following assertions correctly checks if output is approximately equal to 2.718 using pytest?
Aassert pytest.approx(output) == 2.718
Bassert output == pytest.approx(2.718)
Cassert output == approx(2.718, abs=0.01)
Dassert output == pytest.approx(2.718, tol=0.01)
Step-by-Step Solution
Solution:
  1. Step 1: Correct usage of pytest.approx

    The standard syntax is assert value == pytest.approx(expected).
  2. Step 2: Check options

    assert output == pytest.approx(2.718) uses the correct syntax. assert pytest.approx(output) == 2.718 reverses the order, which is allowed but less common. assert output == approx(2.718, abs=0.01) uses approx without import. assert output == pytest.approx(2.718, tol=0.01) uses an invalid parameter tol.
  3. Final Answer:

    assert output == pytest.approx(2.718) -> Option B
  4. Quick Check:

    Standard syntax is value == pytest.approx(expected) [OK]
Quick Trick: Use assert value == pytest.approx(expected) [OK]
Common Mistakes:
MISTAKES
  • Using incorrect parameter names like 'tol'
  • Reversing the order without import
  • Using approx without importing it

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes