# AssertionError: assert 200 == 404
 +  其中 200 = <Response [200]>.status_code

- **ID:** `python/pytest-assertion-rewrite-failure`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

测试期望返回404状态码但收到了200。这表明API端点返回了成功响应而不是预期的错误。常见原因包括URL错误、缺少身份验证，或者资源实际存在而测试假设它不存在。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Print the full URL and check if the resource exists:

print(response.url)
# Manually query the database or use curl to confirm the resource is missing before the test.
   ```
2. **** (85% 成功率)
   ```
   Use fixtures to guarantee the resource does not exist:

@pytest.fixture
def cleanup_user():
    User.objects.filter(id=123).delete()
    yield
    User.objects.filter(id=123).delete()
   ```
3. **** (80% 成功率)
   ```
   If the endpoint returns 200 for authorized users, adjust the test to use an unauthorized client or a different user without access.
   ```

## 无效尝试

- **** — This masks the real issue and makes the test useless for detecting regressions; the test will always pass regardless of actual behavior. (95% 失败率)
- **** — If the resource is immediately available, sleeping won't change the status code from 200 to 404; it just slows down tests. (80% 失败率)
- **** — This bypasses the real integration and may hide bugs in the actual endpoint; the test no longer validates real behavior. (75% 失败率)
