Bird
0
0

What will be printed when running this pytest code?

medium📝 Predict Output Q4 of 15
PyTest - Fixtures
What will be printed when running this pytest code?
import pytest

@pytest.fixture
def data_resource():
    print('Start')
    yield 'info'
    print('End')

def test_data(data_resource):
    print(f'Using {data_resource}')
AUsing info\nEnd\nStart
BUsing info\nStart\nEnd
CStart\nEnd\nUsing info
DStart\nUsing info\nEnd
Step-by-Step Solution
Solution:
  1. Step 1: Fixture setup

    Prints 'Start' before yield.
  2. Step 2: Test runs

    Test prints 'Using info' using yielded value.
  3. Step 3: Fixture teardown

    After test, prints 'End'.
  4. Final Answer:

    Start\nUsing info\nEnd -> Option D
  5. Quick Check:

    Setup print, test print, teardown print [OK]
Quick Trick: Setup print, test print, teardown print [OK]
Common Mistakes:
MISTAKES
  • Assuming teardown runs before test print
  • Confusing order of prints
  • Ignoring yield pauses fixture

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes