python config_error ai_generated true

Failed: Skipped: 你需要在 pytest.mark.skipif 中指定跳过的原因 ERROR: 未找到 tests/test_x.py::test_y 的收集器

Failed: Skipped: you need to specify a reason the pytest.mark.skipif is skipping ERROR: found no collectors for tests/test_x.py::test_y

ID: python/pytest-skipif-string-condition

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-03-05首次发现

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

pytest.mark.skipif 需要同时提供条件和 reason 关键字。将字符串作为第一个位置参数传入(常见错误)会让 pytest 视为无效条件。

English

pytest.mark.skipif requires both a condition and a reason keyword. Passing a string as the first positional arg (a common mistake) makes pytest treat it as an invalid condition.

generic

解决方案

  1. 98% 成功率
    Use keyword arguments:
      @pytest.mark.skipif(sys.platform == 'win32', reason='POSIX-only')
      def test_x(): ...
  2. 95% 成功率
    For module-level skips:
      pytestmark = pytest.mark.skipif(condition, reason='...')

无效尝试

常见但无效的做法:

  1. 90% 失败

    Unconditionally skips the test even on platforms where it should run.

  2. 80% 失败

    Positional args are not supported for reason; must be keyword.