python
runtime_error
ai_generated
true
unittest.SkipTest: 跳过:'numpy' 未安装
unittest.SkipTest: Skipped: 'numpy' is not installed
ID: python/unittest-skip-unless-import-error
80%修复率
87%置信度
0证据数
2024-12-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
根因分析
使用 @unittest.skipUnless 但条件判断错误,导致测试被意外跳过。
English
使用 @unittest.skipUnless 但条件判断错误,导致测试被意外跳过。
解决方案
-
95% 成功率 使用 find_spec 检查
@unittest.skipUnless(importlib.util.find_spec('numpy'), 'numpy not installed') def test_numpy(self): pass -
90% 成功率 模块级导入检查
try: import numpy except ImportError: numpy = None @unittest.skipIf(numpy is None, 'numpy missing') def test(...):
无效尝试
常见但无效的做法:
-
在测试内部 try/except
40% 失败
尝试使用 try/except ImportError 包裹整个测试,但跳过逻辑不灵活
-
手动设置标志
30% 失败
尝试手动设置 skip 标志但忘记恢复