mardi 7 avril 2015

Mocking and patching keystoneclient in Python unittest

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):

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)

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

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


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?


Aucun commentaire:

Enregistrer un commentaire