Bird
0
0

What will be the output when running the following pytest code?

medium📝 Predict Output Q4 of 15
PyTest - Fixtures
What will be the output when running the following pytest code?
import pytest

@pytest.fixture(autouse=True)
def setup():
    print('Setup running')

def test_one():
    assert True

def test_two():
    assert True
ASetup running printed only before test_two
BSetup running printed once before all tests
CSetup running printed twice, once before each test
DNo output printed because print is ignored
Step-by-Step Solution
Solution:
  1. Step 1: Understand autouse fixture default scope

    The fixture has default function scope and autouse=True, so it runs before each test function automatically.
  2. Step 2: Analyze print output

    Since setup() prints 'Setup running', it will print once before test_one and once before test_two.
  3. Final Answer:

    Setup running printed twice, once before each test -> Option C
  4. Quick Check:

    autouse + function scope = runs before each test [OK]
Quick Trick: Autouse fixtures with function scope run before every test [OK]
Common Mistakes:
MISTAKES
  • Assuming print runs only once
  • Thinking print is suppressed in pytest
  • Confusing fixture scope with autouse behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes