python data_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-04-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

`@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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 90% fail

    Skips unconditionally on every platform, permanently disabling the test.