Bird
0
0

Given the code below, what will be the output when running pytest?

medium📝 Predict Output Q13 of 15
PyTest - Fixtures
Given the code below, what will be the output when running pytest?
import pytest

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


def test_one():
    print('Test one running')


def test_two():
    print('Test two running')
ATest one running\nTest two running
BSetup runs\nSetup runs\nTest one running\nTest two running
CSetup runs\nTest one running\nSetup runs\nTest two running
DSetup runs\nTest one running\nTest two running
Step-by-Step Solution
Solution:
  1. Step 1: Understand autouse fixture behavior

    The autouse fixture runs automatically before each test function in the module.
  2. Step 2: Trace the print statements during test runs

    For test_one: 'Setup runs' then 'Test one running'. For test_two: 'Setup runs' then 'Test two running'.
  3. Final Answer:

    Setup runs\nTest one running\nSetup runs\nTest two running -> Option C
  4. Quick Check:

    Autouse runs before each test = output B [OK]
Quick Trick: Autouse fixture runs before every test automatically [OK]
Common Mistakes:
MISTAKES
  • Assuming fixture runs only once
  • Ignoring print inside fixture
  • Thinking tests run without setup prints

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes