Bird
0
0

Identify the error in this Python unittest code snippet:

medium📝 Debug Q6 of 15
Testing Fundamentals - Testing Types and Levels
Identify the error in this Python unittest code snippet:
import unittest

def subtract(a, b):
    return a - b

class TestSubtract(unittest.TestCase):
    def test_subtract():
        self.assertEqual(subtract(5, 3), 2)

if __name__ == '__main__':
    unittest.main()
AMissing 'self' parameter in test_subtract method
Bsubtract function has wrong return statement
CassertEqual is used incorrectly
Dunittest module is not imported
Step-by-Step Solution
Solution:
  1. Step 1: Check test method definition

    In unittest, test methods inside a class must have 'self' as the first parameter.
  2. Step 2: Identify the error

    test_subtract is defined without 'self', causing a TypeError when called.
  3. Final Answer:

    Missing 'self' parameter in test_subtract method -> Option A
  4. Quick Check:

    Test methods need 'self' parameter [OK]
Quick Trick: Test methods must include 'self' parameter [OK]
Common Mistakes:
  • Omitting 'self' in test method definitions
  • Assuming function logic error instead
  • Ignoring unittest import

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Testing Fundamentals Quizzes