单元测试模拟/覆盖套接字的发送

2024-04-26 14:25:07 发布

您现在位置:Python中文网/ 问答频道 /正文

[编辑:问题只存在于Python2.7中。在3.7版本中,它可以正常工作。有什么区别吗?有没有可能使它也适用于2.7,我出于某些原因不得不使用它]

我想用Mock覆盖socket的方法sendto。我的第一个方法是这样的:

class TestClass01(TestCase):
    @mock.patch.object(socket.socket, "sendto", new=mock_sendto)
    def test_function1(self):
        ...

但是,这导致了一个例外:

AttributeError: '_socketobject' object attribute 'sendto' is read-only.

这种方法在其他类中工作得很好,但在socket中没有。我认为,原因是__init__为所有成员函数调用setattr,如下所示:

def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
    if _sock is None:
        _sock = _realsocket(family, type, proto)
    self._sock = _sock
    for method in _delegate_methods:
        setattr(self, method, getattr(_sock, method))      <<<<<<<<<<<<<

然后,我尝试了另一种方法:

class TestClass01(TestCase):
    @mock.patch("socket.socket")
    def test_function1(self, mock_class):
        s = mock.Mock(socket.socket)
        s.sendto = mock_sendto
        s.close = mock_close

        mock_class.return_value = s

        # ... run my tests here ...

        args, kwargs = mock_class.call_args_list[0]

        # Check arguments passed to sendto

不过,这是可行的,调用参数列表只给出了:

((2, 2), {})

它对应于构造函数AF_INET=2SOCK_DGRAM=2。没有我调用sendto的痕迹。我的解决方法是使用全局变量,这是我从mock_sendto函数设置的。不过,不是很优雅。你知道吗

我有三个问题:

  1. 我能否解决“只读”问题并获得初始方法?你知道吗
  2. 为什么all_args_list只跟踪构造的调用而不跟踪sendto的调用?你知道吗
  3. 有比使用全局变量更好的方法吗?你知道吗

提前感谢您的回答!你知道吗


Tags: 方法selfdefargs原因socketmocktestcase