Bird
0
0

What will be the output when running this pytest fixture and test?

medium📝 Predict Output Q13 of 15
PyTest - Fixtures
What will be the output when running this pytest fixture and test?
import pytest

@pytest.fixture
def resource():
    print('Setup')
    yield
    print('Teardown')

def test_example(resource):
    print('Test running')
ATest running\nSetup\nTeardown
BSetup\nTeardown\nTest running
CSetup\nTest running\nTeardown
DTeardown\nSetup\nTest running
Step-by-Step Solution
Solution:
  1. Step 1: Trace fixture execution order

    Fixture prints 'Setup' before yield, then test runs printing 'Test running', then fixture prints 'Teardown' after yield.
  2. Step 2: Combine output sequence

    Output order is 'Setup', 'Test running', then 'Teardown'.
  3. Final Answer:

    Setup\nTest running\nTeardown -> Option C
  4. Quick Check:

    Setup -> Test -> Teardown order [OK]
Quick Trick: Yield splits setup (before) and teardown (after) print order [OK]
Common Mistakes:
MISTAKES
  • Assuming teardown runs before test
  • Mixing print order
  • Ignoring yield effect on execution flow

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes