python runtime_error ai_generated true

ERROR collecting tests/test_math.py File "tests/test_math.py", line 7 assert add(1, 2) == 3 ^ SyntaxError: invalid syntax

ID: python/pytest-collection-error-syntax

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-01-09First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active
8.x active

Root Cause

A syntax error in the test file prevents pytest from importing it during collection, so the entire module is reported as an error rather than running any tests.

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Skips the broken module but hides the syntax error and leaves the test suite silently incomplete.

  2. 85% fail

    SyntaxError is raised at parse time, before any import statement executes, so the try/except never runs.