Bird
0
0

Which of the following is the correct way to define a fixture in conftest.py?

easy📝 Syntax Q12 of 15
PyTest - Test Organization
Which of the following is the correct way to define a fixture in conftest.py?
Adef setup(): pass
B@pytest.fixture def my_fixture(): return 42
Cclass Fixture: pass
Dimport pytest my_fixture = 42
Step-by-Step Solution
Solution:
  1. Step 1: Recall fixture syntax in pytest

    Fixtures are defined using the @pytest.fixture decorator followed by a function.
  2. Step 2: Check each option

    @pytest.fixture def my_fixture(): return 42 correctly uses @pytest.fixture and defines a function returning a value. Others either lack the decorator or are incorrect syntax.
  3. Final Answer:

    @pytest.fixture\ndef my_fixture(): return 42 -> Option B
  4. Quick Check:

    Fixture needs @pytest.fixture decorator = A [OK]
Quick Trick: Look for @pytest.fixture decorator on a function [OK]
Common Mistakes:
MISTAKES
  • Defining fixtures without @pytest.fixture
  • Using classes instead of functions
  • Assigning values without a function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes