25

I'm trying to make a simple test in python, but I'm not able to figure it out how to accomplish the 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 try to make a connection, I need to mock that, I don't want to make a real connection, just return something mock. How can I make that? I'm used to do testing in Java but I never did on python..

4

2 回答 2

38

unittest.mock首先,非常重要的是要了解,您总是需要按照文档中的说明在使用您尝试模拟的东西的地方进行模拟。

基本原则是您在查找对象的位置进行修补,该位置不一定与定义对象的位置相同。

接下来,您需要做的是返回修补对象的MagicMock实例return_value。因此,总结这一点,您需要使用以下序列。

  • 补丁对象
  • 准备MagicMock使用
  • 返回MagicMock我们刚刚创建的return_value

这是一个项目的快速示例。

connection.py(我们想模拟的类)

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

file.py(使用类的地方)

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    

测试/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) 
于 2016-08-17T19:34:26.490 回答
0

我发现这个简单的解决方案适用于 python3:您可以在第一次导入之前替换整个类。假设我必须从 real.manager 模拟类“经理”

class MockManager:
    ...

import real.manager
real.manager.Manager = MockManager

如果没有更好的地方,可以在init .py中进行这种替换。它也可以在 python2 中工作,但我没有检查。

于 2020-09-04T14:45:24.083 回答