# pytest.fail('测试因超时而失败') -> 失败：测试因超时而失败

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

## 根因

测试显式调用pytest.fail()并附带自定义消息，这会立即停止测试并报告失败，通常用于标准断言未涵盖的条件检查。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.0.0 | active | — | — |
| 8.0.0 | active | — | — |

## 解决方案

1. **Use pytest.fail() with a descriptive message to provide context for the failure** (95% 成功率)
   ```
   if condition:
    pytest.fail('Expected condition to be False, but it was True')
This makes the test output clear.
   ```
2. **Replace with a custom assertion using pytest.assume for non-fatal failures** (70% 成功率)
   ```
   from pytest import assume
assume(not condition, 'Condition should be False')
This allows the test to continue but still report the failure.
   ```

## 无效尝试

- **Replacing pytest.fail() with a simple print statement** — This does not stop the test or report failure, so the test may pass even if the condition is met. (80% 失败率)
- **Using assert False instead of pytest.fail()** — This works but provides less descriptive error messages, making debugging harder. (30% 失败率)
