“MagicMock”可以与python3中的int进行比较吗?



我正在迁移我的项目的python版本(2->3)。测试适用于 python2,但抱怨 python3,错误就像

TypeError: '>' not supported between instances of 'MagicMock' and 'int'

这是一个最小案例

# test_mock.py
try:
    from mock import MagicMock
except:
    from unittest.mock import MagicMock
def test_mock_func():
    a = MagicMock()
    b = a.value
    if b > 100:
        assert True
    else:
        assert True 

只需运行py.test .

这些黑客不起作用

MagicMock.__le__ = some_le_method # just not working
MagicMock.__le__.__func__.__code = some_le_method.__func__.__code__ # wrapper_descriptor does not have attribute __func__
您应该在

ba.value 中分配__gt__

# self is MagicMock itself
b.__gt__ = lambda self, compare: True
# or
a.value.__gt__ = lambda self, compare: True

相关内容

  • 没有找到相关文章

最新更新