python data_error ai_generated true

已跳过 [1] tests/test_api.py:10: 'sys.platform == "win32"'

SKIPPED [1] tests/test_api.py:10: 'sys.platform == "win32"'

ID: python/pytest-skipif-string-evaluated-true

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

版本兼容性

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

根因分析

`@pytest.mark.skipif("sys.platform == 'win32'")` 传入的是字符串,pytest 在自己的命名空间中求值;如果那里未导入 `sys`,表达式可能报错或被视为真值,导致所有平台都被跳过。

English

`@pytest.mark.skipif("sys.platform == 'win32'")` was passed a string, which pytest evaluates in its own namespace; if `sys` is not imported there, the expression may raise or be treated as truthy, skipping on all platforms.

generic

解决方案

  1. 96% 成功率
    import sys
    import pytest
    
    @pytest.mark.skipif(sys.platform == "win32", reason="POSIX only")
    def test_symlink():
        ...
  2. 85% 成功率
    import sys
    import pytest
    
    @pytest.mark.skipif(lambda: sys.platform == "win32", reason="POSIX only")
    def test_symlink():
        ...

无效尝试

常见但无效的做法:

  1. 80% 失败

    The skipif expression is evaluated at collection time in the marker namespace, not inside the test body.

  2. 90% 失败

    Skips unconditionally on every platform, permanently disabling the test.