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

时间:2019-01-22 04:21:12

标签: python python-3.x python-2.7

我正在迁移项目的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__

1 个答案:

答案 0 :(得分:0)

您应在__gt__b内分配a.value

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