Newbiee query: mocking in python

I am trying to learn Python and the mocking infrastructure in Python at the same time (due to requirements in my workplace). It should also be mentioned that I am also not familiar with ridicule in C ++ or in any other language.

So far, from what I understood, it is that with a mockery I can implement the application code that the OS does. network-related, etc., without actually invoking these operations.

Say I have an application implemented as 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) 

The things I would like to achieve with my unit test are:

  • Be sure to check both normal and fault tolerant paths.

In this case, to achieve this, I try to find a unit test example that uses the sock_create method, as shown below:

#!/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

, ​​Python 2.7 ( )

+4
1

network.py . , . , , , - AttributeError. AttributeError("'bool' object has no attribute 'close'",)

, , , True False. bool , .

. Exception, . , . , .

, .

+1

Source: https://habr.com/ru/post/1669758/


All Articles