我正在尝试同时学习python和模拟python中的基础设施(由于工作场所的需求)。我还应该提到的是,我还不熟悉C++中的嘲讽特性或任何其他语言。
到目前为止,据我所知,通过mocking,我可以练习生成os的应用程序代码。联网等相关调用,而不实际调用这些操作。
假设我有一个应用程序,实现为network.py

#!/usr/bin/env python

import sys
import socket

class NetworkService(object):
    def sock_create(self):
        try:
            s = socket.socket()
            s.close()
            print "closed socket"
        except Exception, err:
            print "error creating socket"
            sys.exit(1)

我希望通过单元测试实现的目标是:
确保测试正常路径和故障路径。
在本例中,为了实现这一点,我试图提出一个示例单元测试用例来练习sock_create方法,如下所示:
#!/usr/bin/env python

import unittest
import mock
from network import NetworkService

class NetworkServiceTest(unittest.TestCase):
    @mock.patch('network.socket')
    def test_01_sock_create(self, mock_sock):
        reference = NetworkService()
        mock_sock.return_value = False
        # NetworkService::sock_create::s.close() should NOT get called
        reference.sock_create()
        self.assertFalse(mock_sock.close.called, "Failed to not call close")

        mock_sock.socket.return_value = True
        # NetworkService::sock_create::s.close() should get called
        reference.sock_create()
        # how to test this ???
        #mock_sock.close.assert_called_with("")

if __name__ == '__main__':
    unittest.main()

如上所示,最后一个“assert”语句当前已被注释掉;我不确定如何检查此语句以下是我的错误:
#!/usr/bin/env python

import unittest
import mock
from network import NetworkService

class NetworkServiceTest(unittest.TestCase):
    @mock.patch('network.socket')
    def test_01_sock_create(self, mock_sock):
        reference = NetworkService()
        mock_sock.return_value = False
        reference.sock_create()
        self.assertFalse(mock_sock.close.called, "Failed to not call close")

        mock_sock.socket.return_value = True
        reference.sock_create()
        self.assertTrue(mock_sock.close.called, "Should have called s.close")

if __name__ == '__main__':
    unittest.main()

以及错误:
$ python tester.py
F
======================================================================
FAIL: test_01_sock_create (__main__.NetworkServiceTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/mock/mock.py", line 1305, in patched
    return func(*args, **keywargs)
  File "tester.py", line 17, in test_01_sock_create
    self.assertTrue(mock_sock.close.called, "Should have called s.close")
AssertionError: Should have called s.close

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (failures=1)
closed socket
error creating socket

注意,我在Python2.7中使用mocking(mock需要作为单独的模块安装)

最佳答案

network.py中,您正在打印字符串。如果你把实际的错误打印出来,你就会看到它失败的原因。在本例中,您将看到它失败是因为attributerRor。AttributeError("'bool' object has no attribute 'close'",)
之所以会发生这种情况,是因为您将返回值设置为TrueFalse。由于bool没有任何open或close方法,它将抛出该错误。
其他一些建议。您使用的Exception将捕获所有异常。相反,只捕获你知道会发生的异常。找出socket可能抛出的异常。
那样的话你早就发现了。

关于python - 新手查询:在python中模拟,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42237939/

10-12 18:37