# pytest.fixture: error in fixture 'db_connection' during setup

- **ID:** `python/pytest-fixture-setup-error`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

The fixture function raised an exception during setup phase, before the test function could execute. This commonly occurs when the fixture performs I/O operations (database connections, file reads, network calls) that fail.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## Workarounds

1. **** (88% success)
   ```
   @pytest.fixture
def db_connection():
    conn = None
    try:
        conn = create_connection()
        yield conn
    except Exception as e:
        pytest.fail(f'DB setup failed: {e}')
    finally:
        if conn:
            conn.close()
   ```
2. **** (82% success)
   ```
   @pytest.fixture
def db_connection(mocker):
    return mocker.MagicMock()
   ```

## Dead Ends

- **** — The decorator only ensures the fixture runs, it does not handle exceptions raised during fixture setup. The underlying error remains unaddressed. (85% fail)
- **** — The exception occurs during fixture setup, before the test function body executes, so the try/except inside the test never catches it. (92% fail)
