Bird
0
0

You have a fixture in conftest.py that establishes a database connection. To minimize connection overhead when running tests across multiple files, which fixture scope is most appropriate?

hard🚀 Application Q8 of 15
PyTest - Fixtures
You have a fixture in conftest.py that establishes a database connection. To minimize connection overhead when running tests across multiple files, which fixture scope is most appropriate?
Ascope='function' to create a new connection per test
Bscope='module' to share connection within a single test file
Cscope='class' to share connection within test classes only
Dscope='session' to share connection across all tests in the session
Step-by-Step Solution
Solution:
  1. Step 1: Understand fixture scopes

    Function scope creates a new fixture instance per test; module scope per test file; class scope per test class; session scope once per test session.
  2. Step 2: Consider resource cost

    Database connections are expensive to create and tear down.
  3. Step 3: Choose optimal scope

    Using scope='session' allows sharing the connection across all tests, minimizing overhead.
  4. Final Answer:

    scope='session' to share connection across all tests in the session is the best choice for resource optimization.
  5. Quick Check:

    Session scope shares fixture across entire test run [OK]
Quick Trick: Use session scope for expensive shared resources [OK]
Common Mistakes:
MISTAKES
  • Using function scope causing repeated setup/teardown
  • Choosing module scope limiting sharing to one file
  • Assuming class scope covers all tests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes