lundi 4 avril 2016

How to verify whether a method was called from another method in python using mock?

My main aim is to test whether 'session' is called in the 'commit_proto_zk' method.

I have already gone through the following link was not able to understand it: Assert that a method was called in a Python unit test

zooKeeper.py

def session(self, flag = ''):
    if flag == 1:
        return True
    return False 

esx.py

from zooKeeper import session
import sys

class EsxCollector():

    def collectHW(self):
        a1 = self.a()
        b1 = self.b()
        c1 = self.c()
        hwInfo = self.writeProto(a1, b1, c1)
        return hwInfo

    def a(self):
        return 'a'

    def b(self):
        return 'b'

    def c(self):
        return 'c'

    def writeProto(self, a1, b1, c1):
        return '{0} {1} {2}'.format(a1, b1, c1)


    def commit_proto_zk(self, hwInfo):  

        flag = 1
        zkSession = session(flag)

        if not zkSession:
            print "**no session**"
            sys.exit(1)
        else:
            return zkSession
        if isinstance(hwInfo,str):
            print "*****", hwInfo
            return True
        else:
            return False



    def ab(self, l):
        return l*2

def __main__():
    esxCollector = EsxCollector()
    hwInfo = esxCollector.collectHW()
    print esxCollector.commit_proto_zk(hwInfo)

if __name__=='__main__':
    __main__()

Aucun commentaire:

Enregistrer un commentaire