Bird
0
0

Identify the error in this pytest fixture code:

medium📝 Debug Q6 of 15
PyTest - Fixtures
Identify the error in this pytest fixture code:
import pytest

@pytest.fixture
class my_fixture:
    def __init__(self):
        self.value = 10

def test_value(my_fixture):
    assert my_fixture.value == 10
AMissing return statement in fixture function.
BIncorrect assertion syntax in test.
CFixtures cannot be classes; must be functions.
DFixture name conflicts with test function.
Step-by-Step Solution
Solution:
  1. Step 1: Check fixture type

    Pytest fixtures must be functions, not classes decorated with @pytest.fixture.
  2. Step 2: Understand fixture usage

    Using a class as a fixture causes pytest to error because it expects a callable function.
  3. Final Answer:

    Fixtures cannot be classes; must be functions. -> Option C
  4. Quick Check:

    Fixture type = function only [OK]
Quick Trick: Fixtures must be functions, not classes [OK]
Common Mistakes:
MISTAKES
  • Decorating classes as fixtures
  • Forgetting to return values in fixtures
  • Misnaming fixtures and tests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes