Bird
0
0

This fixture in conftest.py aims to provide a database connection:

medium📝 Debug Q7 of 15
PyTest - Test Organization
This fixture in conftest.py aims to provide a database connection:
import pytest

@pytest.fixture
def db_conn():
    conn = connect_db()
    yield conn
    conn.close()

Tests fail with NameError: name 'connect_db' is not defined. How can you fix this?
AChange <code>yield</code> to <code>return</code> in the fixture
BImport or define the <code>connect_db</code> function before using it in the fixture
CRemove the <code>@pytest.fixture</code> decorator
DRename the fixture to avoid conflicts
Step-by-Step Solution
Solution:
  1. Step 1: Understand the error

    NameError means connect_db is not defined or imported.
  2. Step 2: Fix by importing or defining

    Ensure connect_db is either imported from the module where it is defined or implemented in conftest.py.
  3. Final Answer:

    Import or define the connect_db function before using it in the fixture -> Option B
  4. Quick Check:

    Undefined functions must be imported or defined [OK]
Quick Trick: Always import functions used in fixtures [OK]
Common Mistakes:
MISTAKES
  • Assuming yield causes the error
  • Removing fixture decorator thinking it fixes error
  • Renaming fixture without fixing missing function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes