0

I am writing unittest for a class looks like below. I am trying to assert if logging is properly called with patch keystoneclient. The class looks like below. Problem is, I cannot pass through the for statement and can never get to LOGGER.warning or LOGGER.info even after patching CredentialManager. I am new to whole unittest and Mock so I might not be understanding something clearly.

from keystoneclient.v3.client import Client
from keystoneclient.v3.credentials import CredentialManager
import logging

LOGGER = logging.getLogger(__name__)

class MyClass(object):

    def __init__(self):
    ...

    def myfunc(self):

        new_credentials = {}
        client = Client(
            username=self.username,
            password=self.password,
            auth_url=self.auth_url,
            user_domain_name=self.user_domain_name,
            domain_name=self.domain_name
        )

        abc = CredentialManager(client)

        for credential in abc.list():

            defg = str(credential.type)
            (access, secret) = _anotherfunc(credential.blob)

            if not defg:
                LOGGER.warning('no abc')

            if defg in new_credentials:
                LOGGER.info('Ignoring duplate')

            new_credentials[defg] = (access, secret)

My unit tests looks something like this,

import unittest
from mock import patch, MagicMock
import MyClass

LOGGER = logging.getLogger('my_module')

@patch('MyClass.Client', autospec = True)
@patch('MyClass.CredentialManager', autospec = True)
class TestMyClass(unittest.TestCase):

    def test_logger_warning(self,,mock_client, mock_cm):
        with patch.object(LOGGER, 'warning') as mock_warning:
            obj = MyClass()
            mock_warning.assert_called_with('no abc')

The error I am getting looks like this.

    for credential in abc.list():
AttributeError: 'tuple' object has no attribute 'list'

So even after patching CredentialManager with autospect, I am getting error on abc.list(). I need to get to the point where I can test LOGGER but it seems like this patching does not work for list(). How should I make this error go away and be able to pass through the for statment so I can assert on logging?

4

1 回答 1

1

这个答案要涵盖多少细节:

  1. 补丁顺序:@patch装饰器作为堆栈应用,这意味着第一个装饰器 -> 最后一个模拟参数
  2. 如果您希望CredentialManager().list()返回包含空的type内容,则应使用您的工具mock_cm来执行此操作
  3. obj.myfunc()如果你想测试一些东西,你应该打电话:)

编码:

从模拟导入补丁导入 unittest,模拟导入 MyClass

@patch('MyClass.Client', autospec=True)
@patch('MyClass.CredentialManager', autospec=True)
class TestMyClass(unittest.TestCase):
    #Use decorator for test case too... is simpler and neat
    @patch("MyClass.LOGGER", autospec=True)
    def test_logger_warning(self, mock_logger, mock_cm, mock_client):
        #pay attention to the mock argument order (first local and the the stack of patches
        # Extract the mock that will represent you abc
        mock_abc = mock_cm.return_value
        # Build a mock for credential with desidered empty type
        mock_credential = Mock(type="")
        # Intrument list() method to return your credential
        mock_abc.list.return_value = [mock_credential]
        obj = MyClass.MyClass()
        # Call the method
        obj.myfunc()
        # Check your logger
        mock_logger.warning.assert_called_with('no abc')

用于所有补丁的好点autospec=True:这是一个很好的做法。

无论如何,我想鼓励您将您的日志记录部分提取到一个方法中并通过一些虚假凭据对其进行测试:更简单,更好的设计:类似于

def myfunc(self):

    new_credentials = {}
    client = Client(
        username=self.username,
        password=self.password,
        auth_url=self.auth_url,
        user_domain_name=self.user_domain_name,
        domain_name=self.domain_name
    )

    abc = CredentialManager(client)

    for credential in abc.list():
        self.process_credential(credential, new_credentials)

@staticmethod
def process_credential(credential, cache):
    (access, secret) = _anotherfunc(credential.blob)
    defg = str(credential.type)
    MyClass.logging_credential_process(credential, not defg, defg in cache)
    cache[defg] = (access, secret)

@staticmethod
def logging_credential_process(credential, void_type, duplicate):
    if void_type:
        LOGGER.warning('no abc')
    if duplicate:
        LOGGER.info('Ignoring duplate')

测试更简单,看起来更好。

于 2015-04-08T13:13:49.313 回答