python
runtime_error
ai_generated
true
ERROR collecting tests/test_models.py File "tests/test_models.py", line 3 def test_model(: ^ SyntaxError: invalid syntax
ID: python/pytest-collection-error-syntax-in-test
80%Fix Rate
88%Confidence
0Evidence
2024-07-07First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
Root Cause
A syntax error in a test module (or a module it imports) prevents pytest from collecting it. The error aborts collection for the whole file, so other tests in that file never run.
generic中文
测试模块(或它导入的模块)中的语法错误导致 pytest 无法收集它。该错误会中止整个文件的收集,因此该文件中的其他测试永远不会运行。
Workarounds
-
95% success
python -m py_compile tests/test_models.py && pytest tests/test_models.py
-
90% success
ruff check tests/ && pytest
Dead Ends
Common approaches that don't work:
-
80% fail
Lets other files run but the broken file still contributes zero tests, silently reducing coverage.
-
75% fail
The tests in that file are permanently skipped; the syntax error is never fixed.