python module_error ai_generated true

pytest.skip.Exception: Skipped: no module named 'requests'

ID: python/pytest-mark-skip-missing-import

Also available as: JSON · Markdown · 中文
80%Fix Rate
92%Confidence
0Evidence
2025-01-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active

Root Cause

在测试中调用 pytest.skip() 但条件判断错误,或者 @pytest.mark.skipif 条件未正确评估。

generic

中文

在测试中调用 pytest.skip() 但条件判断错误,或者 @pytest.mark.skipif 条件未正确评估。

Workarounds

  1. 98% success 使用 importorskip
    import pytest
    requests = pytest.importorskip('requests')
  2. 90% success 使用 skipif 装饰器
    @pytest.mark.skipif(not ORG, reason='no requests')
    def test():
        import requests

Dead Ends

Common approaches that don't work:

  1. 在模块顶层 try/except 50% fail

    尝试在导入时使用 try/except 并跳过,但 pytest 收集阶段可能报错

  2. 条件逻辑错误 40% fail

    尝试使用 sys.modules 检查但条件写反