`m magicmock'可以与python3中的int进行比较吗?

发布于 2025-01-27 19:21:51 字数 722 浏览 3 评论 0原文

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

I am migrating python's version (2->3) of my project. The tests works fine for python2, but complains for python3, the error is like

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

here is a minimal case

# 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 

just run py.test .

These hacks not work

MagicMock.__le__ = some_le_method # just not working

MagicMock.__le__.__func__.__code = some_le_method.__func__.__code__ # wrapper_descriptor does not have attribute __func__

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隐诗 2025-02-03 19:21:51

您应该分配__ GT __内部ba.value

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

You should assign the __gt__ inside b or a.value

# self is MagicMock itself
b.__gt__ = lambda self, compare: True
# or
a.value.__gt__ = lambda self, compare: True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文