Bird
0
0

You want to write a pytest test function that checks if a function 'add' returns the correct sum for inputs 2 and 3. Which test function is correct?

hard🚀 Application Q8 of 15
PyTest - Test Organization
You want to write a pytest test function that checks if a function 'add' returns the correct sum for inputs 2 and 3. Which test function is correct?
Adef add_test():\n assert add(2, 3) == 5
Bdef test_add():\n assert add(2, 3) == 5
Cdef test_add():\n assert add(2, 3) = 5
Ddef test_add():\n assert add(2, 3) != 5
Step-by-Step Solution
Solution:
  1. Step 1: Verify function name and assert syntax

    The test function must start with 'test_' and use '==' in assert for comparison.
  2. Step 2: Check correctness of assertion

    def test_add():\n assert add(2, 3) == 5 uses correct name and asserts add(2,3) equals 5, which is correct.
  3. Final Answer:

    def test_add():\n assert add(2, 3) == 5 -> Option B
  4. Quick Check:

    Correct test name and assert syntax = def test_add():\n assert add(2, 3) == 5 [OK]
Quick Trick: Test function names start with 'test_', use '==' in asserts [OK]
Common Mistakes:
MISTAKES
  • Wrong function name prefix
  • Using '=' instead of '==' in assert
  • Asserting wrong condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes