Bird
0
0

Which of the following is the correct way to declare a fixture in conftest.py that returns a string value?

easy📝 Syntax Q3 of 15
PyTest - Fixtures
Which of the following is the correct way to declare a fixture in conftest.py that returns a string value?
A@pytest.fixture def greeting(): print('hello')
Bdef greeting(): return 'hello' @pytest.fixture
Cimport pytest def greeting(): return 'hello' @pytest.fixture()
Dimport pytest @pytest.fixture def greeting(): return 'hello'
Step-by-Step Solution
Solution:
  1. Step 1: Import pytest

    Fixtures require the pytest.fixture decorator, so importing pytest is necessary.
  2. Step 2: Define the fixture function with the decorator

    The function must be decorated with @pytest.fixture before its definition.
  3. Step 3: Return the desired value

    The fixture should return the string value as intended.
  4. Final Answer:

    Function decorated with @pytest.fixture and returns string -> Option D
  5. Quick Check:

    Decorator before function and proper import [OK]
Quick Trick: Always decorate fixture functions with @pytest.fixture [OK]
Common Mistakes:
MISTAKES
  • Placing @pytest.fixture after the function
  • Not importing pytest before using the decorator
  • Using print instead of return in fixture

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes