Bird
0
0

Which of the following is the correct way to define a pytest fixture for a Flask test client?

easy📝 Syntax Q12 of 15
Flask - Testing Flask Applications
Which of the following is the correct way to define a pytest fixture for a Flask test client?
A@pytest.fixture def client(): app = Flask(__name__) yield app.test_client()
B@pytest.fixture def client(): app = Flask(__name__) app.test_client()
Cdef client(): app = Flask(__name__) yield app.test_client()
D@pytest.fixture def client(): app = Flask(__name__) return app
Step-by-Step Solution
Solution:
  1. Step 1: Check the use of @pytest.fixture decorator

    The @pytest.fixture decorator is required to mark a fixture. One option lacks it.
  2. Step 2: Identify correct use of yield for setup and teardown

    @pytest.fixture def client(): app = Flask(__name__) yield app.test_client() uses yield to provide the test client and allows cleanup after tests, which is best practice.
  3. Final Answer:

    @pytest.fixture\ndef client():\n app = Flask(__name__)\n yield app.test_client() -> Option A
  4. Quick Check:

    Use @pytest.fixture and yield for fixtures [OK]
Quick Trick: Use @pytest.fixture and yield to define fixtures [OK]
Common Mistakes:
MISTAKES
  • Omitting @pytest.fixture decorator
  • Returning instead of yielding
  • Not yielding or returning the test client

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes