Bird
0
0

Which of the following code snippets correctly defines a fixture factory in pytest that returns a function to create a user dictionary?

easy📝 Syntax Q3 of 15
PyTest - Fixtures
Which of the following code snippets correctly defines a fixture factory in pytest that returns a function to create a user dictionary?
Aimport pytest @pytest.fixture def user_factory(): return {'name': 'default'}
Bimport pytest @pytest.fixture def user_factory(): def create_user(name): return {'name': name} return create_user
Cimport pytest def user_factory(): def create_user(name): return {'name': name} return create_user
Dimport pytest @pytest.fixture def user_factory(name): return {'name': name}
Step-by-Step Solution
Solution:
  1. Step 1: Identify the fixture decorator

    The fixture must be decorated with @pytest.fixture to be recognized by pytest.
  2. Step 2: Define a factory function inside the fixture

    The fixture returns a function (factory) that can accept parameters and create objects dynamically.
  3. Step 3: Return the inner function

    The fixture returns the inner function itself, not the result of calling it.
  4. Final Answer:

    import pytest @pytest.fixture def user_factory(): def create_user(name): return {'name': name} return create_user correctly defines a fixture factory returning a function that creates a user dictionary.
  5. Quick Check:

    Fixture factory returns a function, not a direct value. [OK]
Quick Trick: Fixture factories return functions, not direct values. [OK]
Common Mistakes:
MISTAKES
  • Returning a direct value instead of a function
  • Missing the @pytest.fixture decorator
  • Calling the inner function instead of returning it

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes