# pytest.fail.Exception: pytest.fail() called with message 'Test failed' and no active test

- **ID:** `python/pytest-fail-with-traceback-error`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

pytest.fail() is called outside of a test function, e.g., in a fixture setup or teardown, where no test context exists.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |

## Workarounds

1. **Move the pytest.fail() call inside a test function rather than in fixture code.** (95% success)
   ```
   def test_example(): pytest.fail('Test failed')
   ```
2. **Use assert statements in fixtures instead of pytest.fail.** (90% success)
   ```
   assert condition, 'Fixture condition not met'
   ```

## Dead Ends

- **Using pytest.fail in a conftest.py fixture without test context** — Fixtures are not tests; pytest.fail is intended for test functions only. (80% fail)
- **Catching the exception with try-except to continue execution** — pytest.fail raises a special exception that propagates; catching it may hide failures. (70% fail)
