# 收集 tests/test_math.py 时出错
  File "tests/test_math.py", 第 7 行
    assert add(1, 2) == 3
                       ^
SyntaxError: 无效语法

- **ID:** `python/pytest-collection-error-syntax`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

测试文件中的语法错误阻止 pytest 在收集期间导入该文件，因此整个模块被报告为错误，而不是运行任何测试。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   python -m py_compile tests/test_math.py
   ```
2. **** (92% 成功率)
   ```
   # .pre-commit-config.yaml
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.6.0
    hooks:
      - id: check-ast
   ```

## 无效尝试

- **** — Skips the broken module but hides the syntax error and leaves the test suite silently incomplete. (70% 失败率)
- **** — SyntaxError is raised at parse time, before any import statement executes, so the try/except never runs. (85% 失败率)
