类的初始化中的模拟方法

时间:2018-07-12 14:10:27

标签: python-2.7 mocking code-coverage

以下是我的课程模块:

# *****************************************************************************
# Module Description
# *****************************************************************************
"""
    TODO
"""

# *****************************************************************************
# Imports
# *****************************************************************************
import os
from executor_factory import ExecutorFactory
from noCliToolConstants import SDWANTOOL 
from configReader import ConfigReader
from logging_helper import getline_info


# *****************************************************************************
# Classes
# *****************************************************************************
class CredentialsHandler(ConfigReader):
    """
        TODO
    """

    def __init__(self):
        super(CredentialsHandler, self).__init__()
        try:
            self.client_key = self.get_client_key()
        except:
            self.client_key = None

    def collectdata(self, vmdid, request=None):
        if self.client_key is None:            
            return None
        uri = '/v1/vmd/{}/getcredentials?client_key={}'.format(vmdid, self.client_key) 
        executor = ExecutorFactory.create_executor(SDWANTOOL)
        response_json = executor.execute(uri, 'GET', request=request) 
        return response_json

我目前正在从事该课程的代码覆盖率工作,为此我编写了以下代码:

@mock.patch.object(SdwanToolApiExecutor, 'execute')
def test_cred_hdlr_collectdata(self, mock_execute):
    vmdid = 1
    return_dict = {'credentials': 'details'}
    mock_execute.return_value = return_dict
    apihandler = CredentialsHandler()
    resp = apihandler.collectdata(vmdid)
    self.assertIsNotNone(resp)

该模拟程序涵盖了几乎所有代码,但是并未涵盖构造函数init中的以下几行:

        except:
            self.key = None

我不确定该如何覆盖。一些指针会有所帮助吗? 谢谢。

0 个答案:

没有答案