Bird
0
0

You want a fixture to run automatically once per test class and also provide a resource to test methods. How should you declare the fixture?

hard🚀 Application Q8 of 15
PyTest - Fixtures
You want a fixture to run automatically once per test class and also provide a resource to test methods. How should you declare the fixture?
A@pytest.fixture(scope='class', autouse=True) def setup_class(): return 'resource'
B@pytest.fixture(scope='function', autouse=True) def setup_class(): return 'resource'
C@pytest.fixture(scope='module', autouse=False) def setup_class(): return 'resource'
D@pytest.fixture(autouse=True) def setup_class(): return 'resource'
Step-by-Step Solution
Solution:
  1. Step 1: Identify required scope and autouse

    To run once per test class automatically, scope='class' and autouse=True are needed.
  2. Step 2: Check other options

    @pytest.fixture(scope='function', autouse=True) def setup_class(): return 'resource' uses function scope (runs per test), C disables autouse, and D uses default function scope.
  3. Final Answer:

    @pytest.fixture(scope='class', autouse=True)\ndef setup_class():\n return 'resource' -> Option A
  4. Quick Check:

    Use scope='class' with autouse=True for class-level automatic fixture [OK]
Quick Trick: Use scope='class' and autouse=True for class-level automatic fixture [OK]
Common Mistakes:
MISTAKES
  • Using function scope for class-level setup
  • Forgetting autouse=True for automatic run
  • Confusing module and class scopes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes