# pytest.fail(_pytest.outcomes.Failed)：测试有意失败

- **ID:** `python/pytest-fail-outcomes-failed`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

显式调用 pytest.fail() 将测试标记为失败，通常由于自定义条件。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |

## 解决方案

1. **Use pytest.fail with a descriptive message** (90% 成功率)
   ```
   pytest.fail('Custom failure: expected condition not met')
   ```
2. **Raise a custom exception with pytest.fail** (85% 成功率)
   ```
   if not condition: pytest.fail('Condition failed: ' + str(condition))
   ```

## 无效尝试

- **Removing pytest.fail() call without fixing logic** — The underlying condition that triggered the fail remains unaddressed. (70% 失败率)
- **Replacing with assert False** — assert False does not provide a custom message and may be less informative. (50% 失败率)
