jeudi 3 décembre 2015

Mocking Python SDK Functions

I am attempting to create mock objects for the purpose of unit testing some of my code. The problem is that I have SDK calls embedded in the function I am trying to test. In the below I am attempting to mock invoke_elem(api) and xo.sprintf

Snippet of code I am attempting to mock is

   xo = self.conn.invoke_elem(api)
    obj = xmltodict.parse(xo.sprintf())
    if str(obj['results']['@status']) == "failed":
        logging.error("Error")
        raise CustomException(xo.sprintf(), self.params)
    elif str(obj['results']['@status']) == "passed":
        return True

The Test code is right here...it seems to mock invoke.elem properly but returns "AttributeError: sprintf" as I am uncertain as to how to write a mock for it.

class TestVolumeActions(unittest.TestCase):
def test_create(self):
    with patch('stor.Connect') as mock:
        connection = mock()
        root = ET.Element("results")
        doc = ET.SubElement(root, "status='passed'")
        connection.invoke_elem.return_value = root
        test = module.foo("bar")
        test.method()

if name == 'main': unittest.main()

Let me know if there is any other information I can provide to help. Thank you.

Aucun commentaire:

Enregistrer un commentaire