Bird
0
0

What is wrong with this pytest code if the goal is to group tests inside a class named TestMath?

medium📝 Debug Q14 of 15
PyTest - Test Organization
What is wrong with this pytest code if the goal is to group tests inside a class named TestMath?
class TestMath:
    def test_add():
        assert 1 + 1 == 2

    def test_subtract(self):
        assert 2 - 1 == 1
Atest_subtract should not have self parameter
BClass name should not start with Test
CTests inside classes cannot have assertions
Dtest_add is missing the self parameter
Step-by-Step Solution
Solution:
  1. Step 1: Check method definitions inside test classes

    Instance methods inside test classes must have self as first parameter.
  2. Step 2: Identify the error

    test_add is missing self, so pytest will error or skip it.
  3. Final Answer:

    test_add is missing the self parameter -> Option D
  4. Quick Check:

    Test methods in classes need self [OK]
Quick Trick: Test methods in classes must include self parameter [OK]
Common Mistakes:
MISTAKES
  • Omitting self in test methods
  • Thinking class names cannot start with Test
  • Removing assertions from test methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes