Bird
0
0

What will be the output count of setup_env calls in this pytest code?

medium📝 Predict Output Q5 of 15
PyTest - Fixtures
What will be the output count of setup_env calls in this pytest code?
import pytest

@pytest.fixture(scope='function')
def setup_env():
    print('Setup Env')

class TestExample:
    def test_a(self, setup_env):
        pass
    def test_b(self, setup_env):
        pass
A1
B3
C0
D2
Step-by-Step Solution
Solution:
  1. Step 1: Understand function scope fixture behavior

    Function scope means the fixture runs once per test function that uses it.
  2. Step 2: Count test functions using the fixture

    Two test functions (test_a and test_b) use setup_env, so it runs twice.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Function scope fixture runs once per test function [OK]
Quick Trick: Function scope runs once per test function [OK]
Common Mistakes:
MISTAKES
  • Assuming function scope runs once per class
  • Confusing function and module scopes
  • Ignoring how many tests use the fixture

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes