Layout of the entire python class

I am trying to do a simple python test, but I cannot figure out how to execute a mocking process.

This is the class and def code:

class FileRemoveOp(...) @apply_defaults def __init__( self, source_conn_keys, source_conn_id='conn_default', *args, **kwargs): super(v4FileRemoveOperator, self).__init__(*args, **kwargs) self.source_conn_keys = source_conn_keys self.source_conn_id = source_conn_id def execute (self, context) source_conn = Connection(conn_id) try: for source_conn_key in self.source_keys: if not source_conn.check_for_key(source_conn_key): logging.info("The source key does not exist") source_conn.remove_file(source_conn_key,'') finally: logging.info("Remove operation successful.") 

And this is my test for the execute function:

 @mock.patch('main.Connection') def test_remove_execute(self,MockConn): mock_coon = MockConn.return_value mock_coon.value = #I'm not sure what to put here# remove_operator = FileRemoveOp(...) remove_operator.execute(self) 

Since the execute method is trying to establish a connection, I need to mock it, I don’t want to make a real connection, I just return something to the layout. How can i do this? I'm used to testing in Java, but I have never done this in python.

+6
source share
1 answer

At first it’s very important to understand that you always need a Mock, where this is what you are trying to mock, it is used as indicated in the unittest.mock documentation.

The basic principle is that you correct where the object is viewed, which does not necessarily coincide with the place where it is defined.

Next, you will need to return an instance of MagicMock as the return_value fixed object. So, to summarize this, you will need to use the following sequence.

  • Patch object
  • Prepare MagicMock for use
  • return the MagicMock we just created as return_value

Here is a brief example of a project.

connection.py (the class we would like to execute Mock)

 class Connection(object): def execute(self): return "Connection to server made" 

file.py (where the class is used)

 from project.connection import Connection class FileRemoveOp(object): def __init__(self, foo): self.foo = foo def execute(self): conn = Connection() result = conn.execute() return result 

Tests / test _file.py

 import unittest from unittest.mock import patch, MagicMock from project.file import FileRemoveOp class TestFileRemoveOp(unittest.TestCase): def setUp(self): self.fileremoveop = FileRemoveOp('foobar') @patch('project.file.Connection') def test_execute(self, connection_mock): # Create a new MagickMock instance which will be the # `return_value` of our patched object connection_instance = MagicMock() connection_instance.execute.return_value = "testing" # Return the above created `connection_instance` connection_mock.return_value = connection_instance result = self.fileremoveop.execute() expected = "testing" self.assertEqual(result, expected) def test_not_mocked(self): # No mocking involved will execute the `Connection.execute` method result = self.fileremoveop.execute() expected = "Connection to server made" self.assertEqual(result, expected) 
+8
source

All Articles