Bird
0
0

What is wrong with this pytest fixture usage?

medium📝 Debug Q7 of 15
PyTest - Fixtures
What is wrong with this pytest fixture usage?
import pytest

@pytest.fixture(scope='session')
def setup_api():
    print('Setup API')

class TestAPI:
    def test_one(self):
        setup_api()
ASession scope fixtures cannot be used in classes.
BFixture must be passed as a test function argument, not called directly.
CFixture function must return a value.
DFixture name must start with 'test_'.
Step-by-Step Solution
Solution:
  1. Step 1: Understand fixture usage in pytest

    Fixtures are injected by pytest as function arguments, not called directly inside test functions.
  2. Step 2: Identify incorrect usage

    Calling setup_api() directly inside test_one bypasses pytest fixture mechanism and will not work as intended.
  3. Final Answer:

    Fixture must be passed as a test function argument, not called directly. -> Option B
  4. Quick Check:

    Use fixtures as function arguments, not direct calls [OK]
Quick Trick: Pass fixtures as test arguments, don't call them directly [OK]
Common Mistakes:
MISTAKES
  • Calling fixture functions directly
  • Assuming fixtures must return values
  • Thinking fixture names must start with 'test_'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes