Bird
0
0

Given this fixture and test code, what will be printed when running the test?

medium📝 component behavior Q13 of 15
Flask - Testing Flask Applications
Given this fixture and test code, what will be printed when running the test?
@pytest.fixture
def sample():
    print('Setup')
    yield 'data'
    print('Teardown')

def test_example(sample):
    print(f'Test received: {sample}')
ASetup\nTest received: data\nTeardown
BTest received: data\nSetup\nTeardown
CSetup\nTeardown\nTest received: data
DTest received: data
Step-by-Step Solution
Solution:
  1. Step 1: Understand fixture execution order

    Before the test runs, the fixture prints 'Setup', then yields 'data' to the test.
  2. Step 2: Analyze test function output and fixture teardown

    The test prints 'Test received: data'. After test finishes, fixture resumes and prints 'Teardown'.
  3. Final Answer:

    Setup\nTest received: data\nTeardown -> Option A
  4. Quick Check:

    Fixture setup -> test -> fixture teardown [OK]
Quick Trick: Fixture prints before yield, teardown prints after [OK]
Common Mistakes:
MISTAKES
  • Assuming teardown runs before test
  • Ignoring yield pauses fixture
  • Thinking test runs before setup

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes